-1

I am new to git, there is a master branch in my repo and I sent a pull request from branch A. Now by mistake I made a pull request to master and completed it by mistake. I have local backup (outside git backup) of branch A. Can I undo the master branch to the point it was just before the pull request completion?

I am using azure repo.

M. M. Farhad
  • 39
  • 1
  • 7
  • 1
    What do you mean by "completion"? Do you mean that the branch from your PR was merged into `master`? It is fairly trivial to reset the master branch to the prior (or any) commit. `git reset` (on the remote) will do what you want, or `git push -f` (on the local). – William Pursell Jun 10 '21 at 21:23
  • Yes, I merged everything to master. Just need to go back to the version of master just before the pull request completion (or merge). Thanks for the info – M. M. Farhad Jun 10 '21 at 21:25
  • You could try using a revert commit that basically undoes the changes (in a new commit) or you could overwrite/remove the merge but you shouldn't do that if multiple people work on the repository. – dan1st Jun 10 '21 at 21:31
  • Does this answer your question? [Resetting remote to a certain commit](https://stackoverflow.com/questions/5816688/resetting-remote-to-a-certain-commit) – William Pursell Jun 10 '21 at 21:34
  • You may also want to look at https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit – William Pursell Jun 10 '21 at 21:35

2 Answers2

2

There isn't much you can do at this point. Do not attempt to rewrite history (e.g. with reset or similar).

If the problem is merely that the pull request was merged before it was approved, then one very good option is to do nothing. Just keep working on the pull request. Either it will be approved as it is, in which case you're fine, or it will need further work, in which case just branch off the end of the pull request and submit that branch as another pull request. When it is merged into master, it will correct whatever is wrong with the first merge.

If the problem is that you don't want anyone to pull the "bad" merge you've already made, then you will have to hotfix master with a new commit that undoes the effect of the merge. Pull master to your machine. Note down the SHA number of the merge commit. Now say

git revert -m 1 <SHA>

and push that back up to master.

Now throw away the pull request branch and start over, as you have just said you will never want the changes it contributed to master.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

There are different ways to solve this. It also partly depends on how your repository is set up.

If you want to get back to the state, in which the PR was not merged having two branches, you could reset the HEAD on master by one commit and force push the previous commit. This will rewrite history and may not be allowed on master. Also, it bears a risk to rewrite the git history. If you want to do this, you should know what you do.

Another option would be to just revert the merge commit. This will not undo the merge in the history, but the changed that were applied within the merge.

Now, after explaining these options, here are the commands required to perform those actions.

Delete merge commit and rewrite git history

git checkout master
git reset HEAD~1
git push -f

Revert changes applied in the merge commit

I would recommend trying this.

git checkout master
git revert HEAD

Both of these options assume that the merge commit on master is the last commit and no commit was done after it.

schilli
  • 1,700
  • 1
  • 9
  • 17