September 01, 2012

Git Reference

Create a local branch
$ git checkout -b config origin/mainline
See the current branch and switch to another one
# Lists all branches and identifies the current one with *
$ git branch -a 
# Switches to "mainline" branch $ git checkout mainline
Copy commits from one branch ($B1) to another ($B2)
# Figure out the commit hash in $B1 with  
$ git checkout $B1
$ git log 
# Reset $B2 with this hash. # Keep in mind that all commits previous to this one will also be copied to $B2. $ git checkout $B2 $ git reset --hard $long-commit-hash
Pull from remote repository and make sure local changes are merged properly.
$ git pull --rebase
# If there are conflicts, they will appear in the "not staged to commit" list. 
# Open files (vim, emacs etc..), and fix them manually. 
# Then add them.
$ git add -u 
# Then complete the merge. 
$ git rebase --continue
# If something goes wrong, roll back at any time with 
$ git rebase --abort
Aggregate/re-arrange commits with rebase
# List all commits and figure out how many of them you want to change  
$ git log 
# Say, you want to update 5 last commits. 
# Open them up and rearrange according to instructions. 
$ git rebase -i HEAD~5
Add all files under "not staged for commit" list into "staged for commit" list
  
$ git add -u 
Show changes in a specific commit, i.e. diff a commit
  
$ git show $commit-hash