Rewriting history | Introduction

Rewriting history | Introduction

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 message
  • edit → Modify the commit
  • squash → 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.

ActionCommand
Change the last commit messagegit commit --amend -m "New message"
Edit older commitsgit rebase -i HEAD~N
Remove the last commitgit reset --hard HEAD~1
Delete an older commitgit rebase -i HEAD~N (change pick to drop)
Remove a file from historygit filter-branch
Force push changesgit push --force

Would you like more details or examples? 🚀

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close