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.
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:
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
.
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
.
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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.