使用 git pull 在不合并的情况下跟踪远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3056335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:21:18  来源:igfitidea点击:

Using git pull to track a remote branch without merging

gitgit-pull

提问by dahlbyk

I am using git to track content which is changed by some people and shared "read-only" with others. The "readers" may from time to time need to make a change, but mostly they will not.

我正在使用 git 来跟踪由某些人更改并与其他人共享“只读”的内容。“读者”可能不时需要做出改变,但大多数情况下他们不会。

I want to allow for the git "writers" to rebase pushed branches** if need be, and ensure that the "readers" never accidentally get a merge. That's normally easy enough.

如果需要,我希望允许 git“编写者”对推送的分支进行 rebase,并确保“读者”永远不会意外地获得合并。这通常很容易。

git pull origin +master

There's one case that seems to cause problems. If a reader makes a local change, the command above will merge. I want pull to be fully automatic if the reader has not made local changes, while if they have made local changes, it should stop and ask for input. I want to track any upstream changes while being careful about merging downstream changes.

有一种情况似乎会引起问题。如果读者进行本地更改,则上面的命令将合并。如果读者没有进行本地更改,我希望 pull 是全自动的,而如果他们进行了本地更改,它应该停止并要求输入。我想跟踪任何上游更改,同时小心合并下游更改。

In a way, I don't really want to pull. I want to track the master branch exactly.

在某种程度上,我真的不想拉。我想准确地跟踪主分支。



** (I know this is not a best practice, but it seems necessary in our case: we have one main branch that contains most of the work and some topic branches for specific customers with minor changes that need to be isolated. It seems easiest to frequently rebase to keep the topics up to date.)

**(我知道这不是最佳实践,但在我们的案例中似乎是必要的:我们有一个包含大部分工作的主分支和一些针对特定客户的主题分支,其中包含需要隔离的微小更改。这似乎最简单经常变基以使主题保持最新。)

回答by Seamus Connor

Your are looking for the command git-fetch.

您正在寻找命令git-fetch

回答by dahlbyk

You might also find git pull --rebaseuseful.

你可能也会觉得git pull --rebase有用。

Update:--rebasecan be made the default pullbehavior by setting branch.<name>.rebase = trueon individual branches. Setting branch.autosetuprebase = truewill set this on new branches by default, though existing branches would need to be updated manually. Or you can always default to --rebaseby setting pull.rebase = trueglobally.

更新:--rebase可以pull通过branch.<name>.rebase = true在各个分支上设置来使其成为默认行为。branch.autosetuprebase = true默认情况下,设置将在新分支上设置此项,但需要手动更新现有分支。或者您始终可以--rebase通过pull.rebase = true全局设置来默认为。

回答by cdunn2001

How about:

怎么样:

  git pull --ff-only origin master

I use that often. Unfortunately, as richard-hansen@ pointed out below, this does not work as an alias:

我经常使用那个。不幸的是,正如richard-hansen@ 在下面指出的那样,这不能作为别名使用:

[alias]
pull = pull --ff-only

回答by VonC

Git 2.0 (Q2 2014) will add in commit b814da8a config push.ff:

Git 2.0(2014 年第二季度)将在提交 b814da8 中添加一个配置push.ff

pull.ff::

By default, Git does not create an extra merge commit when merging a commit that is a descendant of the current commit. Instead, the tip of the current branch is fast-forwarded.

  • When set to false, this variable tells Git to create an extra merge commit in such a case (equivalent to giving the --no-ff option from the command line).
  • When set to only, only such fast-forward merges are allowed (equivalent to giving the --ff-onlyoption from the command line).

默认情况下,Git 在合并作为当前提交的后代的提交时不会创建额外的合并提交。相反,当前分支的尖端是快进的。

  • 当设置为 false 时,这个变量告诉 Git 在这种情况下创建一个额外的合并提交(相当于从命令行提供 --no-ff 选项)。
  • 当设置为 only 时,只允许这样的快进合并(相当于--ff-only从命令行提供选项)。

回答by rickfoosusa

I think you want a workflow using two branches. A merged local branch (master), and a remote 'read-only' tracking branch (remote).

我认为你想要一个使用两个分支的工作流。合并的本地分支(主)和远程“只读”跟踪分支(远程)。

Using dual remotes on a master branch allows you to continue working, and keep up to date with the remote project. Your local changes, and merged remote changes can be pushed to your local git server.

在主分支上使用双遥控器可以让您继续工作,并与远程项目保持同步。您的本地更改和合并的远程更改可以推送到您的本地 git 服务器。

 git push localserver master

Upstream patches can be created and refined on your remote tracking branches, and submitted to the project.

可以在远程跟踪分支上创建和改进上游补丁,并提交给项目。

A separate tracking branch for the 'read-only' remote lets you prepare patches and commits for upstream to the remote project.

“只读”远程的单独跟踪分支使您可以为远程项目的上游准备补丁和提交。

# Master branch is merge of local changes, and remote changes
git checkout master
git pull -m origin master
# Set up a local tracking branch for the 'read-only' remote.
git checkout -b remote remote master
# Start reviewing changes between the two branches.
git diff --name-status ..master

When you want to upstream a patch, a diff from master..remote can be used to define the patch your read-only remote branch.

当您想要上游补丁时,可以使用 master..remote 的差异来定义只读远程分支的补丁。

git diff master..remote -- files >patch
git checkout remote
patch -p1 <patch
git commit -m "Your patch"
git format-patch -1

Send the formatted patch file to the remote project. If you need to amend and rebase for the remote project, you can do that work on your remote tracking branch until it's approved.

将格式化的补丁文件发送到远程项目。如果您需要为远程项目修改和变基,您可以在远程跟踪分支上执行该工作,直到获得批准。

(Setting up a git dual remote involves editing .git/config, and is explained elsewhere)

(设置 git dual remote 涉及编辑 .git/config,并在别处解释)

回答by Adam Dymitruk

Since you don't want the manual merging by the readers fetching, you could write a hook to examine what came down and prompt the user:

由于您不希望读取器获取手动合并,您可以编写一个钩子来检查出现的内容并提示用户:

http://git-scm.com/docs/githooks

http://git-scm.com/docs/githooks

I would still just recommend fetching and having the readers manage their merges or rebases locally. That's a simpler solution to your problem.

我仍然只建议获取并让读者在本地管理他们的合并或变基。这是解决您问题的更简单的方法。