问题
Is it possible to reset a remote branch to another remote branch?
Let's say I have two branches: master
and develop
. The develop
branch is made from master
and used as an integration/build branch. It serves no other purpose.
At the end of a sprint all feature branches, which are approved, are merged into master.
At the end of the sprint, everyone is required to delete his local copy of the develop branch, we delete the remote one, and a new one is made from master. This is not ideal, but the strategy we use itself is the one we agreed upon and bests suits or needs. It's loosely based on the branch-per-feature strategy.
The problem is that, if someone doesn't delete his local copy, and performs an update, he gets the message that his local develop branch is ahead of the remote one. This is because feature branches which were merged in from the previous sprint but did not make it into master are still on his local copy. The build infrastructure itself is also at risk, because if we don't perform a fully clean checkout we risk old stuff still being present.
Actions like reset --hard
don't work, they might reset my local copy, but I'm not able to push these through to the remote branch.
Is there a way to make the develop branch completely identical to the master branch using commits, rather than recreating it?
回答1:
So, your branches are master
and develop
. You want to reset develop
back to master
:
git checkout develop
git reset --hard master
git push -f <remote> develop
Or, if you've already deleted the local branch develop
, you can do it in one step:
git push -f <remote> master:develop
This pushes the local branch master
, but names it develop
on the remote.
The -f
or --force
flag means "Yes, Git, I'm trying to delete data. I know what I'm doing." In this case, pushing develop
would lose the remote commits, but you use -f
to tell Git that you want to lose those commits. Never use -f
unless you know why.
回答2:
Update your local version to the commit you want and then force push it to your remote repository:
git fetch origin
git branch -f develop origin/master
git push origin develop --force
来源:https://stackoverflow.com/questions/28671124/how-can-i-reset-a-diverged-branch-back-to-master