Useful Git Commands

By  on  

I've used git quite a bit but I don't consider myself a git expert, per se. I often find myself looking up the same basic commands so I thought I'd share them there so I'd have a fast reference, and maybe this will help you out too!

Create and Checkout a New Branch

#branches from currently checked out directory
git checkout -b <branchName>

Checkout a Remote Branch

git checkout -b <localBranchName> origin/<remoteBranchName>

Abort Changes of a File

git checkout -- <fileName>

Modify the Previous Commit's Message

git commit --amend

Partial Change Checkin

git add --edit

Undo the Previous Commit

git revert HEAD^

Temporarily Stash Changes, Restore Later

# After changes have been made...
git stash

# Do some other stuff here, like switch branches, merge other changes, etc.

#Re-apply the changes
git stash pop

Delete a Remote Branch

git push origin :<branchName>

Pull in the Latest from a Shared Repository

# Add a remote branch
git remote add <remoteName> <gitAddress>
	# For example:  git remote add lightfaceOfficial git://github.com/darkwing/LightFace.git

# Get changes from that branch
git fetch <remoteName>

Tagging, Deleting, and Pushing Tags

# Create a Tag
git tag <tagName>

# Delete the tag
git tag -d <tagName>

# Push Tags
git push --tags

Who F'd it All Up?

git blame <fileName>

These basic git commands should help you on your way. Have a tip you'd like to share with others? Please share!

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Create a Quick MooTools Slideshow with Preloading Images

    I've been creating a lot of slideshow posts lately. Why, you ask? Because they help me get chicks. A quick formula for you: The following code snippet will show you how to create a simple slideshow with MooTools; the script will also...

  • By
    MooTools Zebra Tables Plugin

    Tabular data can oftentimes be boring, but it doesn't need to look that way! With a small MooTools class, I can make tabular data extremely easy to read by implementing "zebra" tables -- tables with alternating row background colors. The CSS The above CSS is extremely basic.

Discussion

  1. Fetch from all remote repositories:
    git remote update

    See a nice ascii graph of your branches:
    git log --oneline --graph --all

    • +1 on the ascii graph. I even make an alias for it.

      git config --global alias.timeline "log --oneline --graph --decorate"

      So all when i need my ascii graph I just:
      git timeline

    • in

      git remote update-git log --oneline --graph --all="git config --global alias.timeline "log --oneline --graph --decorate"{git timeline}
  2. +1 on the ascii graph. I even make an alias for it.

    git config --global alias.timeline "log --oneline --graph --decorate"

    So all when i need my ascii graph I just:

    git timeline

  3. Denis S.

    check this out “git – the simple guide” by @rogerdudler
    http://rogerdudler.github.com/git-guide/index.html

  4. Hey, just to expand on “undo previous commit”:


    git reset --hard HEAD~3

    This will revert three commits back.

  5. tried adapting to command git not to used to it so i settled for SmartGit pending i grasped the whole Git command line thingy

  6. Not sure why this works for you, but I really have to do this to stash:

    $ git stash save “The name of the stash”

    And then to put it back

    $ git stash apply

    Or

    $ git stash apply “The name of the stash”

  7. The below cmd is used for search:

    grep log –grep=”your search word”

  8. John W

    The ASCII timeline (above) can be customised further, to good effect, by using the –format option, e.g. to add time information about the commits (helpful in a timeline!):

    git log –graph –all –format=’%h %an %ad – %s’

    (or use %ar instead of %ad if you like relative dates)

    Mix with other options to suit. Full details of the format option are in the git-log man page (quite far down the page).

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!