0

I have done something 20 commits before which are now breaking my code because of incompatibility issue and now I want to revert back it to that commit. I am stuck in

reset and revert

command. Can anybody please tell me how I can revert back it to the old commit. I have pushed all my changes to master branch as I was working only on one branch.

eis
  • 51,991
  • 13
  • 150
  • 199
Ashish Jambhulkar
  • 1,374
  • 2
  • 14
  • 26
  • https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/ – EncryptedWatermelon Oct 29 '19 at 20:08
  • 1
    Possible duplicate of [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – phd Oct 29 '19 at 21:43
  • https://stackoverflow.com/search?q=%5Bgit-reset%5D+many+commits – phd Oct 29 '19 at 21:43

4 Answers4

1

If this is your personal project, i.e. no one else has pulled from master in the meantime, I would suggest either resetting or rebasing instead of reverting (which, as a consequence, creates new commits).

If you don't care about the stuff introduced by those 20 commits and you just want to get rid of those (that is how I understood your question), then I'd go with

git reset --hard HEAD~21

and then git push -f origin master.

If you do care about 19 out of 20 of those commits and you are sure that the changes they introduce are completely unrelated to that one "which breaks everything", I would go for

git rebase --interactive HEAD~21

which will list all the 20 commits. Change pick to drop in front of the "guilty" commit and save. This will delete that commit from the history. Take care of merge conflicts (if any) and continue with the rebase. Once everything is done, you will need to force push as this also changes the branch history.

mimikrija
  • 113
  • 7
0

Simply git revert HEAD~3 where 3 is the number of commits to revert

vale
  • 1,376
  • 11
  • 25
0

similar to check this and check this too. you can checkout to an older commit by checking your commit messages. ex:

$ git hist
* fa3c141 2011-03-09 | Added HTML header (HEAD, master) [Alexander Shvets]
* 8c32287 2011-03-09 | Added standard HTML page tags [Alexander Shvets]
* 43628f7 2011-03-09 | Added h1 tag [Alexander Shvets]
* 911e8c9 2011-03-09 | First Commit [Alexander Shvets]

and checkout

git checkout <hash>

or

git revert <commit hash>

before pushing, you need to resolve conflicts. for that you need to revert or checkout to your specific commit and "git pull origin master" will sync your local repository with the remote branch. and it will create a set of conflicts that you need to manually resolve. then only you can push the changes as it is now sync. otherwise, the master will always ahead from the old commit you have locally.

Viroj Fernando
  • 461
  • 4
  • 8
  • but checking out will not allow me to push the changes to the origin. It says Updates were rejected because a pushed branch tip is behind its remote – Ashish Jambhulkar Oct 29 '19 at 20:15
  • If you are the only one working on that project (no one else has pulled from master) then you can do a force push. `git push -f origin master` – mimikrija Oct 30 '19 at 10:18
-1
git revert HEAD~20

will revert last 20 commits, one by one.

However, you'll likely want just one revert commit, so do

git revert --no-commit HEAD~20..

To get one revert commit that will then revert 20 last commits, and

git commit

To submit said revert commit. That you can then push.

eis
  • 51,991
  • 13
  • 150
  • 199