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 RetroPie on Raspberry Pi &#8211; Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

Incredible Demos

  • By
    Google Font API

    Google recently debuted a new web service called the Font API.  Google's Font API provides developers a means by which they may quickly and painlessly add custom fonts to their website.  Let's take a quick look at the ways by which the Google Font...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

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!