git 如何只拉取当前分支?

发布于 2024-11-08 23:31:57 字数 31 浏览 5 评论 0原文

有没有一种配置方法可以设置它而无需指定哪个分支?

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

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

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

发布评论

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

评论(6

没︽人懂的悲伤 2024-11-15 23:31:57

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

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

有用的 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 pullhead {
    $cur_head="$(git rev-parse --abbrev-ref HEAD)"
    & git pull origin ${cur_head}
}

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-to=reponame/remotebranch localbranch will set up the tracking relationship. You then issue git pull [--rebase] and only that branch will be updated.

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:

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

Function pullhead {
    $cur_head="$(git rev-parse --abbrev-ref HEAD)"
    & git pull origin ${cur_head}
}
无法言说的痛 2024-11-15 23:31:57

我只是这样做的:

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

或者

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

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

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

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.

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.

萌无敌 2024-11-15 23:31:57

更新

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

尝试这个新版本git:

$ git config --global push.default current

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.

Try this for the new version of git:

$ git config --global push.default current
初雪 2024-11-15 23:31:57

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

示例:
如果您想为此分支设置跟踪信息,可以使用以下命令:

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

The --set-upstream flag is deprecated and will be removed.
Hence, use --track or --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
方圜几里 2024-11-15 23:31:57

这是一个 git 别名,它不假设远程是origin,并在分支未跟踪远程时进行处理。

pullh = "!f() { set -e; set -o pipelinefail; arr=($(git rev-parse --abbrev-ref @{u} | sed 's/\\//\\n /')); git pull ${arr[0]} ${arr[1]}; f"

(免责声明:我是一个 bash 新手,上面的内容可能会简化很多.)

Here's a git alias that doesn't assume the remote is origin and handles if the branch isn't tracking a remote.

pullh = "!f() { set -e; set -o pipefail; arr=($(git rev-parse --abbrev-ref @{u} | sed 's/\\//\\n/')); git pull ${arr[0]} ${arr[1]}; }; f"

(Disclaimer: I'm very much a bash novice and the above could probably be simplified a lot.)

无人问我粥可暖 2024-11-15 23:31:57

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

[push]
  default = current

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

检查方式:

git config --global --get push.default

请参阅:git-config

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

[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.

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