Skip to content

Instantly share code, notes, and snippets.

@bmarcot
Last active September 16, 2019 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmarcot/8647350 to your computer and use it in GitHub Desktop.
Save bmarcot/8647350 to your computer and use it in GitHub Desktop.
Git cheat sheet
## <...>: a mandatory parameter
## [...]: an optional parameter
# Compare two versions of a file
$ git diff HEAD^^ HEAD main.c
$ git diff HEAD~2 HEAD main.c
# Create a patch from a diff between two branches
$ git format-patch -n <master>..<develop>
# Squash some commits together
$ git rebase -i HEAD~3
# Format the last patch before sending it by email
$ git format-patch [--cover-letter] [--subject-prefix="PATCH v2"] HEAD^
$ git format-patch [--cover-letter] [--subject-prefix="PATCH v2"] HEAD~1
# Send patches by email
$ git send-email --to <email address> [--supress-cc=all] *.patch
# Checkout a remote branch
$ git checkout -b <new branch> origin/<remote branch>
# Get one-line commit messages for a range of commits
$ git log --oneline HEAD[^^]..HEAD
# Reverse a rebase
$ git reflog
$ git reset --hard HEAD@{<before the rebase>}
# Create a pull request for the last commit in the current branch,
# and send that pull request to maintainers or mailing list.
$ git request-pull <target repo/branch> <repo url> | mailx <email address> -s <message subject>
$ git request-pull origin/master git@...:project.git | mailx toto@provider.com -s "pull my patch, please"
# Diff a file in two different branches
$ git diff <branch 1> <branch 2> -- <file>
# List all tags
$ git tag -l
# Checkout from a given tag
$ git checkout -b <topic> tags/<tag name>
# Patch a file with a version of that file from another branch
$ git checkout --patch <branch> -- <file>
# Delete a remote branch
$ git push <remote> --delete <branch>
# Reset branch to origin/branch
$ git reset --hard origin/<branch>
# Revert merge from merge commit
$ git revert -m 1 <merge-commit>
# Cherry-pick multiple commits (A older than B, A included)
$ git cherry-pick A^..B
# Fixup commits (to fixup A)
$ git commit --fixup A
$ git rebase -i A --autosquash
# Editing the Author of Past Commits
$ git filter-branch --env-filter '
WRONG_EMAIL="wrong@example.com"
NEW_NAME="New Name Value"
NEW_EMAIL="correct@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment