更新所有分支的分叉Github仓库

时间:2013-03-26 14:38:22

标签: git github git-push git-pull

我在我的Github帐户上分叉了一个存储库,该帐户有超过1000个提交和20个分支。

然后我将其克隆到我的本地机器上。

有没有什么方法可以更新我的本地机器的repo和我的Github的repo与原来的所有分支和提交?

1 个答案:

答案 0 :(得分:4)

也许为时已晚,但迟到的答案总比没有好:

# add the upstream:

git remote add upstream https://github.com/whoever/whatever.git

#create a script to loop through the whole branches and merge changes or create them if not found

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
}

# then call the script

sync_all_branch

#push changes to your remote repository

git push --all

如果你想在上游分支上重新分支你的分支(删除你所做的更改而不是合并到上游),你应该改变

git merge -s recursive -Xours upstream/$upstream_branch 

git rebase -s recursive -Xours upstream/$upstream_branch 

并将“-f”添加到最后一个命令

* sync_all_branch脚本来自https://stackoverflow.com/a/7766487/2481592