Git Quick Reference

9 minute read

Setting up

Initialise a new project:

~ $ git init project
~ $ cd project


~ $ mkdir project
~ $ cd project
~/project $ git init

Ref: Git Pro

Clone a repo from GitHub (or elsewhere):

~ $ git clone

Note: can also be done with SSH, or from elsewhere on local filesystem
Ref: Git Pro

Set user details for git:

~/project $ git config --global ""
~/project $ git config --global "Your Name"

Omit --global to set the identity only in this repository.
Note: If you don’t set these options, git will ask you to do so the first time you try to commit anything!
Ref: Git Pro

Tell git to cache (HTTPS) credentials:

~/project $ git config --global credential.helper cache

Omit --global to set the credential helper only in this repository. Note: Defaults to caching your credentials for 15 minutes
Refs: Git Reference | Git Pro

Check your settings:

~/project $ git config --list
~/project $ git config

Ref: Git Pro
See also: Git Configuration

Basic use

Show status of project:

~/project $ git status

This shows you which branch you are on and which files are ready to commit, unstaged yet, or untracked
Ref: Git Pro

Stage changes in file / directory ready for the next commit:

~/project $ git add <file|directory>

ie. this will add all changed files in the current directory:

~/project $ git add .

Refs: Git Pro - Tracking new files | Staging modified files

Remove a file:

~/project $ git rm file

Note: as well as removing the file from git’s index of tracked files, this will also delete the file.
If you just want remove the file from version control, but not actually delete the file, use:

~/project $ git rm --cached file

Ref: Git Pro

Rename a file:

~/project $ git mv file_from file_to

Note: if you move a file manually, git should work out that you’ve moved it - although you’ll have to run git rm file_from and git add file_to, so therefore git mv saves one or two commands!
Ref: Git Pro

Commit the staged files:

~/project $ git commit -m "<message - try to keep to less than 50 chars!..>"

If you omit -m "message" then a text editor will launch with a default commit message - this gives you the chance to add more lines of detail after the main commit message (I think…)
Ref: Git Pro

Commit all modified, tracked files, without needing to stage them first:

~/project $ git commit -a -m "<message>"

Ref: Git Pro

Working with Branches

List all local branches:

~/project $ git branch

Ref: Git Pro

List the last commit on all local branches:

~/project $ git branch -v

Ref: Git Pro

Create a new branch (at the current HEAD):

~/project $ git branch new-branch

Note: This will create the new branch, but it will not switch the working tree to it; use git checkout new-branch to switch to the new branch.

Create and checkout a new branch (at the current HEAD):

~/project $ git checkout -b new-branch

Switch to (‘checkout’) a branch:

~/project $ git checkout branch-name

Move a branch to point to a different commit:

~/project $ git branch -f branch-name new-target

new-target can be another branch name, or a commit ref.
Note: This will update the branch, but it will not switch the working tree to it; use git checkout branch-name to switch to the branch.
Ref: Git Reference

View Commit History

History for current branch:

~/project $ git log

Ref: Git Pro

Condense, graphed history of current commit:

~/project $ git log --oneline --graph --decorate

--oneline shows each commit on a single line
--graph draws an ASCII graph of the branch and merge history
--decorate add names of branches or tags to the commits shown
Ref: Git Pro

Summary details of one specific commit:

~/project $ git log --stat 44e07a3^!

--stat gives you a summary of which files were changed in a specific commit (44e07a3), and by how much (a ‘diffstat’) - omit the option to see the full diff for the commit
Ref: Git Reference - git log –stat
Note: <commit-ref>^! means the range that starts and ends with the specified commit (if you just pass a simple commit <commit-ref> to git log then it’ll return that commit and all its ancenstors - see Git Reference - Specifying Ranges)
Ref: Git Reference - Parent Shorthand Notations

Condense, graphed history for specific branch:

~/project $ git log --oneline --graph --decorate branch-name

Ref: Git Reference

Condense, graphed history for specific branch, for files in a specific directory:

~/project $ git log --oneline --graph --decorate branch-name -- dir-name

Ref: Git Reference

Remote Branches

See: Git Pro: Git Branching - Remote Branches

List all remote repositoriy references:

~/project $ git remote show

Add a remote repository:

~/project $ git remote add upstream

Ref: Git Pro

Show details of remote repository:

~/project $ git remote show origin

Change origin for the reference name of the remote repositiory!
Ref: Git Pro

List local branches, with details of what remote branches they are tracking:

~/project $ git branch -vv

Fetch status and contents of remote branches (but don’t merge with any local branches):

~/project $ git fetch origin

Merge a remote branch with the current local branch:

~/project $ git fetch origin
~/project $ git merge origin/remote-branch


~/project $ git pull origin remote-branch

Add an existing remote branch to local repository:

~/project $ git checkout -b new-branch origin/new-branch


~/project $ git checkout --track origin/new-branch


~/project $ git checkout new-branch

Ref: Git Pro

Push a local branch up to a remote server (first time):

~/project $ git push origin new-branch

Note: you don’t have to have new-branch currently checked out for this to work.
This will push the named local branch up to the remote repository, and configure it to do so again, along with other branches, when you do a git push --all - but not to pull back with a git pull.
Ref: Git Pro

Push current local branch up to a remote server:

~/project $ git push

This will push the current local branch up to its configured remote repository.
If the current branch doesn’t have a remote repository configured for pushing, it will push to origin (and configure it to do so again when you do a git push --all..?).
Ref: Git Reference

Push all local branches up to remote server:

~/project $ git push --all

This will push all the local branches (that are configured to do so?) up to their respective remote repositories.
Ref: Git Reference

Set current local branch to track a remote branch:

~/project $ git branch --set-upstream-to origin/new-branch


~/project $ git branch -u origin/new-branch

This will configure the current local branch to pull from the remote repository when you do a simple git pull.

Pull branches from the remote server:

~/project $ git pull

Exactly what this does depends somewhat on your repository and how things have been configured.

  • If the current branch has been set to track a remote branch, then the default behaviour is to pull that remote branch from it’s server and merge with the current branch - ie. it is equivalent to something like git fetch origin; git merge origin/this-branch.
  • If the current branch isn’t currently set to track a remote branch, then I think the FETCH_HEAD from the (default? origin?) remote repository is merged with the current branch, but I’m not sure (nor entirely certain what that means in relation to GitHub… it probably fetches and merges in the origin/master branch..?)
  • It appears that by default, if the current branch doesn’t have any tracking information, then git pull will throw an error and ask you to either specifiy which (remote) branch you wish to merge with, or set tracking information for the local branch (and tell you how!)

Note: It isn’t possible to pull multiple branches from their remote repositories with a single git command - see here
Refs: Git Reference | Git Pro

Stop current branch from tracking a remote branch:

~/project $ git branch --unset-upstream

This will stop the current local branch from pulling from the remote repositoriy when you do a simple git pull, but it will continue to push to the remote repository, along with other branches, when you do a git push --all.

Delete a remote branch (from the remote repository):

~/project $ git push origin --delete old-branch

Ref: Git Pro

Modify your history

See: Git Pro: Git Tools - Rewriting History

Note: Be very cautious of doing any of this if you have already pushed your project up to a remote server (eg GitHub) - and you should probably never do it if there’s a chance someone else has already pulled your work down from the remote server to their own computer!..

Note: Amending a commit kinda of forgets about the previous version of the commit, and creates a new one to replace it in the history / log. If anything refers to that previous version (you’ve branched off it, or you’ve pushed up to a server and someone else has branched off it) then those commits will effectivley be orphaned and its potentially a (big) headache to sort it out again.

Amend the previous commit:

If you need to tweak any files, do that first, then git add the relevant files

~/project $ git commit --amend

You’ll then be dropped into a text editor to update the commit message
Ref: Git Reference | Git Pro

Amend the previous commit, without editing the commit message:

~/project $ git commit --amend --no-edit

Amend the date of the previous commit:

~/project $ git commit --amend --date="Wed, 18 Apr 2018 13:00:00 +0100"

Ref: Git Reference - Date Formats

Amend the previous three commits - interactive rebase:

See: Git Pro: Changing Multiple Commit Messages

~/project $ git rebase -i HEAD~3

*Note: The commit ref that you pass will be the one that everything is rebased onto - that specific commit will not get changed.

Amend all commits on this branch:

~/project $ git rebase -i --root

*Note: If you modify any commit then it and all of its decendents will get new SHA-1s - so if you modify the initial commit then all the commits will get re-written. If there are any branches off any of the decendents then they won’t get updated and will still refer to the original commit history.

Remove a file from all commits:

~/project $ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

Ref: Git Reference | Git Pro

Make a subdirectory the new root:

~/project $ git filter-branch --subdirectory-filter subdirname HEAD

Ref: Git Reference | Git Pro


Based on various Git References and Git Cheat Sheets, and other resources around the web!..