Given a change that has been committed using commit
, and then reverted using revert
, what is the best way to then undo that revert?
Ideally, this should be done with a new commit, so as to not re-write history.
You can only reset to a previous commit and ignore the local changes, or revert the changes.
Just revert the revert. So by clicking the revert button you will have created a new PR (your step 2). Once this is merged, you will have the option to revert this, which will create a new branch with all your changes back in. You can then pull this, make changes to it (if needed) and create a new PR.
If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit. To fix the detached head do git checkout <current branch> .
A revert commit is just like any other commit in git. Meaning, you can revert it, as in: git revert 648d7d808bc1bca6dbf72d93bf3da7c65a9bd746 That obviously only makes sense once the changes were pushed, and especially when you can't force push onto the destination branch (which is a good idea for your masterbranch).
The git revert command follows this principle: it introduces a new commit to the commit history whose sole purpose is to undo the changes of a targeted commit. Importantly, this means that the existing commit history prior to the newly added “undo” commit, including the original error commit, is preserved.
Other common gitrevisions are Git branch names followed by ~, followed by the number of commits behind the head commit your target commit is. In the above example, the target commit is 2 commits behind the head commit on the currently checked out branch.
When developers git revert a commit for the first time, they usually guess the command will do one of three things: The git revert will leave two files on the file system -- alpha.html and beta.html -- and will roll back to the state prior to the third commit.
git cherry-pick <original commit sha>
Will make a copy of the original commit, essentially re-applying the commit
Reverting the revert will do the same thing, with a messier commit message:git revert <commit sha of the revert>
Either of these ways will allow you to git push
without overwriting history, because it creates a new commit after the revert.
When typing the commit sha, you typically only need the first 5 or 6 characters:git cherry-pick 6bfabc
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