2

I meant to commit my changes to a local "feature" branch and push them to the corresponding remote "feature" branch. But instead, I accidentally committed my changes to the local "master" branch and pushed my changes to the remote "master" branch.

How do I both undo the changes pushed to "master" (local and remote), and also push those same changes to the intended "feature" branch (local and remote)?

5
  • I understand this is likely to be a duplicate, but before quickly marking it as such, please consider if the existing answer achieves both goals of the question. I've seen answers covering the first goal, but not the 2nd. Commented Sep 19, 2018 at 23:20
  • By the word "push" do you mean "commit"? Have you added your changes to the wrong branch? Or you have committed changes to the feature branch but pushed the branch to remote repo with git push origin feature:master?
    – phd
    Commented Sep 20, 2018 at 14:29
  • @phd, I meant push. I will edit my question to be a little more precise and also clarify eftshifto's question. Commented Sep 20, 2018 at 15:16
  • stackoverflow.com/questions/51089630/…
    – phd
    Commented Sep 20, 2018 at 15:26
  • Link appreciated. A meta-suggestion: I think it would be great to see all the steps combined in one accepted answer. I tried to write the question in terms of a common mistake applicable to more people than myself. Since I haven't seen the question worded this way in my searches, maybe there is some community value to it. Commented Sep 20, 2018 at 15:39

2 Answers 2

2

Sounds like you committed some things on top of the wrong branch? Ok..... it's not that difficult.

Checkout local feature branch. Cherry-pick the changes that you committed on top of master. Say, it's the last 2 commits then git cherry-pick master~2..master. Then push your local feature to remote feature. Then redirect master pointer to the right position git branch -f master master~2. Then push to master with the right position: git push -f whatever-remote master. That should do.

Original Response to Original Question

I guess you have a local branch for this problem, right? The branch that you pushed into master by mistake. Let's call it branch-a

git push whatever-remote branch-a # if you want the remote branch to be called branch-a as well git push whatever correct-revision-id:master # revert master to where it was

That should suffice.

4
  • My apologies--the original question wasn't clear on this point, and I've edited it now. The local branch with the mistake is also "master" and it's necessary for me to undo changes on that branch as well the remote. Commented Sep 20, 2018 at 15:25
  • Corrected my response to new description.
    – eftshift0
    Commented Sep 20, 2018 at 15:40
  • I think the one missing part to the answer is resetting the local master branch as William Pursell suggested. If you agree or have another instruction for making the local master branch match the git push -f, then I'll mark the answer as accepted. Much appreciation for the help. Commented Sep 20, 2018 at 16:22
  • 1
    The branch pointer is set to the right position when you do git branch -f master master~2 (as stated on my recipe). There you are asking git to place the master pointer two revisions back (where it should have been) and then you push it.
    – eftshift0
    Commented Sep 20, 2018 at 16:31
1

You need to force push to the older reference:

git push -f origin last_commit:branch_name

Then you can push your changes to the feature branch. Make sure no other user trying to build on top of commit you want to remove.

3
  • If I understand right, that will set the remote back to the commit I want. But I also need to revert my local "master" branch (I'm sorry--I just added this late clarification to question). After that, the unclear part to me is how I can easily get the changes that I reverted into a local "feature" branch separate from "master". Commented Sep 20, 2018 at 15:30
  • 1
    To resolve the local issue, just do git branch new-branch to create the new local branch at the current state, then reset master with git reset Commented Sep 20, 2018 at 15:39
  • 1
    To resolve local issue you can create a new local branch sitting on the master branch git branch <new-branch-name> and to discard all the local commit in master, checkout to the master and run git reset --hard @{u} this will make your local master identical to the remote master. I think this will probably solve your problem. Commented Sep 21, 2018 at 4:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.