Git Reference
Initial setup
git config --global user.name "John Doe"
git config --global user.email john.doe@example.com
git config --global color.ui auto
# git config --global color.status auto
# git config --global color.branch auto
Committing
Adding partial modifications to index:
git add --patch [foo/bar]
Committing with showing patches:
git commit -v
Committing with auto-adding all unstaged changes:
git commit -a
Undoing
Reset working directory and index to last committed state (last head):
git reset --hard
Reset a single file to the version in the index:
git checkout -- foo/bar.rb
Reset a single file to the last committed version:
git checkout HEAD foo/bar.rb
Remove a change from the index (undoing a previous git add):
git rm --cached foo/bar.rb
Undo a commit (e.g. HEAD, i.e. the one before HEAD) by committing an undoing changeset:
git revert HEAD^
Rewriting unpublished history
Amending the last commit (HEAD):
git commit --amend
Interactive editing of last 10 commits:
git rebase -i HEAD~10
Merging using rebase — move history to different branch:
git checkout featurex
git rebase master
vim foo/conflicted.rb
git add foo/conflicted.rb
git rebase --continue
Stashing
Stash away working directory changes and index state:
git stash
List stashes:
git stash list
Restore from stash:
git stash apply
Branching
List local/remote branches:
git branch
git branch -r
Show branches in relation to each other:
git show-branch
Switch to branch:
git checkout featurex
Create and switch to branch:
git checkout -b featurey
Delete branch after merging it to master or some other branch:
git branch -D featurey
Branching Workflow
Create new topic branch based on master or commit sha1:
git checkout -b topic/foo master|<commit>
Push to somewhere:
git push origin topic/foo
Alternatively, merge back into master:
git checkout master
git merge topic/foo
Alternatively, for private branches containing checkpoint commits, merge as a single changeset back into master:
git checkout master
git merge --squash topic/foo
git commit -v
Replace master with other branch based on commit sha1:
git branch -m master oldmaster
git checkout -b master <commit>
git push -f origin master
Merging
Merge master into featurex:
git checkout featurex
git merge master
vim foo/conflicted.rb
git add foo/conflicted.rb
git commit
Remote repositories
Clone existing remote repository:
git clone git://whatever/foo.git [foo-bar]
List remotes:
git remote [-v]
Add new remote:
git remote add otherdev http://example.com/~otherdev/foo.git
Set remote branch of local master to remote origin, allowing git pull
to know what to fetch and merge without explicitly specifying:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
Fetch from remote without merging:
git fetch otherdev
Fetch from remote and merge:
git pull otherdev
Merge changes from remote fork, complete workflow (using detached HEAD mode):
git remote add otherdev http://example.com/~otherdev/foo.git
git fetch otherdev
git checkout otherdev/master
# test fork before merge
git checkout master
git merge otherdev/master
# test merged result
git push origin master
Creating new repositories
Creating an empty group-shared SSH-based repository:
mkdir foo
chmod 2775 foo
cd foo
git init --bare --shared=group
Creating a HTTP repository
mkdir webroot/foo.git
cd webroot/foo.git
git init --bare
git --bare update-server-info
cp hooks/post-update.sample hooks/post-update
chmod a+x hooks/post-update
Importing from Subversion to GitHub
With standard Subversion layout (trunk, branches, tags directories):
echo 'jdoe = John Doe <john.doe@example.com>' >> /tmp/foo.authors
svn2git https://svn.example.com/foo --authors /tmp/foo.authors
git remote add origin git@github.com:jdoe/foo.git
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
git push
Tagging
List tags:
git tag -l
Add tag and push tags to origin:
git tag -a foo-1.6.234
git push origin --tags
Remove tag and remove from origin:
git tag -d foo-1.6.234
git push :foo-1.6.234
Back to Knowledge Base.


