简体   繁体   English

如何将本地 master 合并到所有本地分支?

[英]How can I merge local master into all local branches?

I have several local branches, and after sometime, I merged all these branches into master, and then I want to merge local master into all local branches.我有几个本地分支,过了一段时间,我将所有这些分支合并到master,然后我想将本地master合并到所有本地分支。

How can I do that?我怎样才能做到这一点?

There is no one single command in git, but the way to do something "for all the branches" is, in a bash session: git 中没有一个命令,但是“为所有分支”做某事的方法是在 bash 会话中:

for BRANCH in `ls .git/refs/heads`; do something $BRANCH; done

That could be used for merging:这可用于合并:

for BRANCH in `ls .git/refs/heads`; do if [[ "$BRANCH" != "master" ]] ; then git checkout $BRANCH ; git merge master ; fi ; done

Or for resetting the branch, as suggested in William 's answer :或者重置分支,如威廉回答中所建议的:

for BRANCH in `ls .git/refs/heads`; do if [[ "$BRANCH" != "master" ]] ; then git checkout $BRANCH ; git reset --hard master ; fi ; done

You can delete the local branches and recreate them.您可以删除本地分支并重新创建它们。 With this, the new branches will be a mirror of the master branch.这样,新分支将成为主分支的镜像。 You can obtain the same result with git checkout branch-name && git reset --hard master .您可以使用git checkout branch-name && git reset --hard master获得相同的结果。

Remember that this can delete some commits.请记住,这可以删除一些提交。 If you don't want that (maybe because these local branches have a remote branch), you will need to do a git merge : git checkout branch-name && git merge master .如果你不想这样(也许是因为这些本地分支有一个远程分支),你需要做一个git mergegit checkout branch-name && git merge master

I used VonC's answer from here, but then for some reason ls .git/refs/heads stopped resulting all branch names - only two ones were in this folder.我从这里使用了 VonC 的答案,但是由于某种原因ls .git/refs/heads停止生成所有分支名称 - 只有两个分支在此文件夹中。 So this modified version worked for me after:所以这个修改后的版本对我有用:

for BRANCH in `git branch`; do if [[ "$BRANCH" != "master" ]] ; then git checkout $BRANCH ; git merge master --no-edit; fi ; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM