0

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
neaumusic
  • 10,027
  • 9
  • 55
  • 83

2 Answers2

2

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.

neaumusic
  • 10,027
  • 9
  • 55
  • 83
TTT
  • 22,611
  • 8
  • 63
  • 69
  • Is there an easy way to subsequently perform a merge or rebase with the current checked out branch? This would perform a `pull` substitute which is what I'm ultimately looking for – neaumusic Mar 06 '23 at 18:55
  • @neaumusic You could make an alias that fetches just your current branch and then rebases onto (or merges in) the remote counterpart. Something like: `git fetch origin $(git branch --show-current) && git rebase @{u}` – TTT Mar 06 '23 at 19:20
  • @neaumusic it just occurred to me that my last comment can be simplified to use pull, which is actually closer to what you asked. ;) I updated the answer to reflect this. – TTT Mar 07 '23 at 17:20
0

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.

Mohammad Raoufi
  • 306
  • 3
  • 7
  • 2
    ... No, git pull updates all the remote references, and fetches all the commits associated with those, not just for the single branch as you write. – Kim Mar 04 '23 at 13:52
  • 1
    I think you may have misunderstood the question. `git pull` does a `git fetch` followed by a `git merge`. It's the `git fetch` part, with no arguments, that OP is trying to avoid. – TTT Mar 04 '23 at 15:23