没有结帐的 Git 拉取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18857570/
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-10 16:58:59  来源:igfitidea点击:

Git pull without checkout?

git

提问by Diana Saunders

I'm used to running git pull and other commands from within a branch I'm working on. But I have set up a development server that several people work on, so I don't want to have to switch branches when I do it. If I want to update an existing branch on the dev server from the github repository we all use, what would be the right way to do that? If I run the command 'git pull github branchname' will that simply pull the branch into the current branch?

我习惯于从我正在处理的分支中运行 git pull 和其他命令。但是我已经搭建了一个开发服务器,几个人一起工作,所以我不想在做的时候切换分支。如果我想从我们都使用的 github 存储库更新开发服务器上的现有分支,那么正确的方法是什么?如果我运行命令 'git pull github branchname' 是否会简单地将分支拉入当前分支?

All of the git examples I can find seem to indicate that you run 'checkout branchname' first, then do the pull. I'm trying to avoid that. As I said, this is an existing branch and I just want to update to the latest version.

我能找到的所有 git 示例似乎都表明您首先运行 'checkout branchname',然后执行 pull。我试图避免这种情况。正如我所说,这是一个现有分支,我只想更新到最新版本。

回答by koral

I was looking for the same thing and finally found the answer that worked for me in another stackoverflow post: Merge, update, and pull Git branches without using checkouts

我一直在寻找同样的东西,最后在另一篇 stackoverflow 帖子中找到了对我有用的答案:合并、更新和拉出 Git 分支而不使用结帐

Basically:

基本上:

git fetch <remote> <srcBranch>:<destBranch>

git fetch <remote> <srcBranch>:<destBranch>

回答by Marcin T.P. ?uczyński

I had the very same issue with necessity to commit or stash current feature changes, checkout masterbranch, do pullcommand do get everything from remote to local masterworkspace, then switch again to a feature branch and perform a rebaseto make it up-to-date with master.

我遇到了同样的问题,需要提交或存储当前功能更改,签出分支,执行pull命令将所有内容从远程master工作区获取到本地工作区,然后再次切换到功能分支并执行 arebase以使其保持最新状态掌握。

To make this all done, keep the workspace on feature branch and avoid all the switching, I do this:

为了完成这一切,将工作区保留在功能分支上并避免所有切换,我这样做:

git fetch origin master:master

git fetch origin master:master

git rebase master

git rebase master

And it does the trick nicely.

它很好地解决了这个问题。

回答by torek

If you want the localbranch tips to get re-pointed after git fetch, you need some additional steps.

如果您希望在之后重新指向本地分支提示git fetch,则需要一些额外的步骤。

More concretely, suppose the github repo has branches D, B, C, and master(the reason for this odd branch-name-set will be clear in a moment). You are on host devhostand you are in a repo where originis the github repo. You do git fetch, which brings over all the objects and updates origin/D, origin/B, origin/C, and origin/master. So far so good. But now you say you want something to happen, on devhost, to localbranches D, B, C, and/or master?

更具体地说,假设 github 存储库有分支DBCmaster(这个奇怪的分支名称集的原因稍后会很清楚)。您在主机上,devhost并且您在origingithub 存储库所在的存储库中。你这样做git fetch,它带来了所有的对象和更新origin/Dorigin/Borigin/C, 和origin/master。到现在为止还挺好。但是现在你说你想要的东西的事情发生,就devhost当地的分支机构DBC,和/或master

I have these obvious (to me anyway) questions:

我有这些明显的(无论如何对我来说)问题:

  1. Whydo you want the tips of allbranches updated?
  2. What if some branch (e.g., B) has commits that the remote (github) repo lacks? Should they be merged, rebased, or ...?
  3. What if you're on some branch (e.g., C) and the work directory and/or index are modified but not committed?
  4. What if the remote repo has new branches added (A) and/or branches deleted (D)?
  1. 为什么要更新所有分支的提示?
  2. 如果某个分支(例如,B)提交了远程(github)repo 缺少的提交怎么办?他们应该合并,重新定位,还是......?
  3. 如果您在某个分支(例如,C)并且工作目录和/或索引已修改但未提交怎么办?
  4. 如果远程仓库添加了新分支 ( A) 和/或删除了分支 ( D) 怎么办?

If the answer to (1) is "because devhostis not actually for development, but rather is a local mirror that simply keeps a locally-available copy of the github repo so that all our actual developers can read from it quickly instead of reading slowly from github", then you want a "mirror" rather than a "normal" repo. It should not have a work directory, and perhaps it should not accept pushes either, in which case the remaining questions just go away.

如果 (1) 的答案是“因为devhost实际上不是用于开发,而是一个本地镜像,它只是保留 github 存储库的本地可用副本,以便我们所有实际的开发人员都可以快速读取它,而不是从github”,那么你想要一个“镜像”而不是“普通”的repo。它不应该有一个工作目录,也许它也不应该接受推送,在这种情况下,剩下的问题就会消失。

If there is some other answer, (2-4) become problematic.

如果有其他答案,(2-4) 就会有问题。

In any case, here's a way to tackle updating local refs based on remote refs (after running git fetch -pfor instance):

无论如何,这里有一种方法可以解决基于远程引用更新本地引用的问题(git fetch -p例如在运行后):

for ref in $(git for-each-ref refs/remotes/origin/ --format '%(refname)'); do
    local=${ref#refs/remotes/origin/}
    ... code here ...
done

What goes in the ... code here ...section depends on the answers to questions (2-4).

... code here ...部分的内容取决于问题 (2-4) 的答案。

回答by SzG

Use

git fetch

instead. It updates the remote refs and objects in your repo, but leaves the local branches, HEAD and the worktree alone.

反而。它更新你的 repo 中的远程引用和对象,但保留本地分支、HEAD 和工作树。

回答by Anuj Aneja

EDIT: Use 'git pull' It will fetch all the branches from the repo and Also update to latest if branch exits on the local system for current branch only. Note: git pull is equivalent to fetch+merge which fetch all the branches but merges only the current branch.

编辑:使用 'git pull' 它将从 repo 中获取所有分支,如果分支仅在当前分支的本地系统上退出,它也会更新到最新版本。注意: git pull 相当于 fetch+merge ,它获取所有分支但只合并当前分支。