DEV Community

Cover image for Undo wrong Git changes
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Undo wrong Git changes

The other day I got a lovely email from one of my readers asking how to revert a commit that was made wrongly.
I looked back at my articles and realized I haven't ever noted this down, so here we go.

We'll be looking at a couple of levels of changes. Each one required a slightly different approach.

  • You have not yet committed the changes
  • Undo a commit that has not been pushed
  • Undo a pushed commit

Note: This article describes the commands. Most GUI's have a visual integration for these commands.

Undo changes that you haven't committed in Git

It could happen that you have made some changes, but then you quickly realize that you did something that isn't right and should be reverted.

Note: you can use git status to see all open changes

Take the file name of the file you want to revert and execute the following command.

git checkout filename.extension
Enter fullscreen mode Exit fullscreen mode

This command will revert the file to what it used to be.

You can see an example below where I changed the astro.config.mjs file and reverted those.

Undo changes in Git

Undo a commit that has not been pushed

Perhaps you got to the point where you already committed the changes.
Luckily you didn't push them yet.

This could have various reasons. Perhaps you noticed you didn't include all files, or you missed a chance.

Run the following command to revert the last commit.

git reset --soft HEAD~
Enter fullscreen mode Exit fullscreen mode

Note: You can specify multiple commits by placing a number at the end.

For instance, if we want to undo the last two commits, we can run the following command.

git reset --soft HEAD~2
Enter fullscreen mode Exit fullscreen mode

You can see an example below where I committed two changes, then reset my commit so I could make changes again.

Undo committed changes in Git

Undo an already pushed commit

This one is the most common scenario and also the messiest.
You have committed and pushed code to Git.

Note that the option described below does not remove the previous commit. It simply reverts it to what it was.

Step one is to ensure that you are on a clean working directory. You shouldn't have any open new changes.

Then you'll need to find the hash for the specific commit you are trying to undo. You can find them on your online Repo (like GitHub, for instance).

GitHub commit hash

Take this hash and then head back over to your terminal.
You can now revert this commit by executing the following command.

git revert f193a76 --no-edit
Enter fullscreen mode Exit fullscreen mode

Note: The --no-edit is optional. It won't prompt you to edit the commit message that way

Once executed, you'll note that it will do the opposite of the commit locally.

The command will return the files to what they used to be before.

Now all that's left is to push the reverted code.

git push
Enter fullscreen mode Exit fullscreen mode

When you view your changes, you'll see the old commit is still there, but a new revert commit replaces the changes.

Git revert pushed commit

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (18)

Collapse
 
balastrong profile image
Leonardo Montini

Might be worth mentioning that in case you have a pushed commit, you can still do the same as for local commit if you have force rights on the repo.

git reset --soft HEAD~
Enter fullscreen mode Exit fullscreen mode

And you get rid of the commit on your local branch, then

git push --force
Enter fullscreen mode Exit fullscreen mode

And the commit is also gone from the upstream repo as well.

There are a few different ways to get a similar result (get code back to the previous state), it's good to know as many as possible to properly decide which one to use based on the situation :)

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks a lot Leonardo!
Indeed there are a lot of different option to keep track of so thanks for adding these 💖

If you want you can also adjust my original article to include these? *(leaving that up to you)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

I'd add it but also explain in bold that this workaround with --force can also create issues with other team members commits if you're working on the same branch 😂

As you'll need to pull changes first
.. Won't be nice to find out a mate's commit disappeared suddenly 😅

I'd rather prefer to aim a single commit by it's hash identifier just to make sure.

Still double-checking before committing and using MR/PR process from an individual branch is king to avoid issues.

Best regards

Collapse
 
zhixiangyao profile image
Yao Zhixiang • Edited
git reset HEAD~
git checkout .
git push --force
Enter fullscreen mode Exit fullscreen mode
git reset --hard HEAD~
git push --force
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rahulrakesh16 profile image
Rakesh S M

A better approach but it can cause issues if multiple people are working on same branch. i guess it can be tweaked abit more.

Collapse
 
hyggedev profile image
Chris Hansen

Hey great article! Just to confirm on 'git revert.' After finding the hash, and using the command, it immediately updates the local files? Or does the remote repo become 'ahead' of local version and must we do a 'git pull' to accept updated changes again!? I'd test it but I'm out right now 😂💯

Collapse
 
dailydevtips1 profile image
Chris Bongers

The revert is still local, you'll need to push it to get up to date on the remote.

Collapse
 
hyggedev profile image
Chris Hansen

Ahhh ofc, what am I saying lol. Thanks 👌

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
dailydevtips1 profile image
Chris Bongers

It happens, luckily we have some good ways of ondoing it 💖

Collapse
 
dailydevtips1 profile image
Chris Bongers

Yep, well still make mistakes time to time 😅

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

My IDE has a command for dropping a commit. Don't know/don't care about how the cli does it.

Git reset soft is my favorite solution for cleaning up the history

Collapse
 
dailydevtips1 profile image
Chris Bongers

Ah yes!
Big fan of using tools that do it for you.

Less struggle and less prone to mistakes.

Collapse
 
android77 profile image
G. Horton

Great post, saving it for later. :)

Collapse
 
bannysilvey profile image
BannySilvey

Can to undo a git clean? ادعيه لجلب الحبيب

Collapse
 
imzivko profile image
imzivko • Edited

Great guide but I have a totally unrelated question.
What's the name of the shell prompt you're using?

Collapse
 
dailydevtips1 profile image
Chris Bongers

I think that's in iTerm (could also be in WebStorm) not sure which one I used for this article.
Mainly look fancy due to OhMyZSH

Collapse
 
mrsinak profile image
sina

👏👏👏