This question is different from other questions in that, I want to keep some commits while I revert some commits.
Here 's my setup.
Upstream repo on github (public repo which I don't own)
Personal repo on my server(git clone --bare $upstream)
My workflow was like this:
Need to customize my personal branch
1. check out origin/master(Personal)
2. make changes
3. push to origin/master
Need to get new feature or bug fix from the original author(github)
1. git fetch upstream
2. git merge upstream/master
Now I find upstream/master has a bug and want to revert,
How do I go back to the state before I fetch/merged upstream for the last time?
Edit.
Suppose
- I merged upstream
- I merged what a team member pushed to origin/master(Personal)
Now I want to undo the step 1.
git reset --hard commit_sha_of_one_prior_the_merge_1
(Although finding the sha is not easy. git log shows a lot of commit sha's of upstream, so it's not easy to find commit-sha-of_one_prior_the_merge_1 which is not of
upstream
but oforigin/master
)
How do I keep the step 2 merge?
Suppose a slightly more complex scenario
- I merged upstream
- I merged what another team member pushed to Personal
- I also pushed my work to Personal
Now I want to undo the upstream merge. How can I do it?
You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.
There is no command to explicitly undo the git pull command. The alternative is to use git reset, which reverts a repository back to a previous commit.
In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z afterwards and Tower will undo the merge for you!
Since you have an actual merge commit--because you've been introducing your own changes--you should be able to do:
git reset --hard HEAD~1
Assuming your last commit was the merge commit in question.
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