Git cheatsheet
Table of Contents
- 1. Git
- 1.1. Configurations
- 1.2. Finding stuff
- 1.2.1. git: Go to the root of the repository
- 1.2.2. git: Find the current branch's name
- 1.2.3. git: Find the current branch's remote
- 1.2.4. git: Find the branches that contains a specific commit
- 1.2.5. git: Find the root(s) branch(es) of a repository
- 1.2.6. git: Find commits by user
- 1.2.7. git: Find commit by message
- 1.2.8. git: Bisecting
- 1.2.9. git: List all the authors
- 1.3. Branches
- 1.4. Rewriting history
1. Git
1.1. Configurations
1.1.1. git: Setup a global git ignore file
git config --global core.excludesfile ~/.gitignore
1.1.2. git: Prune when fetching
To prune branches and tags that were deleted on the remote server when fetching:
git config --global fetch.prune true git config --global fetch.pruneTags true
These can also be set per-remote using the
remote.<name>.prune{,Tags}
config.
1.2. Finding stuff
1.2.1. git: Go to the root of the repository
cd "$(git rev-parse --show-toplevel)"
1.2.2. git: Find the current branch's name
git rev-parse --abbrev-ref HEAD
1.2.3. git: Find the current branch's remote
git config branch.<name>.remote
1.2.4. git: Find the branches that contains a specific commit
git branch -a --contains <commit>
1.2.5. git: Find the root(s) branch(es) of a repository
git rev-list --max-parents=0 HEAD
1.2.6. git: Find commits by user
git log --all --author=<pattern>
1.2.7. git: Find commit by message
git log [--all] --grep=needle
--all
searches across all branches
1.2.8. git: Bisecting
git bisect start [<bad-rev>] [<good-rev>] git bisect [good|bad|skip] [<rev>] git bisect log git bisect reset
- git: Automated bisecting
git bisect start git bisect run command...
Tips:
- you don't need to use
git bisect run
, you can start by making a test script and manually callgit bisect [good|bad]
- don't forget to make your test script executable
- do manually test your script on good and bad checkouts
- the coreutils'
timeout
command might be very useful. - don't track the test script in git, it will get reverted by the
git bisect
command. if it's already tracked, just make an untracked copy. git-bisect consider return codes > 127 as failure
return code meaning 0 good 1-127 \ 125 bad 125 skip 128-255 stop bisecting - you might want to apply a patch before running the tests, you can do it in the script
- random idea: maybe keep a list of known bad commits if your history is messy
- you don't need to use
1.2.9. git: List all the authors
Only the current branch:
git shortlog --summary --numbered --email
For all branches:
git shortlog --summary --numbered --email --all
N.B. Use git gc
to remove unreachable objects (e.g. if you changed
the author of some commits).
1.3. Branches
1.3.1. git: Checking out a path from another branch
git checkout other-branch -- path
1.3.2. git: Fast-forwarding a local branch without checking it out
git fetch remote src-branch:dst-branch
1.3.3. git: creating a branch from the current one without checkout
git branch new-branch-s-name
1.4. Rewriting history
See also the offical documentation: 7.6 Git Tools - Rewriting History
1.4.1. git: Change the email of the commiter of a bunch of commits
If you need to change a lot of commits/branches, maybe consider using git-filter-repo instead of doing a lot of risky, manual commands.
- Make sure your name and email are correct
git config --get-regexp user
- Find the commit from which you want to change the history
git log --pretty='format:%h%x09%an%x09%ae' | uniq -f 3 --group=append
- Rebase, resetting the author of each commit
git rebase -r REF --exec 'git commit --amend --no-edit --reset-author'
Or, if even the first commit needs to be changed:
git rebase --root --exec 'git commit --amend --no-edit --reset-author'
- 4. Double-check
- Push force
Make sure you know what you're doing, then:
git push --force
- Do it for all branches that needs it
1.4.2. git: Automated "interactive" rebase
https://stackoverflow.com/a/12395024/1564493
Here is another example that fixes a typo in the last five commit messages:
EDITOR="sed -i -e 's/borken/broken/g'" GIT_SEQUENCE_EDITOR="sed -i -e 's/pick/reword/g'" git rebase -i HEAD~5
GIT_SEQUENCE_EDITOR="sed -i -re 's/^pick /e /'" git rebase -i