我分叉了一个项目,进行了更改,并创建了一个被接受的拉取请求。新的提交后来被添加到存储库中。我怎样才能把这些承诺交给我?


当前回答

在分叉存储库的本地克隆中,可以将原始GitHub存储库添加为“远程”。(“Remotes”就像是存储库URL的昵称,例如,origin就是其中之一。)然后,您可以从上游存储库中获取所有分支,并重新设置工作基础,继续使用上游版本。就命令而言,可能如下所示:

# Add the remote, call it "upstream":

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

# Fetch all the branches of that remote into remote-tracking branches

git fetch upstream

# Make sure that you're on your master branch:

git checkout master

# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:

git rebase upstream/master

如果您不想重写主分支的历史记录(例如,因为其他人可能已经克隆了它),那么应该用gitmergeupstream/master替换最后一个命令。然而,为了做出尽可能干净的进一步拉取请求,可能最好重新设置基址。


如果您已经将分支重新基于upstream/master,则可能需要强制推送,以便将其推送到GitHub上自己的分叉存储库。你可以这样做:

git push -f origin master

您只需要在重新启动后第一次使用-f即可。

其他回答

前言:您的分叉是“源”,您分叉的存储库是“上游”。

假设您已经使用如下命令将分叉克隆到计算机:

git clone git@github.com:your_name/project_name.git
cd project_name

如果已给出,则需要按以下顺序继续:

将“上游”添加到克隆的存储库(“源”):git远程添加上游git@github.com:original_author/project_name.git从“上游”获取提交(和分支):git获取上游切换到分叉的“master”分支(“origin”):切换到主分支停止“主”分支的更改:暂存将“上游”的“主”分支中的更改合并到“源”的“母”分支中:git合并上游/主解决合并冲突(如果有)并提交合并gitcommit-am“从上游合并”将更改推到叉上数字推送找回你藏起来的零钱(如果有的话)吉特藏弹你完了!祝贺

GitHub还提供了有关此主题的说明:同步分叉

在分叉存储库的本地克隆中,可以将原始GitHub存储库添加为“远程”。(“Remotes”就像是存储库URL的昵称,例如,origin就是其中之一。)然后,您可以从上游存储库中获取所有分支,并重新设置工作基础,继续使用上游版本。就命令而言,可能如下所示:

# Add the remote, call it "upstream":

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

# Fetch all the branches of that remote into remote-tracking branches

git fetch upstream

# Make sure that you're on your master branch:

git checkout master

# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:

git rebase upstream/master

如果您不想重写主分支的历史记录(例如,因为其他人可能已经克隆了它),那么应该用gitmergeupstream/master替换最后一个命令。然而,为了做出尽可能干净的进一步拉取请求,可能最好重新设置基址。


如果您已经将分支重新基于upstream/master,则可能需要强制推送,以便将其推送到GitHub上自己的分叉存储库。你可以这样做:

git push -f origin master

您只需要在重新启动后第一次使用-f即可。

许多答案最终会将fork移到父存储库之前一次提交。这个答案总结了这里找到的步骤,这些步骤会将您的fork移动到与父级相同的提交。

将目录更改为本地存储库。如果您不是git结账主管,请切换到master分支将父级添加为远程存储库,git remote Add upstream<repo location>向上游发出git fetch发布git上游/主数据库在这个阶段,您可以通过键入gitstatus来检查提交将要合并的内容发布git推送源主机

有关这些命令的更多信息,请参阅步骤3。

克隆分叉存储库后,转到克隆所在的目录路径和GitBash终端中的几行。

$ cd project-name

$ git remote add upstream https://github.com/user-name/project-name.git
 # Adding the upstream -> the main repo with which you wanna sync

$ git remote -v # you will see the upstream here 

$ git checkout master # see if you are already on master branch

$ git fetch upstream

你很好去那里。主存储库中所有更新的更改都将被推送到您的fork存储库中。

“fetch”命令对于保持项目中的最新状态是必不可少的:只有在执行“gitfetch”时,您才会被告知同事对远程服务器所做的更改。

您仍然可以访问此处进行进一步查询

$ git remote add upstream https://github.com/....

$ git pull upstream main

$ git push