Git recipes: practical commands and examples
These are Git commands I've used in my own projects and collected here for future reference. They cover history, branches, stashes, commits, debugging with bisect, and moving work between repositories.
Navigate Git history
git log ; git lg
git checkout ca55e77e
git checkout master
git checkout HEAD^3
git checkout "HEAD@{1 month ago}"
git checkout :/Commit messageWork with branches
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" remoteWork with stashes
Restore a single file from a stash.
git checkout stash@{0} -- «filename»Stash all files, including untracked files.
git stash -uSave a stash with a descriptive message.
git stash apply -m "My stash with ABC"Commit changes
Interactively select lines from a file to stage for the next commit.
git add -p «filenames»Commit the staged changes.
git commit -m "your commit message"Find a bug with git bisect
Use git bisect to find the commit that introduced a bug.
Check revisions manually
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)Automate the checks
git bisect start
git bisect good «revision»
git bisect bad «revision»
git bisect «run/path/to/decision/script args…»Rewrite history with git filter-branch
These commands rewrite commit history. Keep a backup and coordinate with anyone sharing the repository before using them.
Correct author and committer details
#!/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 the filter-branch backup reference
git update-ref -d refs/original/refs/heads/masterMove commits to another repository
These examples assume you're already on the target branch where you want to apply the commits.
git remote add oldrepo https://github.com/path/to/oldrepo
git fetch oldrepoCherry-pick a range of commits
git cherry-pick --strategy recursive --strategy-option theirs «oldestCommitHash»^..«latestCommitHash»Cherry-pick the commits from a merge
git cherry-pick --strategy recursive --strategy-option theirs «mergeCommitHash»^..«mergeCommitHash»Handle deleted files and conflicts
When cherry-picking, your target branch may be missing some of the files involved. Resolve any conflicts before continuing.
git mergetool
# either c/m or d based on if you want to keep or delete the files
git cherry-pick --continueMy Git configuration
Loading https://raw.githubusercontent.com/marcofranssen/dotfiles/master/.gitconfig…