A short collection of Git commands I have used repeatedly in production workflows. Some commands are safe inspection tools; others rewrite history and should be used only when the blast radius is understood.

How do I change the author of my commits?

Sometimes it happens that you commit from different computers with different author and email configurations.

If you prefer consistency, the following steps can help remedy that:

Preferred modern approach: git filter-repo

git filter-branch is still seen in older examples, but it is slow and easy to misuse. For serious history rewrites, use git filter-repo.

git filter-repo --mailmap .mailmap

git filter-repo removes remote definitions by default as a safety measure. Re-add origin only after you have reviewed the rewritten history and are ready to coordinate a force push.

Example .mailmap:

Correct Name  Old Name 
Correct Name  

Legacy approach: git filter-branch

Use this only when you cannot install git filter-repo:

git filter-branch --commit-filter \
'export GIT_AUTHOR_NAME="Author Name"; \
export GIT_AUTHOR_EMAIL=authoremail; \
export GIT_COMMITTER_NAME="Committer Name"; \
export GIT_COMMITTER_EMAIL=committeremail; \
git commit-tree "$@"'

Push the changes to remote

git push -u -f origin --all
  • -u refreshes the association between local branches and the remote.
  • -f force-pushes rewritten history.
  • --all pushes all branches.

Do not rewrite shared repository history without coordinating with everyone who has cloned the repository.

Keep your clone in-sync

git fetch origin master
git reset --hard origin/master

Done.

Source: could I change my name and surname in all previous commits?

How many lines of code do I have?

Reading "How many lines of code is Facebook?" question, I found this git gem:

git ls-files | xargs cat | wc -l

For repositories with spaces in filenames, use null delimiters:

git ls-files -z | xargs -0 cat | wc -l

Recover recently lost work

If you reset, rebase, or amend something by mistake, check the reflog before panicking:

git reflog
git checkout HEAD@{1}

Once you find the commit you need, create a branch so it is not garbage-collected:

git switch -c recovery-branch <commit-sha>

Find the commit that introduced a bug

Use git bisect when you know one good revision and one bad revision:

git bisect start
git bisect bad
git bisect good v1.2.0

After testing each checkout:

git bisect good
git bisect bad

When finished:

git bisect reset

This is much faster than manually reading a long commit history.

Remove merged local branches

After a feature branch is merged, clean local branches that are no longer needed:

git branch --merged main
git branch --merged main | grep -vE '^\*| main$| master$' | xargs git branch -d

Review the branch list before deleting anything if the repository has long-lived release branches. On GNU/Linux, you can add xargs -r to avoid running git branch -d when the list is empty; macOS xargs does not support -r.

Use worktrees for parallel tasks

git worktree lets you check out another branch without stashing or disturbing your current working tree:

git worktree add ../project-hotfix hotfix/production-issue

This is useful when a production fix interrupts ongoing feature work.