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 I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • 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...

Incredible Demos

  • By
    HTML5 download Attribute

    I tend to get caught up on the JavaScript side of the HTML5 revolution, and can you blame me?  HTML5 gives us awesome "big" stuff like WebSockets, Web Workers, History, Storage and little helpers like the Element classList collection.  There are, however, smaller features in...

  • By
    Sexy Opacity Animation with MooTools or jQuery

    A big part of the sexiness that is Apple software is Apple's use of opacity. Like seemingly every other Apple user interface technique, it needs to be ported to the web (</fanboy>). I've put together an example of a sexy opacity animation technique...

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!