如何更新或同步 GitHub 上的分叉存储库?

发布于 2024-12-01 21:07:31 字数 68 浏览 3 评论 0 原文

我分叉了一个项目,进行了更改,并创建了一个已接受的拉取请求。新的提交后来被添加到存储库中。我如何将这些提交放入我的分叉中?

I forked a project, made changes, and created a pull request which was accepted. New commits were later added to the repository. How do I get those commits into my fork?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

橙味迷妹 2024-12-08 21:07:32

如果你设置你的上游。使用 git remote -v 检查,这样就足够了。

git fetch upstream
git checkout master
git merge --no-edit upstream/master
git push

If you set your upstream. Check with git remote -v, then this will suffice.

git fetch upstream
git checkout master
git merge --no-edit upstream/master
git push
╰◇生如夏花灿烂 2024-12-08 21:07:32

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

$ 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

现在就可以开始了。主存储库中的所有更新更改都将推送到您的分支存储库中。

“fetch”命令对于保持项目最新状态是必不可少的:只有在执行“git fetch”时,您才会获悉同事推送到远程服务器的更改。

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

When you have cloned your forked repository, go to the directory path where your clone resides and the few lines in your Git Bash Terminal.

$ 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

And there you are good to go. All updated changes in the main repository will be pushed into your fork repository.

The "fetch" command is indispensable for staying up-to-date in a project: only when performing a "git fetch" will you be informed about the changes your colleagues pushed to the remote server.

You can still visit here for further queries

苍风燃霜 2024-12-08 21:07:32

Android Studio 现在已经学会了使用 GitHub fork 存储库(您甚至不必通过控制台命令添加“上游”远程存储库)。

打开菜单 VCSGit

并注意最后两个弹出菜单项:

  • Rebase my GitHub fork

  • 创建拉取请求

尝试一下。我使用第一个来同步我的本地存储库。无论如何,在单击“Rebase my GitHub fork”后,您将可以在 Android Studio 中访问父远程存储库(“上游”)的分支,并且您将能够轻松地使用它们。

(我使用带有“Git 集成”和“GitHub”插件的 Android Studio 3.0。)

在此处输入图像描述

Android Studio now has learned to work with GitHub fork repositories (you don't even have to add "upstream" remote repository by console command).

Open menu VCSGit

And pay attention to the two last popup menu items:

  • Rebase my GitHub fork

  • Create Pull Request

Try them. I use the first one to synchronize my local repository. Anyway the branches from the parent remote repository ("upstream") will be accessible in Android Studio after you click "Rebase my GitHub fork", and you will be able to operate with them easily.

(I use Android Studio 3.0 with "Git integration" and "GitHub" plugins.)

Enter image description here

神爱温柔 2024-12-08 21:07:32

如何在本地计算机上更新分叉存储库?

首先,检查您的远程/主控

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

并从上游合并到 main:

git merge upstream/main

How to update your forked repo on your local machine?

First, check your remote/master

git remote -v

You should have origin and upstream. For example:

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)

After that go to main:

git checkout main

and merge from upstream to main:

git merge upstream/main
如若梦似彩虹 2024-12-08 21:07:32

假设您的分叉是 https://github.com/me/foobar 并且原始存储库是 https://github.com/someone/foobar

  1. 访问https://github.com/me/foobar/compare/master...某人:大师

  2. 如果您看到绿色文本能够合并,则按创建拉取请求

  3. 在下一页上,滚动到页面底部并单击合并拉取请求确认合并

使用此代码片段生成链接以同步您的分叉存储库:

new Vue ({
    el: "#app",
    data: {
      yourFork: 'https://github.com/me/foobar',
      originalRepo: 'https://github.com/someone/foobar'
    },
    computed: {
      syncLink: function () {
        const yourFork = new URL(this.yourFork).pathname.split('/')
        const originalRepo = new URL(this.originalRepo).pathname.split('/')
        if (yourFork[1] && yourFork[2] && originalRepo[1]) {
          return `https://github.com/${yourFork[1]}/${yourFork[2]}/compare/master...${originalRepo[1]}:master`
        }
        return 'Not enough data'
      }
    }
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  Your fork URL: <input size=50 v-model="yourFork" /> <br />
  Original repository URL: <input v-model="originalRepo" size=50 /> <br />
  Link to sync your fork: <a :href="syncLink">{{syncLink}}</a>
</div>

Assuming your fork is https://github.com/me/foobar and original repository is https://github.com/someone/foobar

  1. Visit https://github.com/me/foobar/compare/master...someone:master

  2. If you see green text Able to merge then press Create pull request

  3. On the next page, scroll to the bottom of the page and click Merge pull request and Confirm merge.

Use this code snippet to generate link to sync your forked repository:

new Vue ({
    el: "#app",
    data: {
      yourFork: 'https://github.com/me/foobar',
      originalRepo: 'https://github.com/someone/foobar'
    },
    computed: {
      syncLink: function () {
        const yourFork = new URL(this.yourFork).pathname.split('/')
        const originalRepo = new URL(this.originalRepo).pathname.split('/')
        if (yourFork[1] && yourFork[2] && originalRepo[1]) {
          return `https://github.com/${yourFork[1]}/${yourFork[2]}/compare/master...${originalRepo[1]}:master`
        }
        return 'Not enough data'
      }
    }
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  Your fork URL: <input size=50 v-model="yourFork" /> <br />
  Original repository URL: <input v-model="originalRepo" size=50 /> <br />
  Link to sync your fork: <a :href="syncLink">{{syncLink}}</a>
</div>

所谓喜欢 2024-12-08 21:07:32

我想添加到 @krlmlr 回答

最初,分叉存储库有一个名为:master 的分支。如果您正在开发新功能或修复,通常会创建一个新分支 feature 并进行更改。

如果您希望分叉存储库与父存储库同步,您可以为 pull.yml) /pull" rel="nofollow noreferrer">拉取应用程序在功能分支),如下所示:

version: "1"
rules:
  - base: feature
    upstream: master
    mergeMethod: merge
  - base: master
    upstream: parent_repo:master
    mergeMethod: hardreset

这会保持分叉存储库的 master 分支正常运行- 与父母约会回购。它通过合并分支存储库的 master 分支来保持分支存储库的 feature 分支的更新。这假设 feature 分支是包含配置文件的默认分支。

这里有两种mergemethods发挥作用,一种是hardreset,它有助于强制将分叉存储库的master分支中的更改与父存储库和另一种方法是merge。此方法用于合并您在 feature 分支中完成的更改以及由于 master 分支中强制同步而完成的更改。如果发生合并冲突,拉取应用程序将允许您在拉取请求期间选择下一步操作。

您可以在此处阅读有关基本和高级配置以及各种合并方法的信息

我目前正在我的分叉存储库此处中使用此配置,以确保请求增强< a href="https://stackoverflow.com/q/20700696/10155936">此处保持更新。

I would like to add on to @krlmlr's answer.

Initially, the forked repository has one branch named : master. If you are working on a new feature or a fix, you would generally create a new branch feature and make the changes.

If you want the forked repository to be in sync with the parent repository, you could set up a config file(pull.yml) for the Pull app (in the feature branch), like this:

version: "1"
rules:
  - base: feature
    upstream: master
    mergeMethod: merge
  - base: master
    upstream: parent_repo:master
    mergeMethod: hardreset

This keeps the master branch of the forked repo up-to-date with the parent repo. It keeps the feature branch of the forked repo updated via the master branch of the forked repo by merging the same. This assumes that the feature branch is the default branch which contains the config file.

Here two mergemethods are into play, one is hardreset which helps force sync changes in the master branch of the forked repo with the parent repo and the other method is merge. This method is used to merge changes done by you in the feature branch and changes done due to force sync in the master branch. In case of merge conflict, the pull app will allow you to choose the next course of action during the pull request.

You can read about basic and advanced configs and various mergemethods here.

I am currently using this configuration in my forked repo here to make sure an enhancement requested here stays updated.

时光沙漏 2024-12-08 21:07:32

这取决于您的存储库的大小以及您如何分叉它。

如果它是一个相当大的存储库,您可能希望以特殊的方式管理它(例如删除历史记录)。基本上,您可以获取当前版本和上游版本之间的差异,提交它们,然后挑选回主版本。

尝试阅读这个。它描述了如何处理大型 Git 存储库以及如何使用最新更改将其上游。

That depends on the size of your repository and how you forked it.

If it's quite a big repository you may have wanted to manage it in a special way (e.g. drop history). Basically, you can get differences between current and upstream versions, commit them and then cherry pick back to master.

Try reading this one. It describes how to handle big Git repositories and how to upstream them with latest changes.

凉世弥音 2024-12-08 21:07:32

如果你想让你的 GitHub 分支与相应的上游保持同步,还有专门针对 GitHub 的 Probot 程序: https://probot.github.io/apps/pull/ 完成这项工作。
您需要允许在您的帐户中安装,它将使您的分叉保持最新状态。

If you want to keep your GitHub forks up to date with the respective upstreams, there also exists this probot program for GitHub specifically: https://probot.github.io/apps/pull/ which does the job.
You would need to allow installation in your account and it will keep your forks up to date.

黎歌 2024-12-08 21:07:32

尝试一下,单击“获取上游”以从上游主服务器同步您的分叉存储库。
输入图片此处描述

Try this, Click on "Fetch upstream" to sync your forked repo from upstream master.
enter image description here

指尖凝香 2024-12-08 21:07:32

如果使用 GitHub Desktop,只需 6 步(实际上只有 5 步)即可轻松完成。

打开 Github Desktop 并选择存储库后,

  1. 转到“历史记录”选项卡
  2. 单击搜索栏。它将向您显示所有可用的分支(包括
    来自父存储库的上游分支)
  3. 选择相应的上游分支(它将是upstream/master 以同步master
    分支)
  4. (可选)它将显示上游分支中的所有提交。您可以点击
    任何提交以查看更改。
  5. 根据您的活动分支,在 master / branch-name 中单击“合并”。
  6. 等待 GitHub Desktop 施展魔法。

以下面的 GIF 为例:

从父存储库同步分叉存储库中的上游分支

If you use GitHub Desktop, you can do it easily in just 6 steps (actually only 5).

Once you open Github Desktop and choose your repository,

  1. Go to History tab
  2. Click on the search bar. It will show you all the available branches (including
    upstream branches from parent repository)
  3. Select the respective upstream branch (it will be upstream/master to sync master
    branch)
  4. (OPTIONAL) It will show you all the commits in the upstream branch. You can click on
    any commit to see the changes.
  5. Click Merge in master / branch-name, based on your active branch.
  6. Wait for GitHub Desktop to do the magic.

Checkout the GIF below as an example:

Sync Upstream branches in a forked repository from the parent repository

心作怪 2024-12-08 21:07:32

使用这些命令(幸运的话)

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

Use these commands (in lucky case)

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
红颜悴 2024-12-08 21:07:32

保持分叉存储库始终更新有两个主要因素。

<强>1。从 fork master 创建分支并在那里进行更改

因此,当您的拉取请求被接受时,您可以安全地删除该分支,因为当您使用上游更新它时,您贡献的代码将存在于分叉存储库的主版本中。这样你的master将始终处于干净的状态来创建一个新的分支来进行另一个更改。

<强>2。为 fork master 创建一个计划作业以自动更新

这可以通过 cron 来完成。如果您在 Linux 中执行此操作,这里是示例代码。

$ crontab -e

将此代码放在 crontab 文件 中以每小时执行一次作业。

0 * * * * sh ~/cron.sh

然后创建 cron.sh 脚本文件和 git 交互ssh-agent和/或预计如下

#!/bin/sh
WORKDIR=/path/to/your/dir   
REPOSITORY=<name of your repo>
MASTER="[email protected]:<username>/$REPOSITORY.git"   
[email protected]:<upstream>/<name of the repo>.git  

cd $WORKDIR && rm -rf $REPOSITORY
eval `ssh-agent` && expect ~/.ssh/agent && ssh-add -l
git clone $MASTER && cd $REPOSITORY && git checkout master
git remote add upstream $UPSTREAM && git fetch --prune upstream
if [ `git rev-list HEAD...upstream/master --count` -eq 0 ]
then
    echo "all the same, do nothing"
else
    echo "update exist, do rebase!"
    git reset --hard upstream/master
    git push origin master --force
fi
cd $WORKDIR && rm -rf $REPOSITORY
eval `ssh-agent -k`

检查您的分叉存储库。有时它总是会显示此通知:

该分支与 :master 相同。

在此处输入图像描述

There are two main things on keeping a forked repository always update for good.

1. Create the branches from the fork master and do changes there.

So when your Pull Request is accepted then you can safely delete the branch as your contributed code will be then live in your master of your forked repository when you update it with the upstream. By this your master will always be in clean condition to create a new branch to do another change.

2. Create a scheduled job for the fork master to do update automatically.

This can be done with cron. Here is for an example code if you do it in linux.

$ crontab -e

put this code on the crontab file to execute the job in hourly basis.

0 * * * * sh ~/cron.sh

then create the cron.sh script file and a git interaction with ssh-agent and/or expect as below

#!/bin/sh
WORKDIR=/path/to/your/dir   
REPOSITORY=<name of your repo>
MASTER="[email protected]:<username>/$REPOSITORY.git"   
[email protected]:<upstream>/<name of the repo>.git  

cd $WORKDIR && rm -rf $REPOSITORY
eval `ssh-agent` && expect ~/.ssh/agent && ssh-add -l
git clone $MASTER && cd $REPOSITORY && git checkout master
git remote add upstream $UPSTREAM && git fetch --prune upstream
if [ `git rev-list HEAD...upstream/master --count` -eq 0 ]
then
    echo "all the same, do nothing"
else
    echo "update exist, do rebase!"
    git reset --hard upstream/master
    git push origin master --force
fi
cd $WORKDIR && rm -rf $REPOSITORY
eval `ssh-agent -k`

Check your forked repository. From time to time it will always show this notification:

This branch is even with <upstream>:master.

enter image description here

终止放荡 2024-12-08 21:07:32
rm -rf oldrepository
git clone ...

可能有更微妙的选择,但这是我确信我的本地存储库与上游相同的唯一方法。

rm -rf oldrepository
git clone ...

There may be subtler options, but it is the only way that I have any confidence that my local repository is the same as upstream.

悟红尘 2024-12-08 21:07:31

在分叉存储库的本地克隆中,您可以将原始 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 main branch:

git checkout main

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

git rebase upstream/main

如果您不想重写主分支的历史记录(例如,因为其他人可能已经克隆了它),那么您应该将最后一个命令替换为 git merge upper/主要。然而,为了进一步发出尽可能干净的拉取请求,最好进行变基。


如果您已将分支重新设置为 upstream/main,您可能需要强制推送才能将其推送到 GitHub 上您自己的分叉存储库。您可以这样做:

git push -f origin main

您只需在重新设置基础后第一次使用 -f 即可。

In your local clone of your forked repository, you can add the original GitHub repository as a "remote". ("Remotes" are like nicknames for the URLs of repositories - origin is one, for example.) Then you can fetch all the branches from that upstream repository, and rebase your work to continue working on the upstream version. In terms of commands that might look like:

# 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 main branch:

git checkout main

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

git rebase upstream/main

If you don't want to rewrite the history of your main branch, (for example because other people may have cloned it) then you should replace the last command with git merge upstream/main. However, for making further pull requests that are as clean as possible, it's probably better to rebase.


If you've rebased your branch onto upstream/main you may need to force the push in order to push it to your own forked repository on GitHub. You'd do that with:

git push -f origin main

You only need to use the -f the first time after you've rebased.

欢你一世 2024-12-08 21:07:31

从 2014 年 5 月开始,可以直接从 GitHub 更新分支。截至 2017 年 9 月,这仍然有效,但是它将导致脏的提交历史记录。

  1. 在 GitHub 上打开您的 fork。
  2. 单击拉取请求
  3. 单击新拉取请求。默认情况下,GitHub 会将原始版本与您的分支进行比较,如果您没有进行任何更改,则不应进行任何比较。
  4. 如果您看到该链接,请点击切换基础。否则,手动将base fork下拉设置为您的fork,并将head fork设置为上游。现在 GitHub 会将您的分支与原始版本进行比较,您应该会看到所有最新的更改。
    输入图像描述这里
  5. 创建拉取请求并为您的拉取请求分配一个可预测的名称(例如,从原始内容更新)。
  6. 向下滚动到合并拉取请求,但不要单击任何内容。

现在您有三个选项,但每个选项都会导致不太干净的提交历史记录。

  1. 默认情况下会创建一个丑陋的合并提交。
  2. 如果您单击下拉菜单并选择“压缩并合并”,所有介入的提交将被压缩为一个。这通常是您不想要的。
  3. 如果您点击Rebase and merge,所有提交都将与您“一起”进行,原始 PR 将链接到您的 PR,并且 GitHub 将显示 Thisbranch is X commits advance, Y commits Behind <原始叉子>

所以,是的,您可以使用 GitHub Web UI 更新您的存储库及其上游,但这样做会玷污您的提交历史记录。坚持使用命令行 - 它是简单的。

Starting in May 2014, it is possible to update a fork directly from GitHub. This still works as of September 2017, BUT it will lead to a dirty commit history.

  1. Open your fork on GitHub.
  2. Click on Pull Requests.
  3. Click on New Pull Request. By default, GitHub will compare the original with your fork, and there shouldn't be anything to compare if you didn't make any changes.
  4. Click switching the base if you see that link. Otherwise, manually set the base fork drop down to your fork, and the head fork to the upstream. Now GitHub will compare your fork with the original, and you should see all the latest changes.
    enter image description here
  5. Create pull request and assign a predictable name to your pull request (e.g., Update from original).
  6. Scroll down to Merge pull request, but don't click anything yet.

Now you have three options, but each will lead to a less-than-clean commit history.

  1. The default will create an ugly merge commit.
  2. If you click the dropdown and choose "Squash and merge", all intervening commits will be squashed into one. This is most often something you don't want.
  3. If you click Rebase and merge, all commits will be made "with" you, the original PRs will link to your PR, and GitHub will display This branch is X commits ahead, Y commits behind <original fork>.

So yes, you can keep your repo updated with its upstream using the GitHub web UI, but doing so will sully your commit history. Stick to the command line instead - it's easy.

别靠近我心 2024-12-08 21:07:31

以下是 GitHub 关于同步分叉的官方文档:

同步分叉

设置

在同步之前,您需要添加一个指向上游存储库的远程服务器。您最初分叉时可能已经这样做了。

提示:同步您的分支只会更新您的存储库的本地副本;它不会更新您在 GitHub 上的存储库。

<前><代码>$ git远程-v
# 列出当前的遥控器
来源 https://github.com/user/repo.git (获取)
来源https://github.com/user/repo.git(推送)

$ git 远程添加上游 https://github.com/otheruser/repo.git
# 设置新的遥控器

$ git 远程 -v
# 验证新的远程
来源 https://github.com/user/repo.git (获取)
来源https://github.com/user/repo.git(推送)
上游 https://github.com/otheruser/repo.git (获取)
上游 https://github.com/otheruser/repo.git (推送)

正在同步

将存储库与上游同步需要两个步骤:首先必须从远程获取,然后必须将所需的分支合并到本地分支。

正在获取

从远程存储库中获取将引入其分支及其各自的提交。它们存储在本地存储库的特殊分支下。

$ git fetch 上游
# 获取上游远程分支
远程:计数对象:75,完成。
远程:压缩对象:100% (53/53),完成。
远程:总计 62 个(增量 27),重复使用 44 个(增量 9)
拆包对象:100% (62/62),完成。
来自 https://github.com/otheruser/repo
 * [新分支] master ->上游/主控

我们现在将上游的主分支存储在本地分支upstream/master

$ git 分支 -va
# 列出所有本地和远程跟踪分支
* master a422352 我的本地提交
  遥控器/原点/HEAD ->起源/主人
  Remotes/origin/master a422352 我的本地提交
  Remotes/upstream/master 5fdff0f 一些上游提交

合并

现在我们已经获取了上游存储库,我们希望将其更改合并到本地分支中。这将使该分支与上游同步,而不会丢失我们的本地更改。

$ git checkout master
# 查看我们本地的 master 分支
切换到分支“master”

$ git 合并上游/master
# 将上游的 master 合并到我们自己的 master 中
更新a422352..5fdff0f
快进
 自述文件 | 9 --------
 自述文件.md | 7 ++++++
 更改了 2 个文件,插入了 7 个(+),删除了 9 个(-)
 删除模式 100644 自述文件
 创建模式100644 README.md

如果您的本地分支没有任何唯一的提交,git 将执行“快进”:

$ git merge 上游/master
正在更新 34e91da..16c56ad
快进
 自述文件.md | 5 +++--
 1 个文件已更改,3 个插入(+),2 个删除(-)

提示:如果您想更新 GitHub 上的存储库,请按照说明操作这里

Here is GitHub's official document on Syncing a fork:

Syncing a fork

The Setup

Before you can sync, you need to add a remote that points to the upstream repository. You may have done this when you originally forked.

Tip: Syncing your fork only updates your local copy of the repository; it does not update your repository on GitHub.

$ git remote -v
# List the current remotes
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

$ git remote add upstream https://github.com/otheruser/repo.git
# Set a new remote

$ git remote -v
# Verify new remote
origin    https://github.com/user/repo.git (fetch)
origin    https://github.com/user/repo.git (push)
upstream  https://github.com/otheruser/repo.git (fetch)
upstream  https://github.com/otheruser/repo.git (push)

Syncing

There are two steps required to sync your repository with the upstream: first you must fetch from the remote, then you must merge the desired branch into your local branch.

Fetching

Fetching from the remote repository will bring in its branches and their respective commits. These are stored in your local repository under special branches.

$ git fetch upstream
# Grab the upstream remote's branches
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/otheruser/repo
 * [new branch]      master     -> upstream/master

We now have the upstream's master branch stored in a local branch, upstream/master

$ git branch -va
# List all local and remote-tracking branches
* master                  a422352 My local commit
  remotes/origin/HEAD     -> origin/master
  remotes/origin/master   a422352 My local commit
  remotes/upstream/master 5fdff0f Some upstream commit

Merging

Now that we have fetched the upstream repository, we want to merge its changes into our local branch. This will bring that branch into sync with the upstream, without losing our local changes.

$ git checkout master
# Check out our local master branch
Switched to branch 'master'

$ git merge upstream/master
# Merge upstream's master into our own
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

If your local branch didn't have any unique commits, git will instead perform a "fast-forward":

$ git merge upstream/master
Updating 34e91da..16c56ad
Fast-forward
 README.md                 |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Tip: If you want to update your repository on GitHub, follow the instructions here

月光色 2024-12-08 21:07:31

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

  1. 将目录更改为本地存储库。

    • 如果您不是 git checkout master,请切换到 master 分支
  2. 将父级添加为远程存储库,git remote add upper

  3. Issue git fetch uploaded
  4. Issue git rebase upper/master代码>

    • 在此阶段,您可以通过输入 git status 来检查是否提交了要合并的内容
  5. Issue git Push origin master

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

A lot of answers end up moving your fork one commit ahead of the parent repository. This answer summarizes the steps found here which will move your fork to the same commit as the parent.

  1. Change directory to your local repository.

    • Switch to master branch if you are not git checkout master
  2. Add the parent as a remote repository, git remote add upstream <repo-location>

  3. Issue git fetch upstream
  4. Issue git rebase upstream/master

    • At this stage you check that commits what will be merged by typing git status
  5. Issue git push origin master

For more information about these commands, refer to step 3.

私藏温柔 2024-12-08 21:07:31

如果像我一样,您从不直接向 master 提交任何内容(您确实应该这么做),那么您可以执行以下操作。

从您的分支的本地克隆中,创建您的上游远程。您只需要执行一次:

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

然后每当您想要赶上上游存储库 master 分支时,您需要:

git checkout master
git pull upstream master

假设您自己从未在 master 上提交过任何内容,那么您应该已经完成​​了。现在,您可以将本地 master 推送到原始远程 GitHub 分支。您还可以在当前最新的本地主分支上重新建立您的开发分支。

经过初始上游设置和主节点签出后,您所需要做的就是运行以下命令将主节点与上游同步:git pull upper upper master

If, like me, you never commit anything directly to master, which you should really, you can do the following.

From the local clone of your fork, create your upstream remote. You only need to do that once:

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

Then whenever you want to catch up with the upstream repository master branch you need to:

git checkout master
git pull upstream master

Assuming you never committed anything on master yourself you should be done already. Now you can push your local master to your origin remote GitHub fork. You could also rebase your development branch on your now up-to-date local master.

Past the initial upstream setup and master checkout, all you need to do is run the following command to sync your master with upstream: git pull upstream master.

枯寂 2024-12-08 21:07:31

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

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

git clone [email protected]:your_name/project_name.git
cd project_name

如果给出了该命令,则需要按以下顺序继续:

  1. 将“上游”添加到克隆的存储库中( “起源”):

    git远程添加上游[电子邮件受保护]:original_author /项目名称.git
    
  2. 从“上游”获取提交(和分支):

    git fetch 上游
    
  3. 切换到分支的“master”分支(“源”):

    git checkout master
    
  4. 存储“master”分支的更改:

    git 存储
    
  5. 将“上游”的“master”分支的更改合并到“源”的“master”分支:

    git 合并上游/master
    
  6. 解决合并冲突(如果有)并提交合并

    git commit -am“从上游合并”
    
  7. 将更改推送到您的分支

    <前><代码>git推送

  8. 取回您隐藏的更改(如果有)

    git stash pop
    
  9. 您完成了!恭喜!

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

Foreword: Your fork is the "origin" and the repository you forked from is the "upstream".

Let's assume that you cloned already your fork to your computer with a command like this:

git clone [email protected]:your_name/project_name.git
cd project_name

If that is given then you need to continue in this order:

  1. Add the "upstream" to your cloned repository ("origin"):

    git remote add upstream [email protected]:original_author/project_name.git
    
  2. Fetch the commits (and branches) from the "upstream":

    git fetch upstream
    
  3. Switch to the "master" branch of your fork ("origin"):

    git checkout master
    
  4. Stash the changes of your "master" branch:

    git stash
    
  5. Merge the changes from the "master" branch of the "upstream" into your the "master" branch of your "origin":

    git merge upstream/master
    
  6. Resolve merge conflicts if any and commit your merge

    git commit -am "Merged from upstream"
    
  7. Push the changes to your fork

    git push
    
  8. Get back your stashed changes (if any)

    git stash pop
    
  9. You're done! Congratulations!

GitHub also provides instructions for this topic: Syncing a fork

关于从前 2024-12-08 21:07:31

可以通过三种方式执行此操作:从 Web UI(选项 1)、从 GitHub CLI(选项 2)或从命令行(选项 3)。


选项 1 - 网页用户界面

  1. 在 GitHub 上,导航到您想要与上游存储库同步的分叉存储库的主页。

  2. 选择“获取上游”下拉列表。

输入图片此处描述

  • 查看有关上游存储库提交的详细信息,然后单击“获取并合并”。
  • 输入图片此处描述


    选项 2 - GitHub CLI

    要从其父级更新远程分叉,请使用 gh reposync 子命令并提供您的分叉名称作为参数。

    $ gh repo sync owner/cli-fork
    

    如果上游存储库的更改导致冲突,则 GitHub CLI 无法同步。您可以设置 -force 标志来覆盖目标分支。


    选项 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 将上游默认分支(在本例中为上游/主分支)中的更改合并到本地默认分支中。这将使您的分支的默认分支与上游存储库同步,而不会丢失本地更改。

    $ 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 文档- 同步分支


    @Paebbels 实现选项 2

    在您的用户空间/GitHub 组织中创建一个存储库,并添加单个 YAML 文件以及多个*.repos 文件。 _All.repos 引用其他 *.repos 文件(存储库组)。语法如下: /=:branch[,branch]*

    • .github/workflows /Synchronize.yml

      name:同步分叉存储库

      在:
      推:
      日程:
      # 每天 06:20 - 重新运行管道以检查分叉存储库的更新
      - 计划任务:'20 6 * * *'

      职位:
      同步:
      运行:ubuntu-latest

      步骤:
      - 名称:“

    There are three ways one can do that: from the web UI (Option 1), from the GitHub CLI (Option 2), or from the command line (Option 3).


    Option 1 - Web UI

    1. On GitHub, navigate to the main page of the forked repository that you want to sync with the upstream repository.

    2. Select the Fetch upstream drop-down.

    enter image description here

    1. Review the details about the commits from the upstream repository, then click Fetch and merge.

    enter image description here


    Option 2 - GitHub CLI

    To update the remote fork from its parent, use the gh repo sync subcommand and supply your fork name as argument.

    $ gh repo sync owner/cli-fork
    

    If the changes from the upstream repository cause conflict then the GitHub CLI can't sync. You can set the -force flag to overwrite the destination branch.


    Option 3 - Command Line

    Before syncing one's fork with an upstream repository, one must configure a remote that points to the upstream repository in Git.

    1 Open Git Bash.

    2 Change the current working directory to your local project.

    3 Fetch the branches and their respective commits from the upstream repository. Commits to BRANCHNAME will be stored in the local branch upstream/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 Check out your fork's local default branch - in this case, we use main.

    $ git checkout main
    > Switched to branch 'main'
    

    5 Merge the changes from the upstream default branch - in this case, upstream/main - into your local default branch. This brings your fork's default branch into sync with the upstream repository, without losing your local changes.

    $ 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
    

    If one's local branch didn't have any unique commits, Git will instead perform a "fast-forward":

    $ git merge upstream/main
    > Updating 34e91da..16c56ad
    > Fast-forward
    >  README.md                 |    5 +++--
    >  1 file changed, 3 insertions(+), 2 deletions(-)
    

    Note: Syncing one's fork only updates one's local copy of the repo.
    To update one's fork on GitHub.com, one must push ones changes.


    Source: GitHub Docs - Syncing a fork


    Implementation of Option 2 by @Paebbels

    Create a repository in your userspace / GitHub organisation and add a single YAML file as well as multiple *.repos files. The _All.repos refers to other *.repos files (repository groups). The syntax is as follows: <upstream-org>/<upstream-repo>=<local-repo>:branch[,branch]*

    • .github/workflows/Synchronize.yml

      name: Synchronize forked repositories
      
      on:
        push:
        schedule:
      # Every day at 06:20 - rerun pipeline to check for updates on forked repositories
          - cron: '20 6 * * *'
      
      jobs:
        Synchronize:
          runs-on: ubuntu-latest
      
          steps:
            - name: '???? Checkout'
              uses: actions/checkout@v4
      
            - name: Check GitHub API
              run: |
                echo "which gh=$(which gh)"
                echo "gh --version: $(gh --version)"
                gh help
      
            - name: Synchronize Repositories
              run: |
                echo "See: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork"
                export GH_TOKEN=${{ secrets.GH_TOKEN }}
                GitHubUser=Paebbels      # Adjust to your user/organisation name
      
                while IFS=
      
    • _ALL.repos

      OSVVM
      _Others
      
    • OSVVM.repos

      OSVVM/osvvm=OSVVM:main
      OSVVM/osvvm=OSVVM-Libraries:main,dev
      
    • _Others.repos

      progit/progit2=progit2:main
      
    \r\n' read -r organisation; do echo "${organisation}" while IFS=
  • _ALL.repos

    
    
  • OSVVM.repos

    
    
  • _Others.repos

    
    
  • \r\n' read -r line; do parent=${line%=*} fork=${line#*=} repo=${fork%:*} branches=${fork#*:} branches=(${branches//,/ }) echo " ${parent} => ${repo}" for branch in "${branches[@]}"; do echo " gh repo sync $GitHubUser/${repo} -b ${branch}" gh repo sync $GitHubUser/${repo} -b ${branch} || true done done < <(cat "${organisation}.repos") done < <(cat "_ALL.repos")
  • _ALL.repos

    
    
  • OSVVM.repos

    
    
  • _Others.repos

    
    
  • 倒数 2024-12-08 21:07:31

    GitHub 现在引入了一项功能,只需单击按钮即可同步分叉

    转到您的分叉,单击获取上游,然后单击获取并合并直接同步您的分叉及其父仓库。

    输入图像描述这里

    您还可以在合并之前点击比较按钮来比较更改。

    参考:GitHub 的 文档

    GitHub has now introduced a feature to sync a fork with the click of a button.

    Go to your fork, click on Fetch upstream, and then click on Fetch and merge to directly sync your fork with its parent repo.

    enter image description here

    You may also click on the Compare button to compare the changes before merging.

    Reference: GitHub's documentation

    咋地 2024-12-08 21:07:31

    自 2013 年 11 月以来,GitHub 提出了一个非官方功能请求,要求他们添加一个非常简单直观的方法来保持本地分支与上游同步:

    https://github.com/isaacs/github/issues/121

    注意:由于该功能请求是非官方的,因此建议联系 [email protected] 将您对此类功能的支持添加到予以实施。上述非官方功能请求可以用作对此实施的兴趣程度的证据。

    Since November 2013 there has been an unofficial feature request open with GitHub to ask them to add a very simple and intuitive method to keep a local fork in sync with upstream:

    https://github.com/isaacs/github/issues/121

    Note: Since the feature request is unofficial it is also advisable to contact [email protected] to add your support for a feature like this to be implemented. The unofficial feature request above could be used as evidence of the amount of interest in this being implemented.

    游魂 2024-12-08 21:07:31

    截至本答案发布之日,GitHub 还没有(或者我应该说不再了吗?) Web 界面中的此功能。不过,您可以询问[电子邮件受保护] 对此添加您的投票。

    与此同时,GitHub 用户 bardiharborow 创建了一个工具来执行此操作:
    https://upriver.github.io/

    来源在这里:https://github.com/upriver/upriver.github.io

    As of the date of this answer, GitHub has not (or shall I say no longer?) this feature in the web interface. You can, however, ask [email protected] to add your vote for that.

    In the meantime, GitHub user bardiharborow has created a tool to do just this:
    https://upriver.github.io/

    Source is here: https://github.com/upriver/upriver.github.io

    江南月 2024-12-08 21:07:31

    如果您使用的是 Windows 或 Mac 版 GitHub,那么现在他们具有更新分支的一键功能:

    1. 在 UI 中选择存储库。
    2. 单击顶部的“从用户/分支更新”按钮。

    If you are using GitHub for Windows or Mac then now they have a one-click feature to update forks:

    1. Select the repository in the UI.
    2. Click "Update from user/branch" button the top.
    木有鱼丸 2024-12-08 21:07:31
    $ git remote add upstream https://github.com/....
    
    $ git pull upstream main
    
    $ git push
    
    $ git remote add upstream https://github.com/....
    
    $ git pull upstream main
    
    $ git push
    
    月下凄凉 2024-12-08 21:07:31

    实际上,可以从浏览器中上游的任何提交在分支中创建分支:

    在此处输入图像描述

    然后,您可以将该分支提取到本地克隆,并且当您在此基础上进行编辑 犯罪。或者使用 Web 界面更改该分支中的某些内容。

    它是如何工作的(这是一个猜测,我不知道 GitHub 到底是如何做到的):分叉共享对象存储并使用 命名空间来分隔用户的引用。因此,您可以通过分叉访问所有提交,即使它们在分叉时不存在。

    Actually, it is possible to create a branch in your fork from any commit of the upstream in the browser:

    Enter image description here

    You can then fetch that branch to your local clone, and you won't have to push all that data back to GitHub when you push edits on top of that commit. Or use the web interface to change something in that branch.

    How it works (it is a guess, I don't know how exactly GitHub does it): forks share object storage and use namespaces to separate users' references. So you can access all commits through your fork, even if they did not exist by the time of forking.

    巴黎盛开的樱花 2024-12-08 21:07:31

    请按照以下步骤操作。我尝试过它们,它对我有帮助。

    到您的分行结帐

    语法: gitbranch yourDevelopmentBranch
    示例: git checkout master

    拉取源存储库分支以获取最新代码

    语法: git pull https://github.com/tastejs /awesome-app-ideas 大师
    示例: git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git< /a> BRANCH_NAME


    Follow the below steps. I tried them and it helped me.

    Checkout to your branch

    Syntax: git branch yourDevelopmentBranch
    Example: git checkout master

    Pull source repository branch for getting the latest code

    Syntax: git pull https://github.com/tastejs/awesome-app-ideas master
    Example: git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git BRANCH_NAME

    新一帅帅 2024-12-08 21:07:31

    我用这一行更新我的分叉存储库:

    git pull https://github.com/forkuser/forkedrepo.git branch
    

    如果您不想向项目添加另一个远程端点,请使用此行,正如此处发布的其他解决方案一样。

    I update my forked repos with this one line:

    git pull https://github.com/forkuser/forkedrepo.git branch
    

    Use this if you dont want to add another remote endpoint to your project, as other solutions posted here.

    何时共饮酒 2024-12-08 21:07:31

    作为对这个答案的补充,我一直在寻找一种从上游分支一次性更新我的克隆存储库(来源)的所有远程分支的方法。我就是这样做的。

    这假设您已经配置了一个指向源存储库的上游远程(其中起源是从其分叉的),并将其与git fetch上游同步。

    然后运行:

    for branch in $(git ls-remote --heads upstream|sed 's#^.*refs/heads/##'); do git push origin refs/remotes/upstream/$branch:refs/heads/$branch; done
    

    此命令的第一部分列出上游远程存储库中的所有头,并删除 SHA-1 后跟refs/heads/分支名称前缀。

    然后,对于每个分支,它将上游远程跟踪分支(本地端的refs/remotes/upstream/)的本地副本直接推送到上的远程分支(refs/heads/ 在远程端)。

    这些分支同步命令中的任何一个都可能因以下两个原因之一而失败:上游分支已被重写,或者您已将该分支上的提交推送到您的分支。在第一种情况下,如果您没有向分支上的分支提交任何内容,则可以安全地强制推送(添加 -f 开关;即 git push -f上面的命令)。在另一种情况下,这是正常的,因为您的分叉分支已经分叉,并且在您的提交合并回上游之前,您不能指望同步命令起作用。上游

    As a complement to this answer, I was looking for a way to update all remote branches of my cloned repo (origin) from upstream branches in one go. This is how I did it.

    This assumes you have already configured an upstream remote pointing at the source repository (where origin was forked from) and have synced it with git fetch upstream.

    Then run:

    for branch in $(git ls-remote --heads upstream|sed 's#^.*refs/heads/##'); do git push origin refs/remotes/upstream/$branch:refs/heads/$branch; done
    

    The first part of this command lists all heads in the upstream remote repo and removes the SHA-1 followed by refs/heads/ branch name prefix.

    Then for each of these branches, it pushes the local copy of the upstream remote tracking branch (refs/remotes/upstream/<branch> on local side) directly to the remote branch on origin (refs/heads/<branch> on remote side).

    Any of these branch sync commands may fail for one of two reasons: either the upstream branch have been rewritten, or you have pushed commits on that branch to your fork. In the first case where you haven't committed anything to the branch on your fork it is safe to push forcefully (Add the -f switch; i.e. git push -f in the command above). In the other case this is normal as your fork branch have diverged and you can't expect the sync command to work until your commits have been merged back into upstream.

    山有枢 2024-12-08 21:07:31

    “Pull”应用是一种自动设置并忘记的解决方案。它将把你的分支的默认分支与上游存储库同步。

    访问该 URL,单击绿色的“安装”按钮,然后选择要启用自动同步的存储库。

    该分支每小时直接在 GitHub 上更新一次,在本地计算机上,您需要拉取 master 分支以确保本地副本同步。

    The "Pull" app is an automatic set-up-and-forget solution. It will sync the default branch of your fork with the upstream repository.

    Visit the URL, click the green "Install" button and select the repositories where you want to enable automatic synchronization.

    The branch is updated once per hour directly on GitHub, on your local machine you need to pull the master branch to ensure that your local copy is in sync.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文