我分叉了一个项目,进行了更改,并创建了一个被接受的拉取请求。新的提交后来被添加到存储库中。我怎样才能把这些承诺交给我?
当前回答
有三种方法可以做到这一点:从web UI(选项1)、从GitHub CLI(选项2)或从命令行(选项3)。
选项1-Web UI
在GitHub上,导航到要与上游存储库同步的分叉存储库的主页面。选择“获取上游”下拉列表。
查看上游存储库中有关提交的详细信息,然后单击“获取并合并”。
选项2-GitHub CLI
要从父级更新远程分叉,请使用gh repo sync子命令并提供分叉名称作为参数。
$ gh repo sync owner/cli-fork
如果来自上游存储库的更改导致冲突,则GitHub CLI无法同步。您可以设置-force标志来覆盖目标分支。
如何安装GitHub CLIGitHub CLI手册
选项3-命令行
在将分叉与上游存储库同步之前,必须在Git中配置指向上游存储库的远程。
1打开Git Bash。
2将当前工作目录更改为本地项目。
3从上游存储库获取分支及其各自的提交。对BRANCHNAME的提交将存储在本地分支上游/BRANCHNAME中。
$ git fetch upstream
> remote: Counting objects: 75, done.
> remote: Compressing objects: 100% (53/53), done.
> remote: Total 62 (delta 27), reused 44 (delta 9)
> Unpacking objects: 100% (62/62), done.
> From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
> * [new branch] main -> upstream/main
4查看fork的本地默认分支-在本例中,我们使用main。
$ git checkout main
> Switched to branch 'main'
5将来自上游默认分支(在本例中为upstream/main)的更改合并到本地默认分支中。这将使fork的默认分支与上游存储库同步,而不会丢失本地更改。
$ git merge upstream/main
> Updating a422352..5fdff0f
> Fast-forward
> README | 9 -------
> README.md | 7 ++++++
> 2 files changed, 7 insertions(+), 9 deletions(-)
> delete mode 100644 README
> create mode 100644 README.md
如果一个人的本地分支没有任何唯一的提交,Git将执行“快进”:
$ git merge upstream/main
> Updating 34e91da..16c56ad
> Fast-forward
> README.md | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
注意:同步一个人的分叉只会更新回购的本地副本。要更新GitHub.com上的分叉,必须推送更改。
来源:GitHub Docs-同步分叉
其他回答
在分叉存储库的本地克隆中,可以将原始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 remote -v
git pull
git fetch upstream
git checkout master
git merge upstream/master --no-ff
git add .
git commit -m"Sync with upstream repository."
git push -v
假设你的叉子是https://github.com/me/foobar原始存储库是https://github.com/someone/foobar
参观https://github.com/me/foobar/compare/master...someone:master如果您看到绿色文本“能够合并”,请按Create pull request在下一页上,滚动到页面底部,然后单击合并请求和确认合并。
使用以下代码段生成链接以同步分叉存储库:
新Vue({el:“#app”,数据:{yourFork:'https://github.com/me/foobar',原始回购:'https://github.com/someone/foobar'},计算:{syncLink:函数(){const yourFork=新URL(this.yourFork).pathname.split('/')const originalRepo=新URL(this.originalRepo).pathname.split('/')如果(yourFork[1]&&yourFork[2]&&originalRepo[1]){返回`https://github.com/${yourFork[1]}/${yourFork[2]}/compare/master${originalRepo[1]}:主`}return“数据不足”}}})<script src=“https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js“></script><div id=“app”>您的分叉URL:<inputsize=50v-model=“yourFork”/><br/>原始存储库URL:<input v-model=“originalRepo”size=50/><br/>用于同步分叉的链接:<a:href=“syncLink”>{syncLink}</a></div>
克隆分叉存储库后,转到克隆所在的目录路径和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 -v
你应该有起点和上游。例如:
origin https://github.com/your___name/kredis.git (fetch)
origin https://github.com/your___name/kredis.git (push)
upstream https://github.com/rails/kredis.git (fetch)
upstream https://github.com/rails/kredis.git (push)
之后转到main:
git checkout main
并从上游合并到干管:
git merge upstream/main
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 在GitHub上有一个公共回购的私人分支?
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- 只用GitHub动作在特定分支上运行作业
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 是否有一个链接到GitHub下载文件的最新版本的存储库?