如何使用git pull只获取当前分支?

huangapple go评论58阅读模式
英文:

How can I git pull to only fetch the current branch?

问题

After looking at this post I couldn't find any answers that worked, aside from perhaps overriding the bash command.

I would like git pull to only fetch branches that I have a local branch for, if not just the current one. I prefer not to manually git fetch origin develop then git rebase origin/develop


I don't want these random remote branches polluting my git lg command:

# .gitconfig
[alias]
    lg = log --all --decorate --branches --oneline --graph --color --date=short
英文:

After looking at this post I couldn't find any answers that worked, aside from perhaps overriding the bash command.

I would like git pull to only fetch branches that I have a local branch for, if not just the current one. I prefer not to manually git fetch origin develop then git rebase origin/develop


I don't want these random remote braches polluting my git lg command:

# .gitconfig
[alias]
    lg = log --all --decorate --branches --oneline --graph --color --date=short

</details>


# 答案1
**得分**: 2

你可以通过向`pull`传递参数来指示它仅获取你的本地分支,例如:

```shell
git pull origin develop
# 或者如果你总是跟踪同名的分支,可以使用别名:
[alias]
    p = !git pull origin $(git branch --show-current)

这样你只会获取并合并(或者变基)你指定的分支。

至于你的所有本地分支,我认为你不能改变git pull的行为以一条命令同时获取所有 只有 你的本地分支。你可以创建一个别名来循环遍历每个本地分支的名称并获取该分支,可能像这样(在bash中):

git for-each-ref --format='%(refname:short)' refs/heads/ | xargs git fetch origin

话虽如此,由于你希望这样做的原因是为了避免在你的lg别名中打印出所有远程分支,你可以尝试简单地从你的别名中删除--all选项,该选项表示包括所有本地和远程分支。你已经有的--branches选项(目前是多余的),表示包括所有你的本地分支。删除--all后,你应该能够简单地运行git fetch(或让pull获取所有分支),你的lg别名仍然会像你一直在做的那样工作。需要注意的是,这将不再显示你的本地分支的相应远程版本,所以这可能不完全符合你的要求。

英文:

You can instruct pull to fetch only your local branch, by passing the arguments to pull, for example:

git pull origin develop
# or if you always track a branch of the same name, perhaps an alias like:
[alias]
    p = !git pull origin $(git branch --show-current)

That way you will only fetch and merge (or rebase) the branch you specify.

As for "all" of your local branches, I don't think you can change the behavior of git pull to fetch all of, and only, your local branches in a single command. You could create an alias to loop through each local branch name and fetch that branch, perhaps like this (in bash):

git for-each-ref --format=&#39;%(refname:short)&#39; refs/heads/ | xargs git fetch origin

That being said, since the reason you wish to do this is to avoid printing out all the remotes in your lg alias, you could try simply removing the --all option from your alias, which says to include all local and remote branches. The --branches option which you also already have (which is currently redundant), says to include all of your local branches. After removing --all, you should be able to simply git fetch (or let pull fetch all branches), and your lg alias will still work (similarly) to what you've been doing. A caveat here is that this will no longer also show the corresponding remote versions of your local branches, so that may not be exactly what you want.

答案2

得分: 0

Git已经只拉取当前分支。如果你已经将分支设置为跟踪分支,就不需要指定远程分支。git branch --set-upstream localbranch reponame/remotebranch将建立跟踪关系。然后,你可以执行git pull [--rebase],只有该分支会被更新。

英文:

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

huangapple
  • 本文由 发表于 2023年3月4日 02:52:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定