Git Commands – A Cheat Sheet

GitHub is a great place to store and share projects that you or your team has been developing. There are a number of clients you can download to interface with GitHub to checkout, pull, merge and everything else you need to do within a brach. But real power comes from using git via the command line. Plus it adds a little geek factor to your repertoire! Here’s a quick cheat sheet of useful commands. 

List your local branches

$ git branch

List remote branches

$ git branch -r

Create a new branch and switch to it in one command

$ git checkout -b new-branch-name

Add modified files to the head to get them ready for a commit, and list the modified files

$ git add -Av

Commit all your changes and add a commit message

$ git commit -am "commit message added here"

Pull changes from the remote tracked branch

$ git pull origin

Push your committed changes to the remote branch

$ git push origin

Push your local branch to the remote repository and enable tracking (-u)

$ git push -u origin <local-branch-name>

When a pull goes wrong, and you get stuck in a merge, reset your branch to the last valid commit

$ git reset --hard HEAD