Consider this scenario:
and then discovers that in his commit #n+2 he introduced a defect.
How can dev. A rollback his last 2 commits and continue developing on commit #n+1?
Tried git reset --hard HEAD~2
*, but it's coming back to dev A's commit #n.
In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”).
The easy way to revert a group of commits on shared repository (that people use and you want to preserve the history) is to use git revert in conjunction with git rev-list . The latter one will provide you with a list of commits, the former will do the revert itself.
To roll back to a previous commit w/o throwing away your work, use --soft. Unless you want it to remove all the changes up to that point, in that case use --hard instead of --soft, it would get you to the desired point in the tree WITHOUT trowing away all of the changes made in the commits.
14 Use git revert: git revert A^..B where Ais hash of the first of the two commits to be reverted and Bis the hash of the second commit. This approach will work even if other commits have been made on the remote branch since the two commits were made.
There are different ways to rollback in Git. Here, we will use the git revert command. Revert doesn't mean undoing the changes. I described the scenario above for ease of understanding that you rollback to the previous version.
Every commit made is the latest version of the changes made to the files in that repo. This means the previous commit contains some changes, and the latest commits contain some new changes but are not limited to a feature update, bug fixes, etc. Say you are working on a particular feature improvement of a website.
It should come back to the n+1 commit. You probably have a merge commit in there as well. You can also do a git reset --hard <sha1_of_where_you_want_to_be>
WARNING!!
--hard
means that any uncommitted changes you currently have will be thrown away permanently.
I would suggest using instead something like this:
git reset --soft <commit_hash>
Unless you want it to remove all the changes up to that point, in that case use --hard instead of --soft, it would get you to the desired point in the tree WITHOUT trowing away all of the changes made in the commits.
While I was reading I tried with --HARD and for my misfortune, it removed all my changes up to that point. So, pay attention to whether you want them removed or not.
so, if you are unlucky as me try this! : git revert <commit_hash>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With