If you ever typed a commit message that made future you sigh with regret this is your survival guide. We will cover how to change the last commit message or content amend staged changes rewrite older commits and push rewritten history without summoning chaos
Sometimes you typo things sometimes you forget a file sometimes you want tidy history that does not scream amateur hour. Amending and rebasing let you clean up the mess so the git log actually tells a story and not a crime report
If you only need to fix the most recent commit message run this simple command
git commit --amend -m "Fix typo in README"
Want drama instead of a one liner omit the -m flag to open your editor. It is the same command but with more emotion
Staged files become part of the amended commit. Stage what you need then amend without changing the message like this
git add path/to/file
git commit --amend --no-edit
This replaces the last commit object so the tree matches what you staged. It is not creating a new commit so history looks tidy
If the problem is deeper than the last commit use interactive rebase to surgically edit history. For example to rewrite the last three commits run
git rebase -i HEAD~3
In the editor change pick to edit or reword for the commit you want to change. When rebase pauses apply fixes then run
git commit --amend
git rebase --continue
If you need to split a commit or change files you can amend at the stop point and continue. Interactive rebase is powerful and a little dangerous when used carelessly
If you rewrote commits that were already pushed update the remote using safe force. Use this to avoid accidentally clobbering other peoples work
git push --force-with-lease origin your-branch
This is safer than plain force because it checks that the remote has not moved unexpectedly. Also tell your teammates you are rewriting history unless you enjoy passive aggressive chats in standup
That is the practical bit. Amend for typos amend for missing files and rebase when you want to tidy history. If things go wrong the reflog is your friend and GitHub likely has a notification calling you out. Breathe deep and fix it properly
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.