Home » git » git cheatsheet

git cheatsheet

There are lots of good docs on git and github around. Perhaps, sometimes, too many! I am not attempting to add to them, just collating the specific sets of commands that I find useful in one place. I don’t promise they are useful for you, but the references to more complete docs may be.

Managing local git

git init
  • check current status
git status
  • commit changes
git commit -a -m "Commit message"
  • view commits
git log
  • view last 5 commits, with a graphical illustration of the relationship between them
git log -n 5 --graph
  • view diffs
git view
git diff HEAD^^ HEAD main.c
git diff <revision_1>:<file_1> <revision_2>:<file_2>

Getting from github

  • clone a git repository
git clone https://github.com/chr1swallace/coloc.git [mygitname]
  • clone a git repository and name it something different
git clone https://github.com/chr1swallace/coloc.git mynewgitname
  • show remotes
git remote show
git remote show origin
  • changes between local and remote
git fetch origin
git log master..origin/master
  • updating from github (origin points to remote, master points to branch)
git pull origin master

Pushing changes to github

  • put changes back on github
git push
  • add a github remote
git remote add -f origin https://github.com/chr1swallace/annotSnpStats.git


  • list tags
git tag
  • Add an annotated tag
git tag -a v1.5-1 -m "CRAN v1.5-1"
  • Push that tag to github (NB, this is not automatic with git push)
git push origin v1.5-1
  • Delete a tag
git tag -d v1.5-1
git push origin :refs/tags/v1.5-1


  • list branches and show current branch
git branch [--color]
  • create a new branch
git branch mynewbranch
  • switch branches
git checkout mynewbranch
  • push branch to github
git push origin mynewbranch
  • merge branch into master
git checkout master         # switch to master
git merge mynewbranch      # do the merge
git branch -d mynewbranch  # delete old branch

Github forks

git remote add <remotename> https://github.com/<user>/<repo>.git
git fetch <remotename>
git merge <remotename>/<branch>

usually branch will be master

  • Pulling in changes from a forked repo with a request on GitHub
git branch olly
git checkout olly
git pull https://github.com/ollyburren/wgsea master

check/edit new code, then merge branch into master as above


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s