How to remove a commit that has been pushed to the remote repository using Git
Asked Answered
C

4

21

I've pushed a couple of commits to the remote repository and found they are creating problems.

How can I go back to the previous version? That is, removing the two latest commits?

Cherian answered 16/3, 2012 at 2:57 Comment(0)
C
49

Since you have already pushed the commits to a remote repository, the best way is probably to revert the two commits so that you do not create any problems for anyone who has already pulled from the remote repository.

Examples use the following commit history:

e512d38 Adding taunts to management.
bd89039 Adding kill switch in case I'm fired.
da8af4d Adding performance optimizations to master loop.
db0c012 Fixing bug in the doohickey

If you just want to revert the commits without modifying the history, you can do the following:

git revert e512d38
git revert bd89039

Alternatively, if you don’t want others to see that you added the kill switch and then removed it, you can roll back the repository using the following (however, this will cause problems for others who have already pulled your changes from the remote):

git reset --hard da8af4d
git push origin -f localBranch:remoteBranch

where localBranch is the name of the local branch and remoteBranch is the name of the remote branch.

Caseate answered 16/3, 2012 at 3:21 Comment(5)
"Adding taunts to management" gives rise to the old (new?) adage, "don't drink and push". :-)Conspire
@Conspire What can I say, I'm both a committed drinker and drinking committer.Caseate
Following worked for me git reset --hard da8af4d git push origin -f localBranch:remoteBranchAindrea
Worth noting that git reset --hard will undo any local modifications in the working tree, including those that were not part of the troublesome commit(s). If in doubt, it's certainly advisable to take a backup of that working tree first (a quick tar will do nicely).Kenny
I was having a hard time with the git push origin -f localBranch:remoteBranch because I was literally typing main:remote, but it didn't work, you have to write git push origin -f main:main so as to overwrite the main branch on github with my main branch. Sounds obvious now but it took me a while to figure this out.Brilliant
F
7

I think you can rollback locally and push the result:

$ git reset HEAD^ --hard
$ git push REMOTE -f

Where 'REMOTE' is the remote name.

Fanciful answered 16/3, 2012 at 3:16 Comment(3)
You can do that, but it's inadvisable since anyone else who grabbed the commits in the meantime will get ... well, to avoid bad words, let's say "they'll have to work hard to recover from your actions." :-) It's better to use "git revert" to add a new commit that undoes a previous commit. (Think of a "revert" as "add a commit whose patch is the action of exactly undoing a previous commit," since that's what it is.)Conspire
@Conspire I was thinking of this at first, but I think shadowfax might have other purposes, such as keeping every remote commit capable of running correctly, otherwise he would have thought of 'revert and push'.Fanciful
I think you might be surprised at how many people don't understand git revert. On the other hand, there could be a completely different motive (as in David M. Syzdek's answer). In some cases there are no good solutions, only the least-bad one (which varies per situation).Conspire
W
1

First of all type this command.

git log -n 4

This command will display your last 4 commits with their SHA. After this type the following command.

git rebase -i SHA_ID

Instead of SHA_ID type the SHA of the commit prior to the commit which you want to delete.

After this a file will open, on the top of that file you will see the SHA and the message of the commit which you want to delete. Delete this line and save and exit out of the file.

When you have done this you have to make a push. So type the following command.

git push -f origin master

After you are done with this you can see that your commit is deleted.

Weisbart answered 20/2, 2017 at 3:34 Comment(0)
P
1
git push origin -f last_good_commit:yourEditingBranch

The last_good_commit will be the new Head of the branch. Then locally you do a hard reset:

git reset origin/yourEditingBranch
Parsley answered 6/1, 2022 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.