We’ve all done it — deleted a file we shouldn’t have, merged things that aren’t supposed to be on the main branch, accidentally pushed up sensitive information, committing to the wrong branch…
GIT is fantastic when things go as planned — but not so much when it comes to undoing whatever mistake it is you made. In part, it’s because GIT is version control. The point is that you’re not supposed to be making changes to the past once a change has been committed.
But accidents and unintended acts of GIT commits happen. Here are 10 ways you can undo your GIT mistakes.
😫 1. Discard all your local changes in a particular file
You’ve saved something but accidentally also included changes in a file you didn’t want to include. What do you do? Use the following command:
git checkout HEAD targetfilename.js
😲 2. How to restore a deleted file in Git
You’ve deleted a file in git but want it back. Here is how you do it:
git checkout HEAD accidentallydeletedfilename.js
🤔 3. How to discard chunks/lines in a file
There are parts of the code you want to keep, while other parts you want to discard.
One thing is for sure — you don’t want to revert the entire file back to its original state. You can use
patch for this. Here is how you can do it:
git checkout -p index.js
-p stands for
patch. You can also write it in longhand with
--patch instead of
This will take us into an interactive mode where git will go through every chunk of change that’s occurred in the file. You get to chose which block of changes you want to keep or discard.
You can discard the change by typing
'y' for ‘yes’, or keep the changes by typing
n for ‘no’.
And that’s basically it for keeping/deleting certain chunks or blocks of code in git.
😁 4. Discarding all local changes
You just want a clean start on the current branch you’re on.
Use the following git command to achieve this:
git reset --hard HEAD
😐 5. How to revert a commit
You want to revert a commit without deleting things in between. Here’s how you can do it.
Let’s take a look at the commit flow below:
C1 --> C2 --> C3
Here, you have three distinctive commits. However, you want to revert a change done in
C2 without deleting
C3. You can do this via
revert, which will create another commit but with the reverted changes of the unwanted commit.
git log to bring up your previous commits. This will give you the git hash. Use this git hash as the reference point to which commit you want to revert.
git revert 9238be0
Git will now create a new commit that will undo the changes as performed by that particular commit.
😢 6. How to roll back and reset to an older Git commit
You’ve done something you shouldn’t and now you want to restore your git commit to an older version.
This means that we’re setting our head pointer to a previous commit. It also means that any commits that come afterward will disappear — sorta like it never happened in our commit history.
To do this, go into your
git log, grab the hash of the commit you want to reset to. Then use the following command, followed by the target git commit.
git reset --hard 02938jk3dd
--hard flag makes sure that we have a clean working after this action. This means that no local changes will be preserved and will only be left with the files as it was saved at that particular point in time.
😓 7. How to reset a specific file to an older Git commit
Something’s gone wrong. Someone has committed something strange to a file that was working at some point. The rest of the repo is fine — just that one file you want to target.
Here is how you roll back and reset a specific file to an older git commit.
First, you need to look at the logs of that specific file. This is how you do it:
git log -- nameoftargetfile.js
This will get only the commits where the specific file was changed. Grab the git hash of the commit you want to roll back to. Run the following git command:
git checkout 02931ssa1 -- nameoftargetfile.js
Take note that we are using
-- to tell git that we’re targeting a specific file and not an entire branch. Now, if we run
git status, it will show that we’ve restored that specific file to a previously saved state in the project.
Another note to take is that this revert is currently not saved until you do so via a new commit. It just rolls back the file to the specific state.
😖 8. How to recover deleted commits
You’ve done a git reset and now you’ve lost a few commits you didn’t want to lose. At first, it felt like a good idea but now you want your commits back. What do you do?
There is a tool called
reflog — this keeps track of everything that happens, including deletions. It keeps track of things like checkouts, commits, cherry-picks, and resets. The
reflog will list these actions in chronological order.
You can use the hash before your deleted commit in
reflog to roll back your delete.
git reset --hard 02938jk3dd
😨 9. Moving a commit to a new branch on GIT
You’ve committed some code but it’s on the wrong branch. In fact, it should have been on a new branch — not the one where the work is currently on.
For example, you’ve got code on the
master branch instead of its own feature branch.
First, create the feature branch.
git branch feature/blah
Now your new branch has everything that is on your master branch. The next step is to clean up your master branch by using
git reset --hard HEAD~1
HEAD~1 is telling git to roll back by 1 commit.
And that’s basically it. Now make sure you’re on the right branch by using
git checkout to switch to the correct one.
🙃 10. Moving a commit to a different branch
So you’ve made a commit — but you’re on the wrong branch. It’s supposed to be on a different branch that already exists.
What do you do?
First, you’ll need the commit hash. You can get this via
Second step is to check out the correct branch. For example:
git checkout feature/targetbranch
Now we can move our change over with the
cherry-pick command. To do this, use the following
git command, followed by the commit hash you want to move.git cherry-pick 98289fjdee
feature/targetbranch now has the commit. The final step is to clean up the original branch you accidentally committed to. To do this,
checkout the accident branch and use
git reset to revert the changes there.
git checkout whoopsiesbranch git reset --hard HEAD~1
That’s basically it. Be sure to share and subscribe to the newsletter for more helpful tips and tricks!