Rewriting Git History
Rewriting history in Git means modifying past commits, changing commit messages, reordering commits, or removing sensitive data. These actions can alter commit history and should be used carefully, especially in shared repositories.
1. Changing the Last Commit
1.1 Modify the Last Commit Message
If you made a commit but want to change its message:
git commit --amend -m "New commit message"
🔹 This updates the commit without creating a new one.
1.2 Add More Changes to the Last Commit
If you forgot to include a file in the last commit:
git add missing-file.txt git commit --amend --no-edit
🔹 This updates the commit while keeping the same commit message.
2. Changing Older Commits (Interactive Rebase)
2.1 Modify Multiple Past Commits
Use interactive rebase to edit, delete, or squash commits:
git rebase -i HEAD~3
🔹 This will open an interactive editor showing the last 3 commits.
Each commit will look like this:
pick a1b2c3 First commit
pick d4e5f6 Second commit
pick g7h8i9 Third commit
You can modify pick to:
reword
→ Edit commit messageedit
→ Modify the commitsquash
→ Merge commits
Save and close the editor to apply changes.
2.2 Edit an Older Commit
If you choose edit
, Git will pause at that commit:
git rebase -i HEAD~3
# Change "pick" to "edit" on the commit you want to modify
git add updated-file.txt # Add changes
git commit --amend # Modify commit
git rebase --continue # Continue the rebase
3. Reordering Commits
During an interactive rebase (git rebase -i HEAD~N
), simply change the order of commits in the editor.
Example:
pick a1b2c3 Second commit
pick d4e5f6 First commit
pick g7h8i9 Third commit
🔹 Swapping lines will change the commit order.
4. Deleting Commits
4.1 Remove the Last Commit
git reset --hard HEAD~1
🚨 Warning: This permanently deletes the commit!
4.2 Remove an Older Commit
Use interactive rebase (git rebase -i HEAD~N
) and change pick
to drop
:
drop a1b2c3 Unwanted commit
pick d4e5f6 Important commit
pick g7h8i9 Another commit
🔹 This removes a1b2c3
from history.
5. Removing Sensitive Data (Like Passwords)
5.1 Find the Commit with Sensitive Data
git log --oneline -p | grep "password"
5.2 Remove the File from History
If a file contains sensitive data, use:
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch sensitive-file.txt" \
--prune-empty --tag-name-filter cat -- --all
Then force push:
git push origin --force --all
🚨 Warning: This rewrites history and affects all collaborators.
6. Force Push After Rewriting History
If you've changed history, you need to force push:
git push --force
🔹 Use with caution! This overwrites the remote history.
For safer force push:
git push --force-with-lease
🔹 This only forces the push if no one else has updated the remote branch.
Conclusion
Rewriting history can be powerful but also dangerous in shared repositories. Always backup your work before making major changes.
Action | Command |
---|---|
Change the last commit message | git commit --amend -m "New message" |
Edit older commits | git rebase -i HEAD~N |
Remove the last commit | git reset --hard HEAD~1 |
Delete an older commit | git rebase -i HEAD~N (change pick to drop ) |
Remove a file from history | git filter-branch |
Force push changes | git push --force |
Would you like more details or examples? 🚀