Git Cheat Sheet
Created 24 February 2014 15:58, updated 24 August 2017 19:22

We're switching to git (on github.com) at the moment, and I really wanted to use it properly instead of just relying on the GIU clients. I decided to learn it using the command-line, and I found this excellent tutorial online which really helped me understand some of the fundamentals: http://www.sbf5.com/~cduan/technical/git/.

This article contains my basic cheat sheet for Git, which more or less follows the general workflow when using a git repo.

git initInitialises a new git repository in the current folder.
git clone https://myrepo.comClones and initialises a remote git repository locally in the current folder - adds a remote repository reference named "origin".
git logView log changes.
git add .Recursively adds all changes to the repository.
git commit --dry-runSee what changes will be committed before actually running git commit.
git commit -m "My message"Commits changes to the repository.
git branchGet a list of local branches, with a star next to the current head.
git branch branch-name baseCreates a new branch based on an existing branch e.g. git branch test-branch HEAD.
git checkout branch-nameSwitches to a new branch and updates the local folder with the files from that branch.
git fetch originRetrieves remote changes and updates remote heads.
git pull originPulls all remote changes (origin can be replaced with a URL, for example).
git pull --rebase originPulls all remote changes but baselines them BEFORE your local changes, so your changes move on top of what everybody else has already contributed.
git push origin HEADPushes all changes back to the repository origin.
git clone --branch xyz https://github.com/MyOrg/MyRepo.gitClone a specific tag from a remote repo.
git mv MyFolder SubFolderMoves MyFolder into SubFolder.

Edit (30 June 2015): To prevent yourself from having to enter credentials with each command-line entry, you can configure it to use a local store with the following command: git config credential.helper store - this will result in one request for user credentials and then no more.