Git replace master branch with another branch
I am fairly new to Git, having only been using it for the last few months, since taking on a new client (it was what they had been using to manage version control of their software before I started working for them).
I recently merged a development
branch with my local master
branch, and pushed master
to the server. I had tested the site (website written in Django/ Python) on my local development machine prior to pushing it to the server, and it all appeared to be working correctly (including the bug fix that had been implemented on the development
branch).
However, after testing the changes that had been made after pushing to the server, I found that something had broken a part of the site. So I checked out an old commit
on the server, and the live version is now using that old commit
, meaning that it is in a 'Detached HEAD' state, but is functional.
So, my local master
branch is currently the version with the bug fix that I had pushed to the server (which is working locally, but didn't on the server), but since I need to work on another aspect of the site, I want to revert to a backup of master
that I had made prior to merging the development
branch with the bug fix into master
, so that I am developing from the 'most recent working' state of the code.
The branches I have on my machine are:
master
(with development
merged into it, which has broken something else)
masterBackup
(ie the master
branch as it was before merging development
into it)
development
(which is the branch on which the original bug has been fixed, but something else is now broken)
So effectively, masterBackup
is actually my working 'master' branch at the moment, since merging development
into master
broke master
. I want to make masterBackup
my 'master' branch again, and master
in its current state can be discarded/ removed. I still have the development
branch, so can look into why/ how this broke the site and fix it there before merging it to master
again.
I found the question at How to replace master branch in git, entirely, from another branch? on which the accepted answer indicates that I should do the following steps:
git checkout masterBackup
git merge -s ours master
git checkout master
git merge masterBackup
But I can't find anything definitive explaining what the command git merge -s ours
does... is this definitely how to make my masterBackup
branch the master
branch again?
The commands in that post is not suitable for you. The result of the commands is make the masterBackup branch contains all the changes in master branch. Of cause this is not what you want.
If you just want masterBackup branch work as master branch, you can rename it by
git branch -m master master1
git branch -m masterBackup master
Now the masterBackup is replace master branch. But if you want to push local master branch to remote master, you should use git push -f origin master
.
The safest option is to revert the commits which were made to your master
branch happening after the detached commit where everything is working properly:
git revert <SHA-1>^..HEAD # SHA-1 is the earliest commit you want gone
This option will make a commit on top of master
which undoes each of the commits in the range. After this, your branch should be functionally identical to where you want it. You can continue working as usual and push as usual.
But you could also rewrite the history of master
if you wanted to.
Warning: This option will rewrite the history of your published master
branch and therefore should only be considered if you can handle the caveats that come with this.
If you want to permanently revert your master
branch to an earlier commit you can do the following steps:
git checkout master
git reset --hard <SHA-1 of earlier commit you want to keep>
This will overwrite your local master
branch such that its HEAD is now the earlier functioning commit.
Note that this will change the history of the remote published master
branch, so in order to update the remote, you will have to do a force push via:
git push --force origin master
链接地址: http://www.djcxy.com/p/94816.html
下一篇: Git将主分支替换为另一个分支