你怎么能 git pull 只拉当前分支?

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

How can you git pull only the current branch?

gitconfigpull

提问by qodeninja

Is there a config way to set this up without having to specify which branch?

有没有一种配置方法来设置它而不必指定哪个分支?

采纳答案by Seth Robertson

Git already only pulls the current branch. If you have branch set up as a tracking branch, you do not need to specify the remote branch. git branch --set-upstream localbranch reponame/remotebranchwill set up the tracking relationship. You then issue git pull [--rebase]and only that branch will be updated.

Git 已经只拉取当前分支。如果您将分支设置为跟踪分支,则无需指定远程分支。 git branch --set-upstream localbranch reponame/remotebranch将建立跟踪关系。然后你发出git pull [--rebase],只有那个分支会被更新。

Of course, all remote tracking branches and all refs for the remote will be updated, but only your local tracking branch will be modified.

当然,所有远程跟踪分支和远程的所有引用都将被更新,但只会修改您的本地跟踪分支。

Useful Bash alias to cut down on typing of this common operation:

有用的 Bash 别名可以减少这种常见操作的输入:

# Add an alias to pulling latest git changes into your same branch
alias pullhead='git pull origin $(git rev-parse --abbrev-ref HEAD)'

Powershell function that does the same:

执行相同操作的 Powershell 函数:

Function pullhead {
    $cur_head="$(git rev-parse --abbrev-ref HEAD)"
    & git pull origin ${cur_head}
}

回答by ayke

I just did it this way:

我就是这样做的:

git pull origin "$(git branch | grep -E '^\* ' | sed 's/^\* //g')"

or

或者

git pull origin $(git rev-parse --abbrev-ref HEAD)

This extracts the current branch from git branch, and pulls that branch from remote origin.

这将从 中提取当前分支git branch,并从远程源中拉出该分支。

Note, that like Seth Robertson said, when no arguments are given only the current branch is modified but all remote branches are fetched. I don't want to fetch all remote branches, so I did it this way.

请注意,就像 Seth Robertson 所说的那样,当没有给出参数时,只会修改当前分支,但会获取所有远程分支。我不想获取所有远程分支,所以我这样做了。

回答by Bruno Casali

UPDATE

更新

The old answer i've add does not work any more :/. But after receive some upvotes about the PUSH version I've placed, to me means this answer is actually helping someone that ending coming here from search engines, so I'll keep this answer.

我添加的旧答案不再起作用:/。但是在收到关于我放置的 PUSH 版本的一些赞成票后,对我来说意味着这个答案实际上是帮助那些从搜索引擎来到这里的人,所以我会保留这个答案。

Try this for the new version of git:

在新版本的 git 上试试这个:

$ git config --global push.default current

回答by kenorb

Yes, there is a config which can be changed in .gitconfig, for example:

是的,有一个可以在 中更改的配置.gitconfig,例如:

[push]
  default = current

which would push the current branch to update a branch with the same name on the receiving end.

这将推动当前分支以更新接收端具有相同名称的分支。

Check by:

检查方式:

git config --global --get push.default

See: git-config.

请参阅:git-config

回答by biniam

The --set-upstreamflag is deprecated and will be removed. Hence, use --trackor --set-upstream-to

--set-upstream标志已弃用并将被删除。因此,使用--track--set-upstream-to

Example: If you wish to set tracking information for this branch you can do so with:

示例:如果您希望为此分支设置跟踪信息,您可以这样做:

git branch --set-upstream-to=<remote>/<branch> develop