Git Recipes
This page contains a whole lot of Git commands that I personally used in the past to keep as a track record for future use.
Navigating history
git log ; git lg
git checkout ca55e77e
git checkout master
git checkout HEAD^3
git checkout "HEAD@{1 month ago}"
git checkout :/Commit messageBranching
git checkout -b develop # creates new branch develop and does checkout of develop
git merge feature/awesomeness # merges the branch "feature/awesomeness" into the current branch
git branch -d feature/bullshit # deletes local branch "feature/bullshit"
git push origin :feature/bullshit # deletes remote branch "feature/bullshit" from the "origin" remoteStash
Getting a single file from a stash…
git checkout stash@{0} -- «filename»Stash all files including untracked files…
git stash -uSave a stash with a specific message…
git stash apply -m "My stash with ABC"Comitting
Add specific lines of a file to the commit interactively.
git add -p «filenames»Commit the staged changes.
git commit -m "your commit message"Bisect
Trace buggy commit with git bisect…
Manual
git bisect reset # only needed after a bisect
git bisect start
git bisect good «revision»
git bisect bad «revision»
# git will checkout the next revision to check
git bisect (good | bad)Automated
git bisect start
git bisect good «revision»
git bisect bad «revision»
git bisect «run/path/to/decision/script args…»Filter Branch
Fix committer/author
#!/bin/sh
git filter-branch -f --env-filter'
n=$GIT_AUTHOR_NAME
m=$GIT_AUTHOR_EMAIL
case ${GIT_AUTHOR_NAME} in
"Marco Franssen") n="Marco Franssen" ; m="marco.franssen@gmail.com" ;;
"Marco Franssen marco.franssen@gmail.com") n="Marco Franssen" ; m="marco.franssen@gmail.com" ;;
esac
export GIT_AUTHOR_NAME="$n"
export GIT_AUTHOR_EMAIL="$m"
export GIT_COMMITTER_NAME="$n"
export GIT_COMMITTER_EMAIL="$m"
' HEADRemove filter-branch backup
git update-ref -d refs/original/refs/heads/masterMove commits to new Repository
These examples assume you are already on the branch you want to cherry-pick these commits into.
git remote add oldrepo https://github.com/path/to/oldrepo
git fetch oldrepoTake a specific subset of commits
git cherry-pick --strategy recursive --strategy-option theirs «oldestCommitHash»^..«latestCommitHash»Take all commits involved in a specific merge commit
git cherry-pick --strategy recursive --strategy-option theirs «mergeCommitHash»^..«mergeCommitHash»Handle deleted files and conflicts
As you are cherry-picking your target branch might not have all files. You might end up in some conflicts that you will have to handle.
git mergetool
# either c/m or d based on if you want to keep or delete the files
git cherry-pick --continueGit config
Loading https://raw.githubusercontent.com/marcofranssen/dotfiles/master/.gitconfig…