Git tips and tricks

Stashing

Stash tracked changes with git stash.

Also stash untracked files:

git stash -u

Only stash some of the changes, git prompts interactively:

git stash -p # --patch

Create new branch with stashed changes:

git stash branch <branchname>

Remote

Add remote

$ git remote add <remotename> <url>

Branches

Removing branches

# Remove local branch
git branch -d <branch>

# Remove remote branch
git push origin --delete <branch>

Fetch and merge upstream

$ git fetch upstream
$ git checkout <branch>
$ git merge upstream/<branch>

Cherry picking

Cherry pick merge from another branch

For cherry picking merge commits, you need to specify which parent of the merge will be used. This can be specified with -m

# Get the parent commits

$ git show <merge commit sha>
commit <merge commit sha>
Merge: <parent 1 sha> <parent 2 sha>
# ...

Settings

Global settings

git config --global user.name "Ville Outamaa"
git config --global user.email my@email.com
git config --global core.editor emacs
# etc