Git Cheat Sheet

October 12th, 2016

Make a new repository, locally & remote


cd /Users/nathan/myProject
git init
git add .
git commit -m "initial commit"

remote server

ssh -p 8022
cd /Users/git/Repositories
mkdir myProject.git && cd myProject.git
git --bare init


git remote add origin ssh://
git push origin master
Clone remote repository:
git clone ssh://

Add All Untracked (new) files:

git add .

Commit changes & remove all deleted files:

git commit -a

Useful (one time) things from

Global ignore

echo ".DS_Store">> ~/.gitignore
git config --global core.excludesfile ~/.gitignore

Make a new branch see

git branch experimental

Make a new branch and check it out

git checkout -b experimental

List branches

git branch

Switch to branch

make sure you commit your first branch before you go crazy with new branch!

git checkout experimental

Make changes, switch back to main branch, e.g.

git commit -a
git checkout master

Branches diverged; merge them with

git merge experimental


git diff

or to use filemerge (which should be set up it .gitconfig)

git mergetool

Delete a branch

git branch -d experimental

diff a particular file on a particular commit



List remotes

git remote -v

Rename remote

git remote rename origin destination


Create a Submodule

mkdir mysubmoduledir
cd mysubmoduledir
git init

[make files in submodule]

git commit -m "initial commit for mysubmodule"
cd ..
git submodule add mysubmoduledir mysubmodule
git commit -m "added submodule mysubmodule"

Clone project with submodule

git clone cloned
cd cloned
git submodule init
git submodule update

Always publish the submodule change before publishing the change to the superproject that references

cd mysubmoduledir
[update files]
git commit -am "changes to mysubmodule"
git push
cd ..
git commit -am "updated mysubmodule"
git push

Pulling submodule changes

git pull origin master
git submodule update

If you get "fatal: reference is not a tree" error, you may not have pushed the commit of the submodule. Go to the "source" submodule, commit and push the submodule, then commit and push the parent repository, then try "git pull origin master," and "git submodule update" again on the "destination" parent repository.

Fixing Things

Forgot to add a file to .gitignore before changing it?

git update-index --assume-unchanged dir-im-removing/

or a specific file

git update-index --assume-unchanged path/to/file.txt

Forgot to add a file to .gitignore before adding (but not committing) it?

git reset HEAD dir-im-removing/

Forgot to add a file to .gitignore before adding and committing it (appears to work, does not delete files on disk)?

git rm --cached -r dir-im-removing/

or for a file

git rm --cached path/to/file.txt

Fix "Not Currently on Any Branch"

git commit -a -m “any changes” commit any changes you've made
git checkout -b temp create a new branch at current head
git checkout master checkout master (or whatever branch you want to be on)
git merge temp merge with the temp branch you made with your changes
git branch -d temp delete the temp branch

Fix "fatal: empty ident"

(if setting git config per instructions doesn't work)

... then add the following parameters to your ~/.bashrc file and source it with . ~/.bashrc (or relogin, alternatively):

 export GIT_AUTHOR_NAME="Nathan Lamont"
 export GIT_COMMITTER_NAME="Nathan Lamont"

Fix "reference is not a tree"


Colorized Output

git config --global color.branch auto
git config --global color.diff auto
git config --global color.interactive auto
git config --global color.status auto
TextMate as the default editor:gitgit config --global core.editor "mate -w"
Opendiff (FileMerge) to resolve merge conflicts:
git config --global merge.tool opendiff

Check out a specific file from another branch

git checkout <branch> -- <path>


git checkout develop -- foo.json

NOT YET CONFIRMED: To see what's different in a particular file between two commits:

git diff <revision_1>:<file_1> <revision_2>:<file_2>

Between branches:

git diff mybranch master -- myfile.cs

Files/directories present that haven't been added and you want to delete? This deletes them.

git clean -f -d

Omit -d to exclude directories from the operation.