Updating multiple branches of a forked repository on Github

老子叫甜甜 提交于 2019-12-10 18:43:45

问题


I have a forked github repository (call it repo-O and call my fork repo-F) which contains about 8 branches. Several (100s) commits have been made to repo-O from other contributors, and on multiple branches of repo-O. I would now like to pull these changes into my forked repo (repo-F). I cant use the fork queue as there are about 3000 commits to pick through, and I would much rather do this from the command line.

So.. I have cloned my repo, added repo-O as a remote (upstream) and fetched the changes, followed by merge origin/upstream... then a push back to repo-F.

This appears to have applied the changes to the master branch, but not to any other branch....

How can I repeat the above process so that 'all' the branches are updated?

Thanks


回答1:


What you want to accomplish is "for each remote branch on repo-o, create the same branch pointing to repo-o's branch if I don't have it or pull information on the local branch from whatever is on the same branch on repo-o, and at the end push all these branches to my repo-f".

Assuming your remotes are really named repo-o and repo-f, I'd play with something like, in bash:

for repo_o_branch in \
    $(git branch -a|grep repo-o|perl -nle's,^\s*repo\-o/,,;print $_';
do
    (                                                              \
       ( git checkout $repo_o_branch                               \
         && git pull --rebase repo-o $repo_o_branch)               \
       || ( git checkout -b $repo_o_branch repo-o/$repo_o_branch ) \
    ) && git push repo-f $repo_o_branch;
done

For all "repo o branches" (shown by git branch -a as " repo-o/branchname", without the "spaces and 'repo-o/'" part of it),

  • try checking out the branch and doing a git pull --rebase repo-o branchname inside it,
  • or, if the git checkout fails (as you do not have that branch): checkout a new branch named after the repo-o's branch name and point it to repo-o's branchname.
  • If any of the two above succeed, push the newly created or updated branch name to your repo-f.

Season to taste; best tried on a newly created git clone of repo-f with the remote repo-o added to it, just in case things go wrong ;)




回答2:


awk version with some merge treaks

sync_all_branch () {
  git fetch upstream
  for upstream_branch in   $( git branch -a |awk 'BEGIN {FS="/"} $2=="upstream" {print $3}' ) ;
  do
      if git checkout $upstream_branch
      then
          echo merge $upstream_branch
          git merge -s recursive -Xours upstream/$upstream_branch 
      else
          echo create $upstream_branch
          git checkout -b $upstream_branch upstream/$upstream_branch
      fi
  done
  git checkout master
  git push --all
}


来源:https://stackoverflow.com/questions/4333553/updating-multiple-branches-of-a-forked-repository-on-github

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!