I have a project in a remote repository, synchronized with a local repository (development) and the server one (prod). I've been making some commited changes already pushed to remote and pulled from the server. Now, I want to undo those changes. So I could just git checkout
to the commit before the changes and commit the new changes, but I'm guessing that there will be problems to push them again to remote. Any suggestion on how should I proceed?
14 Answers
You can revert individual commits with:
git revert <commit_hash>
This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits that come after that. If you want to revert a range of commits, you can do it like this:
git revert <oldest_commit_hash>..<latest_commit_hash>
It reverts all the commits after <oldest_commit_hash>
up to and including <latest_commit_hash>
. On some versions of git it also reverts the <oldest_commit_hash>
, so double check if that commit gets reverted or not. You can always drop the latest revert commit (which reverts the oldest commit) with g reset --hard HEAD~
.
To find out the hash of the commit(s) you can use git log
.
Look at the git-revert man page for more information about the git revert
command. Also, look at this answer for more information about reverting commits.
-
53
-
4In git documentation, it says that revert command reverts commits between first and last commits (both first and last included). See Documentation Aug 29, 2016 at 12:55
-
7@aod is correct, this answer needs to be updated. the current git API for revert has
<oldest_commit_hash>
being included in the list of reverts– JHixsonJan 19, 2018 at 18:24 -
10with git 2.17.2 revert <old>..<new> does not include old but includes <new>– chingisDec 18, 2018 at 13:36
-
14just tried....final step is missed, after "git revert <commit_hash>" use "git push origin <your_Branch>"– VicXjMay 12, 2021 at 8:53
A solution that keeps no traces of the "undo".
NOTE: don't do this if someone already pulled your change (I would use this only on my personal repo.)
run:
git reset <previous label or sha1>
this will re-checkout all the updates locally (so git status will list all updated files)
then you "do your work" and re-commit your changes (Note: this step is optional)
git commit -am "blabla"
At this moment your local tree differs from the remote
git push -f <remote-name> <branch-name>
will force the remote branch to take this push and remove the previous one (specifying remote-name and branch-name is not mandatory but is recommended to avoid updating all branches with update flag).
!! watch-out some tags may still be pointing removed commit !! how-to-delete-a-remote-tag
-
15
-
11Exactly what I searched for. Somebody did a faulty commit and push and reverted it again afterwards. The branches could not be merged because of this and I wanted to get the repository to be in the correct state again (and removing the commit from history as they were faulty ones anyway)– byemuteOct 30, 2015 at 15:27
-
5Which "previous label or sha1" should I use?. Should I enter the last "correct" one or the one before that and re-do all the changes done by the last correct one?– elyschMar 28, 2016 at 15:19
-
21
-
3Exactly what I need. I've pushed a branch to master by mistake. And as a result I had many rubbish commits during all commits history. I just done
git push -f
for last correct commit and remote history cleaned up! Thanks! Dec 16, 2016 at 13:04
What I do in these cases is:
In the server, move the cursor back to the last known good commit:
git push -f origin <last_known_good_commit>:<branch_name>
Locally, do the same:
git reset --hard <last_known_good_commit> # ^^^^^^ # optional
See a full example on a branch my_new_branch
that I created for this purpose:
$ git branch
my_new_branch
This is the recent history after adding some stuff to myfile.py
:
$ git log
commit 80143bcaaca77963a47c211a9cbe664d5448d546
Author: me
Date: Wed Mar 23 12:48:03 2016 +0100
Adding new stuff in myfile.py
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit
I want to get rid of the last commit, which was already pushed, so I run:
$ git push -f origin b4zad078237fa48746a4feb6517fa409f6bf238e:my_new_branch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:me/myrepo.git
+ 80143bc...b4zad07 b4zad078237fa48746a4feb6517fa409f6bf238e -> my_new_branch (forced update)
Nice! Now I see the file that was changed on that commit (myfile.py
) shows in "not staged for commit":
$ git status
On branch my_new_branch
Your branch is up-to-date with 'origin/my_new_branch'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: myfile.py
no changes added to commit (use "git add" and/or "git commit -a")
Since I don't want these changes, I just move the cursor back locally as well:
$ git reset --hard b4zad078237fa48746a4feb6517fa409f6bf238e
HEAD is now at b4zad07 Initial commit
So now HEAD is in the previous commit, both in local and remote:
$ git log
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit
-
31This is the correct answer! this is exactly what needed o be done in my case thanks learned something new today :) May 17, 2018 at 3:31
-
3
-
12If you want to truly revert changes (as if you've never pushed them), this is the correct answer.– JacobMay 13, 2019 at 6:27
-
1
-
1
This will remove your pushed commits
git reset --hard 'xxxxx'
git clean -f -d
git push -f
Make a backup just in case
Two notes bumped up from comments:
'xxxxx'
is the hash code of the last commit that you want to keep.clean
is dangerous. It removes files, not under version control!
-
22This method properly rewrites the history to delete the commit. Awesome Aug 12, 2019 at 5:45
-
7
-
5Just to be clear (I wasn't sure): The commit will be the current commit after the operation. Not the last one to delete.– user1199624Apr 30, 2020 at 14:06
-
2
-
5
clean
is dangerous. It removes files, which not under version control! Sep 20, 2021 at 17:53
You can REVERT (or you can also call it DELETE) the Git Commit BOTH Locally and Remotely if you follow the steps as given below via git command line.
Run the following command to see the commit id that you want to revert
git log --oneline --decorate --graph
You will get like a following screenshot
If you also check remote (via Web Interface) then you can see that this would be same as shown below
As per screenshot currently you are on commit id e110322 however you want to revert back to 030bbf6 BOTH LOCALLY and REMOTELY.
Perform the following steps to DELETE/REVERT Commits Locally+Remotely
First Locally Reverting to commit id 030bbf6
git reset --hard 030bbf6
followed by
git clean -f -d
These two commands clean force reset to commit stage 030bbf6 as shown below in snapshot
now if you run
git status then you'll see that you are TWO Commits BEHIND from the remote branch as shown below
Run following to update your indexes (if there are any updates). It is recommended that you ask all developers not to accept any pull requests on main remote branch.
git fetch --all
Once you are done with it then you are required to Push this commit forcefully by using + symbol in-front of branch as shown below. I have used here as master branch, you can replace it with any
git push -u origin +master
now if you see the web interface of remote then commit there should be reverted as well.
-
1This is the best answer on here, because it actually explains with an example! Thanks for that. However, I get the following error: ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'gitlab.com:information-provision/collectors/deribit-collector.git' Despite having maintainer status etc Jul 19, 2021 at 8:09
-
1@user6400946 are you using '+' (plus) symbol as shown in the last command ?
git push -u origin +YourBrandName
– vibs2006Jul 19, 2021 at 12:04 -
Yes, howeverI just solved it. It seems my master branch was protected from force push and I needed to unprotect it temporarily (under gitlab repository settings). Jul 19, 2021 at 12:56
-
1
-
1
Let's say 61234
is the sha-number of the last good commit you want to keep.
git reset --hard 61234
git push -f
will remove completely all wrong commits without any trace.
Note: If you wanted to push (the commit you reset to) to a specific branch you'd use git push -f origin branch-name
instead.
-
2Thank you! And the commit history is innocent! To convenient getting sha-number I've used: $ git log --oneline <feature-branch-name>.– InaFKApr 14, 2021 at 11:16
-
2
-
I got
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
error for runninggit push -f origin master
. I think I have to change some configuration on my gitlab server.– BrianOct 29, 2021 at 9:15 -
I edited your answer because I almost ran
git push -f origin master
when I really wanted to reset a commit on a branch – glad it didn't go! Dec 23, 2021 at 17:27 -
1I'm like you , always need push-back , use this way : git reset --hard <last-good-commit-hash> and after git push -f origin branch-name Mar 8, 2022 at 5:57
2020 Simple way :
git reset <commit_hash>
(The hash of the last commit you want to keep).
You will keep the now uncommitted changes locally.
If you want to push again, you have to do :
git push -f
-
1It does not work always as the admin can block re-write history which is very common in the industry. So the solution mentioned by @gitaarik will work. git revert <commit_hash> git push origin <branch_name>– tejp124Oct 19, 2020 at 13:26
-
git revert HEAD -m 1
In the above code line. "Last argument represents"
1 - reverts one commits.
2 - reverts last two commits.
n - reverts last n commits.
You need to push after this command to take the effect on remote. You have other options like specifying the range of commits to revert. This is one of the option.
Later use git commit -am "COMMIT_MESSAGE"
then git push
or git push -f
-
9This is not true - the -m parameter specifies number of parent to revert to (typically 1 if you want to revert the incoming, "theirs", changes, as opposed to 2 for merged-to changes, "ours" - for merging 2 commits). It has nothing to do with number of commits reverted - if you want to revert a commit range, use
git revert ref1..ref2
Oct 27, 2016 at 13:18 -
2
Here is my way:
Let's say the branch name is develop
.
# Checkout a new temp branch based on one history commit
# (command to get history commit hashes: git log --oneline)
git checkout <last_known_good_commit_hash>
# Delete the original develop branch
git branch -D develop
# Create a new develop branch based on the temp branch
git checkout -b develop
# Force update the remote with the new branch
git push -f origin develop
-
2for me there were many things to learn from this answer, so I went with this.– AUROct 22, 2021 at 9:19
-
I faced with illegal symbols on mac and only this solution helped me save the project.– storenthJan 11 at 17:55
-
It seems too risky to delete the develop branch just to undo a commit. What if it's a very busy project? Jan 25 at 17:35
-
@Nikita128 You are right, if it's a very busy project and this method will remove others commits for sure. Jan 25 at 21:20
The reset hard worked for me: Thanks @Mo D Genensis and @vibs2006
git reset --hard 'your last working commit hash'
git clean -f -d
git push -f
-
This overrides the files that are changed and deletes the files that are added.– SahinDec 15, 2021 at 9:58
-
git reset <commit_hash>
(to get the<commit_hash> use git log --oneline)
git restore .
to restore all the changed files to the version of yout target commit
git push origin master --force
to force the push to your remote master branch. But be careful when using the force push if there is anyone working with you in the same branch
To do it cleanly:
git rebase -i <hash of last good commit, 9 chars is enough>
Now you will get a list of the commits from the last good commit to the HEAD
with options what to do with each commit. DROP
will throw away that commit. Save the file.
Now to fix the upstream do :
git push --force-with-lease
(With lease so you don't accidentally cause problems for someone else working on your pushed update)
This keeps the log clean by removing the wrong commit instead of introducing new commits fixing earlier erroneous commits.
-
1This doesn't work as it creates a diverged branch and you can't push it (not even with -force). But would be the nicest solution if it was working...– rimesJun 11, 2021 at 4:06
-
@rimes, I have used this type of construction, perhaps not often but, a number of times on code meant for production. A colleague (our resident git guru) uses it to squash code from PR's with a large number of commits to combine commits and keep the master branch log clean. Jun 16, 2021 at 8:47
You can do something like
git push origin +<short_commit_sha>^:<branch_name>
Another way to do this without revert (traces of undo):
Don't do it if someone else has pushed other commits
Create a backup of your branch, being in your branch my-branch
. So in case something goes wrong, you can restart the process without losing any work done.
git checkout -b my-branch-temp
Go back to your branch.
git checkout my-branch
Reset, to discard your last commit (to undo it):
git reset --hard HEAD^
Remove the branch on remote (ex. origin
remote).
git push origin :my-branch
Repush your branch (without the unwanted commit) to the remote.
git push origin my-branch
Done!
I hope that helps! ;)