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
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Introducing MooTools ElementSpy

    One part of MooTools I love is the ease of implementing events within classes. Just add Events to your Implements array and you can fire events anywhere you want -- these events are extremely helpful. ScrollSpy and many other popular MooTools plugins would...

  • By
    Cross Browser CSS Box Shadows

    Box shadows have been used on the web for quite a while, but they weren't created with CSS -- we needed to utilize some Photoshop game to create them.  For someone with no design talent, a.k.a me, the need to use Photoshop sucked.  Just because we...

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!