英文:
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='%(refname:short)' 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论