Published on

Git Snippets for Power Users

views·3 mins read

History & Commits

Fixup and Autosquash

Clean up your commit history by marking a commit as a fixup for a previous one.

# Commit as a fixup
git commit --fixup <commit-hash>

# Rebase and automatically squash fixups
git rebase -i --autosquash <base-branch>

Git Reflog

Find lost commits or undo a hard reset.

git reflog
# Reset to a specific state from reflog
git reset --hard HEAD@{index}

Search Commit Messages

git log --grep="bug fix"

Search Code in History

Find when a specific string was added or removed.

git log -S "secret_key"

Beautiful Log Graph

git log --graph --oneline --all --decorate

Branching & Stashing

Stash Specific Files

git stash push path/to/file1 path/to/file2 -m "partial stash"

Git Worktree

Work on multiple branches at once in different directories.

git worktree add ../new-feature-dir feature-branch

Delete Merged Branches

git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

Debugging

Git Bisect

Find the commit that introduced a bug.

git bisect start
git bisect bad          # Current is bad
git bisect good v1.0    # v1.0 was good
# Test and repeat:
git bisect good/bad
# Finish:
git bisect reset

Git Blame

See who changed each line of a file.

git blame -L 10,20 path/to/file

Configuration

Conditional Config

Use different emails for work and personal projects.

# In ~/.gitconfig
[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

Global Gitignore

git config --global core.excludesfile ~/.gitignore_global

Maintenance

Clean Untracked Files

# Dry run
git clean -fdn
# Real delete
git clean -fd

Prune Remote Branches

Remove local references to branches that were deleted on remote.

git fetch --prune

Change Last Commit Message

git commit --amend -m "New message"