How to Git for Beginners

A best-practice model for using git:
http://nvie.com/posts/a-successful-git-branching-model/

Merge a development branch with master:

git checkout master
git pull origin master
git merge test
git push origin master

Simple rebase with conflict resolution

git checkout experiment
git rebase master

If there is a conflict at this time git will show an error and mark conflicting code in your source code. Resolve it and then continue, make sure you delete all the markers as well.

git rebase --continue

Bring an experimental branch up-to-date in comparison to the root branch

# in a secondary branch:
# as if work in the secondary branch had started on top of the changes made to the root branch in the meantime
git pull --rebase
# or
git checkout <root-branch>
git pull
git checkout <experiment-branch>
git rebase <root-branch>
# for example git rebase stage
# If there are conflicts you can either resolve them manually or use 
git checkout --ours foo/bar.js
# to take the experiment version use --theirs, for the root-branch version use --ours
# see http://stackoverflow.com/questions/8146289/how-to-get-their-changes-in-the-middle-of-conflicting-git-rebase
# finally add any changes
git add .
# before you run
git rebase --continue

change to a previous commit

# change to a previous commit
# find the commit id
git log
# revert to that commit id
git reset --hard commit_sha

Apply hotfixes:

checkout master, then do

git cherry-pick <COMMIT_HASH>

if you merge the whole branch later on git will only apply the commits that weren’t already in master that’s a pretty cool way to apply urgent patches.

git merge origin/master

or create a pull request on github, have somebody review / accept it.

cherry pick files (or commits) from another branch

git fetch
git checkout someotherbranch
git pull
git checkout workingbranch
git checkout someotherbranch folder/file.py

amend last comit that is already pushed

Watch out if others have referred to the previous commit they are in serious trouble afterwards

git commit --amend
git push -f

submodules

Watch out: In a git repo, only the commit id of a submodule is tracked.

Add a submodule:

git submodule add git@github.com:.../... filepath
git submodule init

Updating all submodules in a repo

git pull && git submodule init && git submodule update && git submodule status
git commit -a

already pushed to remote and want to squash commits?

git rebase -i <commit-id-of-first-commit-you-want-to-squash>

make sure to replace pick by ‘squash’ for all but the first commit in the list that git will show you.