git - 维护一个与上游同步的分支

标签 git

我打算根据 github 上的项目添加一些代码,例如添加一些适合我的项目的定制选项。

一个想法是 fork 并创建一个分支以包含我的所有更改。每当上游有新的更改时,我都会获取它们并将其 rebase 到我的分支。

假设这些是我的 Remote :

$ git remote -v
my   git@github.com:khing/myproject.git (fetch)
my   git@github.com:khing/pyproject.git (push)
up   https://github.com/upstream/project.git (fetch)
up   https://github.com/upstream/project.git (push)

上游有三个提交:A、B、C,我有两个额外的提交:M、N

所以我猜当前的分支会是这样的:

up: A--B--C
my: A--B--C--M--N

假设上游有两个新的提交 D 和 E,我应该获取和 rebase (是的,我可能必须修复 merge 冲突),所以我猜它会是:

up: A--B--C--D--E
my: A--B--C--D--E--M--N

这是维护我自己的分支同时与上游分支保持同步的好方法吗?

最佳答案

是的。这就是我在“Pull new updates from original GitHub repository into forked GitHub repository”中描述的内容。

自该答案(2010 年)以来,您可以:

  • 配置为始终从原始上游仓库 pull

    git remote set-url origin https://github.com/upstream/project.git
    git remote set-url --push origin git@github.com:khing/myproject.git 
    
  • 确保始终在获取的内容之上重新设置本地提交的基数:
    (需要 Git 2.9+,2016 年 6 月,参见“Can “git pull” automatically stash and pop pending changes?”)

    git config pull.rebase true
    git config rebase.autoStash true
    

那么一个简单的 git pull 就足够了。

关于git - 维护一个与上游同步的分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39483034/

相关文章:

git 克隆通过 ssh 错误 'fatal: The remote end hung up unexpectedly'

php - 如何从 GitHub 推送到你的 VPS

git - 按提交/merge 日期排序 git diff --name-only list

Git Subtree Merging 在 merge 一个简单的上游更改时报告冲突

具有文件名支持的 Git bash-completion?

ruby-on-rails - Capistrano 发布未发布

git - 如何删除 Github 和 Bitbucket 上的 git 提交

git grep——但仅限于索引中的新文件或修改过的文件

objective-c - xcworkspace 应该在 gitignore 中吗

git - MacOS:使用 Bitbucket 和 Github、SSH key ?