-1

I have one bad pushed commit.
How to undo this commit, and back to my working tree (was before)?

This will lead me to right working tree:

git checkout HEAD~1

This will lead me to right commit, but working tree remains the same unnecessary files:

git revert HEAD~1

How to completely rollback to previous commit?

I mean, I shouldn't commit on detached HEAD, right? (in case of git checkout <commit>)

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
kAldown
  • 610
  • 2
  • 8
  • 27
  • You say you *pushed* the bad commit, do you want to only roll back the change locally or on the remote repo too? Both are different. Depending on your remote repo permissions you may not be able to delete the remote repo's bad commit, you may only be able to make a commit that undoes the change. – Captain Man Feb 19 '16 at 21:32

2 Answers2

1

According to this answer, git revert HEAD is what you should use ; however, if the reverted commit added any files or folders, they will be left in your working copy as untracked files. To get rid of them, run git clean (as explained in this answer).

UPDATE: I edited the required command to simply git revert HEAD after testing this locally. There should be no need to run git clean. As the linked answer explains, git revert commits a 'reverse merge' of the commit passed to it, so—if HEAD~1 is the end result you want, you should pass HEAD to git revert.

Community
  • 1
  • 1
evadeflow
  • 4,704
  • 38
  • 51
1

If you want to completely rollback to the previous commit and remove the bad commit from history.

git reset --hard HEAD~1

Charles Durham
  • 2,445
  • 16
  • 17
  • The OP said he already _pushed_ the bad commit, so doing a reset will lead to problems when he tries to push the corrected commit later. I prefer `git reset` myself for bad commits I haven't already pushed, but... seems like `git revert` is the better solution in this particular case. – evadeflow Feb 19 '16 at 21:53
  • You can just force push onto your remote branch. – Charles Durham Feb 19 '16 at 22:58
  • Sure, but if somebody has pulled the 'bad' commit in the meantime, and pushed _another_ commit on top of it, won't that overwrite the other developer's commit? I suppose this isn't such a problem in workflows that are _strictly_ pull request-based, but an awful lot of people are still using a central repo with shared branches that multiple people can push to. – evadeflow Feb 19 '16 at 23:30
  • 1
    I suppose that scenario is possible. I would argue, like you touched on, that no two developers should be pushing to the same branch. – Charles Durham Feb 19 '16 at 23:56