2

我在我的 Github 帐户上创建了一个存储库,其中包含 1000 多个提交和 20 个分支。

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

有什么办法可以用原始的所有分支和提交更新我的本地机器的存储库和我的 Github 的存储库?

4

1 回答 1

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

于 2014-02-02T09:16:02.083 回答