简体   繁体   English

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

[英]How do I update or sync a forked repository on GitHub?

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?我如何将这些提交放入我的分叉中?

In your local clone of your forked repository, you can add the original GitHub repository as a "remote".在分叉存储库的本地克隆中,您可以将原始 GitHub 存储库添加为“远程”。 ("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. (“远程”就像存储库 URL 的昵称 - 例如, origin是其中之一。)然后您可以从该上游存储库中获取所有分支,并重新调整您的工作以继续处理上游版本。 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 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

If you don't want to rewrite the history of your master branch, (for example because other people may have cloned it) then you should replace the last command with git merge upstream/master .如果您不想重写 master 分支的历史记录(例如,因为其他人可能已经克隆了它),那么您应该将最后一个命令替换为git merge upstream/master 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/master you may need to force the push in order to push it to your own forked repository on GitHub.如果您已将分支重新定位到upstream/master上,您可能需要强制推送以便将其推送到您自己在 GitHub 上的分叉存储库。 You'd do that with:你会这样做:

git push -f origin master

You only need to use the -f the first time after you've rebased.您只需要在重新定位后第一次使用-f即可。

Starting in May 2014, it is possible to update a fork directly from GitHub.从 2014 年 5 月开始,可以直接从 GitHub 更新分叉。 This still works as of September 2017, BUT it will lead to a dirty commit history.这在 2017 年 9 月仍然有效,它会导致一个肮脏的提交历史。

  1. Open your fork on GitHub.在 GitHub 上打开你的 fork。
  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.默认情况下,GitHub 会将原始版本与您的 fork 进行比较,如果您未进行任何更改,则不应有任何可比较的内容。
  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.现在 GitHub 会将您的 fork 与原始版本进行比较,您应该会看到所有最新的更改。 在此处输入图像描述
  5. Create pull request and assign a predictable name to your pull request (eg, Update from original ).创建拉取请求并为您的拉取请求分配一个可预测的名称(例如, Update from original )。
  6. Scroll down to Merge pull request , but don't click anything yet.向下滚动到Merge pull request ,但不要单击任何内容。

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> .如果你点击Rebase and merge ,所有的提交都会和你一起进行,原来的 PR 会链接到你的 PR,GitHub 会显示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.所以是的,你可以使用 GitHub Web UI 让你的 repo 更新到它的上游,但是这样做会破坏你的提交历史。 Stick to the command line instead - it's easy.改用命令行- 这很容易。

Here is GitHub's official document on Syncing a fork :这是 GitHub 关于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;提示:同步你的 fork 只会更新你的本地仓库副本; it does not update your repository on GitHub.它不会更新您在 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我们现在将上游的 master 分支存储在本地分支 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 将改为执行“快进”:

 $ 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提示:如果您想更新 GitHub 上的存储库,请按照此处的说明进行操作

A lot of answers end up moving your fork one commit ahead of the parent repository.很多答案最终会将您的 fork 移到父存储库之前的一个提交 This answer summarizes the steps found here which will move your fork to the same commit as the parent .此答案总结了此处找到的步骤,这些步骤会将您的 fork 移至与 parent 相同的提交

  1. Change directory to your local repository.将目录更改为您的本地存储库。

    • Switch to master branch if you are not git checkout master如果你不是git checkout master切换到 master 分支
  2. Add the parent as a remote repository, git remote add upstream <repo-location>将父级添加为远程存储库, git remote add upstream <repo-location>

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

    • At this stage you check that commits what will be merged by typing git status在这个阶段,您可以通过输入git status检查是否提交了将要合并的内容
  5. Issue git push origin master发出git push origin master

For more information about these commands, refer to step 3 .有关这些命令的更多信息,请参阅步骤 3

If, like me, you never commit anything directly to master , which you should really, you can do the following.如果像我一样,你从不直接向 master 提交任何东西,你真的应该这样做,你可以执行以下操作。

From the local clone of your fork, create your upstream remote.从 fork 的本地克隆,创建上游远程。 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.假设你自己从来没有在 master 上做过任何事情,你应该已经完成​​了。 Now you can push your local master to your origin remote GitHub fork.现在,您可以将本地 master 推送到您的源远程 GitHub 分支。 You could also rebase your development branch on your now up-to-date local master.您还可以在您现在最新的本地 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 .通过初始上游设置和主节点结帐,您需要做的就是运行以下命令以将您的主节点与上游同步: git pull upstream master

Foreword: Your fork is the "origin" and the repository you forked from is the "upstream".前言:你的 fork 是“起源”,而你 fork 的存储库是“上游”。

Let's assume that you cloned already your fork to your computer with a command like this:假设您已经使用以下命令将 fork 克隆到您的计算机:

git clone git@github.com: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 git@github.com: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"):切换到 fork 的“master”分支(“origin”):

     git checkout master
  4. Stash the changes of your "master" branch:存储“master”分支的更改:

     git stash
  5. Merge the changes from the "master" branch of the "upstream" into your the "master" branch of your "origin":将“上游”的“master”分支的更改合并到“origin”的“master”分支中:

     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将更改推送到您的 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 GitHub 还提供了有关此主题的说明:同步分叉

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:自 2013 年 11 月以来,GitHub 收到了一个非官方的功能请求,要求他们添加一种非常简单直观的方法来保持本地分支与上游同步:

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

Note: Since the feature request is unofficial it is also advisable to contact support@github.com to add your support for a feature like this to be implemented.注意:由于功能请求是非官方的,因此建议您联系support@github.com以添加您对要实现的此类功能的支持。 The unofficial feature request above could be used as evidence of the amount of interest in this being implemented.上面的非官方功能请求可以用作对此正在实施的兴趣量的证据。

GitHub has now introduced a feature to sync a fork with the click of a button . GitHub 现在推出了一项功能,只需单击按钮即可同步分叉

Go to your fork, click on Fetch upstream , and then click on Fetch and merge to directly sync your fork with its parent repo.转到您的 fork,单击Fetch upstream ,然后单击Fetch and merge以直接将您的 fork 与其父 repo 同步。

在此处输入图像描述

You may also click on the Compare button to compare the changes before merging.您也可以单击“ Compare ”按钮在合并之前比较更改。

Reference : GitHub's documentation参考:GitHub 的文档

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).有三种方法可以做到这一点:从 Web UI(选项 1)、从 GitHub CLI(选项 2)或从命令行(选项 3)。


Option 1 - Web UI选项 1 - 网页界面

  1. On GitHub, navigate to the main page of the forked repository that you want to sync with the upstream repository.在 GitHub 上,导航到要与上游存储库同步的分叉存储库的主页。

  2. Select the Fetch upstream drop-down.选择获取上游下拉菜单。

在此处输入图像描述

  1. Review the details about the commits from the upstream repository, then click Fetch and merge.查看来自上游存储库的提交的详细信息,然后单击 Fetch and merge。

在此处输入图像描述


Option 2 - GitHub CLI选项 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子命令并提供您的分支名称作为参数。

$ gh repo sync owner/cli-fork

If the changes from the upstream repository cause conflict then the GitHub CLI can't sync.如果上游存储库的更改导致冲突,则 GitHub CLI 无法同步。 You can set the -force flag to overwrite the destination branch.您可以设置-force标志以覆盖目标分支。


Option 3 - Command Line选项 3 - 命令行

Before syncing one's fork with an upstream repository, one must configure a remote that points to the upstream repository in Git.在将一个分支与上游存储库同步之前,必须在 Git 中配置一个指向上游存储库的远程

1 Open Git Bash. 1 打开 Git Bash。

2 Change the current working directory to your local project. 2 将当前工作目录更改为您的本地项目。

3 Fetch the branches and their respective commits from the upstream repository. 3 从上游存储库获取分支及其各自的提交。 Commits to BRANCHNAME will be stored in the local branch upstream/BRANCHNAME.对 BRANCHNAME 的提交将存储在本地分支 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. 4 检查你的 fork 的本地默认分支 - 在这种情况下,我们使用 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. 5 将上游默认分支(在本例中为 upstream/main)中的更改合并到本地默认分支中。 This brings your fork's default branch into sync with the upstream repository, without losing your local changes.这将使您的 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

If one's local branch didn't have any unique commits, Git will instead perform a "fast-forward":如果一个本地分支没有任何独特的提交,Git 将改为执行“快进”:

$ 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 .要在 GitHub.com 上更新一个分支,必须推送一个更改


Source: GitHub Docs - Syncing a fork来源: GitHub Docs - 同步分叉

As of the date of this answer, GitHub has not ( or shall I say no longer? ) this feature in the web interface.截至本答案发布之日,GitHub 还没有(或者我应该不再说? )Web 界面中的此功能。 You can, however, ask support@github.com to add your vote for that.但是,您可以要求support@github.com添加您的投票。

In the meantime, GitHub user bardiharborow has created a tool to do just this: https://upriver.github.io/与此同时,GitHub 用户 bardiharborow 创建了一个工具来做到这一点: https ://upriver.github.io/

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

If you are using GitHub for Windows or Mac then now they have a one-click feature to update forks:如果您使用 GitHub for Windows 或 Mac,那么现在它们具有一键更新 forks 的功能:

  1. Select the repository in the UI.在 UI 中选择存储库。
  2. Click "Update from user/branch" button the top.单击顶部的“从用户/分支更新”按钮。

Actually, it is possible to create a branch in your fork from any commit of the upstream in the browser:实际上,可以从浏览器中上游的任何提交在您的 fork 中创建一个分支:

在此处输入图像描述

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.然后,您可以将该分支获取到您的本地克隆,当您在该提交之上推送编辑时,您不必将所有数据推送回 GitHub。 Or use the web interface to change something in that branch.或者使用 Web 界面更改该分支中的某些内容。

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.它是如何工作的(这是一个猜测,我不知道 GitHub 到底是怎么做的):forks 共享对象存储并使用命名空间来分隔用户的引用。 So you can access all commits through your fork, even if they did not exist by the time of forking.因此,您可以通过您的 fork 访问所有提交,即使它们在分叉时不存在。

Follow the below steps.请按照以下步骤操作。 I tried them and it helped me.我尝试了它们,它帮助了我。

Checkout to your branch结帐到您的分行

Syntax: git branch yourDevelopmentBranch语法: git branch yourDevelopmentBranch
Example: git checkout master示例: git checkout master

Pull source repository branch for getting the latest code拉取源代码库分支以获取最新代码

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

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.如果您不想将另一个远程端点添加到您的项目中,请使用此选项,就像此处发布的其他解决方案一样。

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.作为对这个答案的补充,我正在寻找一种方法来一次从上游分支更新我克隆的 repo ( origin ) 的所有远程分支。 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 .这假设您已经配置了一个指向源存储库( origin从中分叉的地方)的上游远程,并已将其与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.该命令的第一部分列出了上游远程 repo 中的所有头,并删除了 SHA-1,后跟refs/heads/分支名称前缀。

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).然后对于这些分支中的每一个,它将上游远程跟踪分支的本地副本(本地端的refs/remotes/upstream/<branch> )直接推送到端的远程分支(远程端的refs/heads/<branch> )。

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.这些分支同步命令中的任何一个都可能由于以下两个原因之一失败:上游分支已被重写,或者您已将该分支上的提交推送到您的 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; ie git push -f in the command above).在第一种情况下,您没有向 fork 上的分支提交任何内容,强制推送是安全的(添加-f开关;即git push -f在上面的命令中)。 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 .在另一种情况下,这是正常的,因为您的 fork 分支已经发散,并且在您的提交合并回upstream之前,您不能期望 sync 命令起作用。

The "Pull" app is an automatic set-up-and-forget solution. “Pull”应用程序是一种自动设置和忘记的解决方案。 It will sync the default branch of your fork with the upstream repository.它会将你的 fork 的默认分支与上游存储库同步。

Visit the URL, click the green "Install" button and select the repositories where you want to enable automatic synchronization.访问 URL,单击绿色的“安装”按钮并选择要启用自动同步的存储库。

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.该分支每小时直接在 GitHub 上更新一次,在您的本地机器上,您需要拉取 master 分支以确保您的本地副本是同步的。

If you set your upstream.如果你设置你的上游。 Check with git remote -v , then this will suffice.检查git remote -v ,就足够了。

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

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.克隆分叉存储库后,转到克隆所在的目录路径和 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

And there you are good to go.你可以走了。 All updated changes in the main repository will be pushed into your fork repository.主存储库中的所有更新更改都将推送到您的 fork 存储库中。

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. “fetch”命令对于在项目中保持最新是必不可少的:只有在执行“git fetch”时,您才会被告知您的同事推送到远程服务器的更改。

You can still visit here for further queries您仍然可以访问此处进行进一步查询

Assuming your fork is https://github.com/me/foobar and original repository is https://github.com/someone/foobar假设你的 fork 是https://github.com/me/foobar并且原始存储库是https://github.com/someone/foobar

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

  2. If you see green text Able to merge then press Create pull request如果您看到绿色文本Able to merge然后按Create pull request

  3. On the next page, scroll to the bottom of the page and click Merge pull request and Confirm merge .在下一页上,滚动到页面底部,然后单击Merge pull requestConfirm 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>

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

$ git pull upstream main

$ git push

Android Studio now has learned to work with GitHub fork repositories (you don't even have to add "upstream" remote repository by console command). Android Studio 现在已经学会了使用 GitHub fork 存储库(您甚至不必通过控制台命令添加“上游”远程存储库)。

Open menu VCSGit打开菜单VCSGit

And pay attention to the two last popup menu items:并注意最后两个弹出菜单项:

  • Rebase my GitHub fork Rebase 我的 GitHub 分支

  • 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.无论如何,在您单击“Rebase my GitHub fork”后,将可以在 Android Studio 中访问来自父远程存储库(“上游”)的分支,并且您将能够轻松地使用它们进行操作。

(I use Android Studio 3.0 with "Git integration" and "GitHub" plugins.) (我使用带有“Git 集成”和“GitHub”插件的 Android Studio 3.0。)

在此处输入图像描述

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 (eg drop history).如果它是一个相当大的存储库,您可能希望以一种特殊的方式来管理它(例如删除历史记录)。 Basically, you can get differences between current and upstream versions, commit them and then cherry pick back to master.基本上,您可以获得当前版本和上游版本之间的差异,提交它们,然后将它们挑选回 master。

Try reading this one .试试看 这个 It describes how to handle big Git repositories and how to upstream them with latest changes.它描述了如何处理大型 Git 存储库以及如何使用最新更改将它们上传到上游。

I would like to add on to @krlmlr's answer .我想补充一下@krlmlr 的答案

Initially, the forked repository has one branch named : master .最初,分叉的存储库有一个名为master的分支。 If you are working on a new feature or a fix, you would generally create a new branch feature and make the changes.如果您正在开发新功能或修复,您通常会创建一个新的分支feature并进行更改。

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:如果您希望分叉存储库与父存储库同步,您可以为Pull 应用程序在功能分支中)设置一个配置文件( pull.yml ),如下所示:

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.这使分叉存储库的master分支与父存储库保持同步。 It keeps the feature branch of the forked repo updated via the master branch of the forked repo by merging the same.它通过合并相同的分叉存储库的master分支来保持分叉存储库的feature分支更新。 This assumes that the feature branch is the default branch which contains the config file.这假设feature分支是包含配置文件的默认分支。

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 .这里有两种合并hardreset mergemethods它有助于强制在分叉的 repo 的master分支中与父 repo 同步更改,另一种方法是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.此方法用于合并您在feature分支中所做的更改以及由于master分支中的强制同步而完成的更改。 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 .您可以在此处阅读有关基本和高级配置以及各种mergemethods的信息。

I am currently using this configuration in my forked repo here to make sure an enhancement requested here stays updated.我目前在我的分叉存储库中使用此配置,以确保此处请求增强功能保持更新。

Try this, Click on "Fetch upstream" to sync your forked repo from upstream 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 . 1. 从 fork master 创建分支在那里进行更改

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.因此,当您的Pull Request被接受时,您可以安全地删除分支,因为当您使用上游更新它时,您贡献的代码将存在于您的分叉存储库的主库中。 By this your master will always be in clean condition to create a new branch to do another change.这样,您的 master 将始终处于干净状态,可以创建一个新分支来进行另一次更改。

2. Create a scheduled job for the fork master to do update automatically . 2.为fork master创建一个计划的工作自动更新

This can be done with cron .这可以通过cron来完成。 Here is for an example code if you do it in linux.如果您在 linux 中执行此操作,这是一个示例代码。

$ crontab -e

put this code on the crontab file to execute the job in hourly basis.将此代码放在crontab file中,以每小时执行一次作业。

0 * * * * sh ~/cron.sh

then create the cron.sh script file and a git interaction with ssh-agent and/or expect as below然后创建cron.sh脚本文件和与ssh-agent和/或期望git 交互,如下所示

#!/bin/sh
WORKDIR=/path/to/your/dir   
REPOSITORY=<name of your repo>
MASTER="git@github.com:<username>/$REPOSITORY.git"   
UPSTREAM=git@github.com:<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 .这个分支甚至与<upstream> :master

在此处输入图像描述

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

If you use GitHub Desktop, you can do it easily in just 6 steps (actually only 5).如果您使用 GitHub Desktop,只需 6 步(实际上只有 5 步)即可轻松完成。

Once you open Github Desktop and choose your repository,打开 Github Desktop 并选择您的存储库后,

  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.根据您的活动分支,在master / branch-name中单击 Merge。
  6. Wait for GitHub Desktop to do the magic.等待 GitHub Desktop 施展魔法。

Checkout the GIF below as an example:以下面的 GIF 为例:

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

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.如果你想让你的 GitHub 分支与各自的上游保持同步,还有专门为 GitHub 提供的这个 probot 程序: https ://probot.github.io/apps/pull/ 可以完成这项工作。 You would need to allow installation in your account and it will keep your forks up to date.您需要在您的帐户中允许安装,这将使您的分叉保持最新。

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
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.可能有更微妙的选择,但这是我确信我的本地存储库与上游相同的唯一方法。

Delete your remote dev from github page从 github 页面删除您的远程开发人员

then apply these commands:然后应用这些命令:

1) git branch -D dev
2) git fetch upstream
3) git checkout master
4) git fetch upstream && git fetch upstream --prune && git rebase upstream/master && git push -f origin master
5) git checkout -b dev
6) git push origin dev
7) git fetch upstream && git fetch upstream --prune && git rebase upstream/dev && 8) git push -f origin dev

to see your configuration use this command:要查看您的配置,请使用以下命令:

git remote -v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM