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?
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.
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