my-notes

Debugging_and_Optimizing_Git_History

1 min read

Debugging and Optimizing Git History

Find bugs and clean up history.


πŸ” git bisect

Start Bisect

git bisect start

Begins binary search.

git bisect bad

Current commit is broken.

git bisect good v1.0.0

This version worked.


Test and Mark

git bisect good

This commit works.

git bisect bad

This commit is broken.


Finish

git bisect reset

Returns to original state.


Automate

git bisect run npm test

Automatically bisects using test command.


πŸ‘€ git blame

Find Author of Lines

git blame filename.txt

Shows who wrote each line.


Specific Lines

git blame -L 10,20 filename.txt

Blame lines 10-20 only.


Ignore Whitespace

git blame -w filename.txt

Ignores formatting changes.


Show Email

git blame -e filename.txt

Shows email instead of name.


Find by Code

git log -S "functionName"

Finds commits adding/removing this code.


Find by Message

git log --grep="fix"

Finds commits with "fix" in message.


Find by File Change

git log -- path/to/file.txt

History of specific file.


πŸ”„ Interactive Rebase

Clean Last 5 Commits

git rebase -i HEAD~5

Opens editor to modify commits.

Options:

  • pick - keep
  • reword - change message
  • squash - combine with previous
  • drop - remove

Squash All Into One

git rebase -i --root

Rebases from first commit.


✏️ Amend Commits

Fix Last Commit Message

git commit --amend -m "Corrected message"

Changes last commit message.


Add Files to Last Commit

git add forgotten-file.txt

Stage forgotten file.

git commit --amend --no-edit

Add to last commit.


🧹 History Optimization

Garbage Collect

git gc

Cleans up and compresses.


Aggressive Cleanup

git gc --aggressive

Thorough optimization.


Prune Unreachable

git prune

Removes unreachable objects.


πŸ’‘ Tips

Find Deleted Code
git log -S "deleted_function" --all
Who Deleted a Line
git log -p -- filename | grep -B 5 "deleted text"


#git #debug #history #optimize

Built with mdgarden