Nathan Lamont

Notes to Self

Git Cheat Sheet

Make a new repository, locally & remote

locally

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

remote server

ssh -p 8022 git@colossus.biggerplanet.net
cd /Users/git/Repositories
mkdir myProject.git && cd myProject.git
git --bare init
exit

locally

git remote add origin ssh://git@yoursecret.domain.com:8022/Users/git/Repositories/myProject.git
git push origin master
Clone remote repository:
git clone ssh://git@yoursecret.domain.com:8022/Users/git/Repositories/myProject.git

Add All Untracked (new) files:

git add .

Commit changes & remove all deleted files:

git commit -a

Useful (one time) things from http://arthurkoziel.com/2008/05/02/git-configuration/

Global ignore

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

Make a new branch see http://book.git-scm.com/3_basic_branching_and_merging.html

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

Conflicts?

git diff

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

git mergetool

Delete a branch

git branch -d experimental

If a branch has changes that have not been merged into your current branch, this will complain. If you're certain you want to delete the branch anyway, use capital D flag -D instead.

diff a particular file on a particular commit

???

Remotes

List remotes

git remote -v

Rename remote

git remote rename origin destination

Submodules

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"

See http://stackoverflow.com/questions/2155887/git-submodule-head-reference-is-not-a-tree-error

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>

E.g.

git checkout develop -- foo.json

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.

List branches sorted by last commit in reverse chronological order:

git branch -v

Fancier, with date and committer

 git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'

git diff but exclude files by extension

git diff -- . ':!*.md'
# additional types can be listed
git diff -- . ':!*.md' ':!*.foo*'

Tagging

New release? Add a tag.

# add a tag
git tag -a v1.0.0 -m "Initial release"
# push current branch with tag
git push origin master --tags
# or push just the tag
git push origin v1.0.0
# list tags
git tag
## GitHub

Have an existing project and want to make it into a GitHub repo?

Make the repo in github then:

```shell
git remote add origin git@github.com:beepy/my-new-project.git
git push -u -f origin main

Git from the bottom up