Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating multiple branches of a forked repository on Github

Tags:

git

github

fork

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

like image 200
mrwooster Avatar asked Sep 12 '25 05:09

mrwooster


2 Answers

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 ;)

like image 114
mfontani Avatar answered Sep 14 '25 18:09

mfontani


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
}
like image 25
tomyjwu Avatar answered Sep 14 '25 18:09

tomyjwu