Cant git push to master after revert
Asked Answered
C

1

7

I pushed my changes to my remote master branch by mistake. So to keep them safe I created a backup branch. I then reverted the changes I did to remote master.

On my local master branch I ran:

git revert <commit_sha>

and then

git push

I have now finished working on the new branch (backup) and it all looks good. But I can't push the changes from my local backup branch to remote master. When I run git pull on my backup branch the changes I made are lost. The code is replaced with the contents of the remote master.

Is there a way for me to push my changes to the remote master branch without losing my work?

Cox answered 25/9, 2014 at 23:14 Comment(5)
I checked out a new branch and pushed them there. That doesn't make sense: you can check out a local branch, but you can't push to one. You need to clarify your question.Doublecheck
@Jubobs I think OP meant he created a "backup" branch and push that to his remote repo.Humanist
"I then reverted the push I did to master" also doesn't make much sense. Do you mean that you reverted the last commit in the local master, or that you force push to the remote master branch?Doublecheck
Sorry @Jubobs, I meant I created a backup branch and continued the work there as isim saidCox
Also, please clarify "all my changes are overwritten with master".Doublecheck
H
7

UPDATE

Since you have already performed a revert on your local master branch and push it to your remote, you should just make all your new changes on your local master branch and forget about the backup branch. Keep in mind your backup branch still contains those erroneous codes from before. If you pushed it to your remote, you will introduce those codes you reverted out back.

Original Answer

If you are the only person working on that remote repository, then you should be able to use

git push -f <remote> <branch>

to force push your new branch to your remote master branch.

To help out with the terminology a bit, every git repository has a local master branch. Hence, saying I pushed my changes to master.. doesn't make sense. We either push to remote (which by default is labeled as origin) or merge with (local) master.

Humanist answered 25/9, 2014 at 23:26 Comment(5)
I'm not the only person working on the remote master branch, is there a safe way to do it?Cox
@user3393708 If you're not the only person working on the remote branches, you should never rewrite history to begin with (by using git revert), unless you are absolutely 200% sure that no one has done a git pull since you pushed your unintended changes.Humanist
no one did git pull since I pushed the unintended changesCox
@user3393708 Actually, I was mistaken between reset and revert. If you used revert, then you won't have to worry about anyone has done a pull.Humanist
Since you have already reverted your mistakes on your local master branch, why can't you make your new changes there and forget about your backup branch?Humanist

© 2022 - 2024 — McMap. All rights reserved.