Git cheatsheet

Table of Contents

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) 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
  1. 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 call git 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

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.

  1. Make sure your name and email are correct
    git config --get-regexp user
    
  2. 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
    
  3. 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. 4. Double-check
  5. Push force

    Make sure you know what you're doing, then:

    git push --force
    
  6. 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
  1. git: split a commit
    1. interactive rebase, edit the commit
    2. reset the commit git reset HEAD^
    3. create the new commits
    4. continue rebase

Created: 2024-04-06 Sat 00:49