git Cheatsheet

February 1, 2010 1 comment

When you’re new to git, it can get difficult to wrap your head around it at first. This is especially true if you’re migrating from subversion or when you’d rather spend more time writing your code than trying to learn git.

Git requires some learning, and you might as well spend the time doing it rather than wondering what just happened after running a command. Learn.github is a good place to start to learn the basics.

But even after spending some time learning git, there are going to be times that you’ve to painfully search the web/manual for basic stuff like working with remote branches or more complex stuff like handling a failed merge.

For the same reason, I’m creating this post as a reference point for myself to avoid repetitive searches. I’ll keep adding new commands here if and when I use them.

This is no way near to a complete reference for git and there might be better ways to do the things listed here. These are just the commands I use the most, in the manner I prefer to use them.

Basics

  • Commit and Stage all changes
    git commit -am "log message"
  • Push Changes to remote repo
    git push
  • Pull Changes to local repo
    git pull
  • Check current status of working tree
    git status
  • View diff
    git diff
  • Discard local uncommitted changes to a file (in this case, abc.txt)
    git checkout -- abc.txt

Branches ( Branch name is assumed to be ‘abc’ )

Working with local branches

  • Create a branch
    git branch abc
  • Switch to a branch
    git checkout abc
  • View all existing branches
    git branch
  • Remove a branch
    git branch -d abc
  • Merge a branch

    # In this case, merging abc into master
    git checkout master
    git merge abc
    

Working with remote branches

  • Push a local branch to remote repo
    git push origin abc
  • Delete a remote branch
    git push origin :abc
  • Setup a local branch to track a remote branch
    git branch --track abc origin/abc
  • Create a local branch, push it to remote repo. and then pull changes from it

    # Creating the new branch and switching to it
    git checkout -b abc;
    # Pushing the changes
    git push
    # Meanwhile someone else made changes to the remote version
    # Pulling those changes
    git pull origin abc
    # if you want to simply use 'git pull' to get the changes, do the following
    git config branch.abc.remote origin
    git config branch.abc.merge refs/heads/abc
    

    Alternative methods listed here.

Tags ( Tag name is assumed to be v1.0 here )

  • View all tags
    git tag
  • Create a tag referencing a particular commit using its checksum (annotated)

    # here commit checksum is assumed to be 98eda02.
    # to view the checksum of a commit, you can do
    git log --pretty=oneline
    git tag -am "v1.0" 98eda02
    
  • Push tags to remote repo
    git push --tags
  • Delete a local tag

    git tag -d v1.0
  • Rename a local tag

    	git tag NEW_TAG OLD_TAG
    	git tag -d OLD_TAG
  • Delete a remote tag

    git push origin :refs/tags/v1.0

Useful Git References

1 Comment »

RSS feed for comments on this post. TrackBack URI

Leave a comment