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
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

Incredible Demos

  • By
    Degradable SELECT onChange

    Whenever I go to Google Analytics I notice a slight flicker in the dropdown list area. I see a button appear for the shortest amount of time and the poof! Gone. What that tells me is that Google is making their site function...

  • By
    Form Element AJAX Spinner Attachment Using MooTools

    Many times you'll see a form dynamically change available values based on the value of a form field. For example, a "State" field will change based on which Country a user selects. What annoys me about these forms is that they'll often do an...

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!