19

I know that I can fetch any remote branch to any local branch, but is there also some kind of shortcut to fetch just from the tracked remote branch to the current tracking local branch (without the need to specify the local and remote branch names explicitly)?

Motivation: I want to just fetch remote changes for the current branch to avoid getting (maybe large) changes from currently irrelevant branches. I will merge/rebase later in a separate step.

Community
  • 1
  • 1
Mot
  • 28,248
  • 23
  • 84
  • 121

5 Answers5

10

Let's assume that you have origin remote with master, develop branches. You want to sync master but not develop.

You can do the following steps:

git fetch origin
git merge origin/master

UPDATE: in case of only branch have to be fetched:

git fetch origin master
git merge FETCH_HEAD
kalys.osmonov
  • 504
  • 3
  • 14
  • 6
    `git fetch origin` fetches all. I **only** want to fetch changes from the **tracked branch**. – Mot Jul 05 '12 at 19:43
  • You need to add the value to choose the branch you want from the specified repo, as per the manual. – Philip Oakley Jul 05 '12 at 22:27
  • Then try these lines: `git fetch origin master` `git merge FETCH_HEAD` – kalys.osmonov Jul 06 '12 at 02:53
  • 5
    I want to avoid using `origin` and `master` and instead use some kind of placeholders for the *current* branch, no matter whether it is `master`, `release-3` or `feature-xyz`. – Mot Jul 06 '12 at 10:39
  • 1
    If you use zsh, oh-my-zsh and its git plugin then you can use `ggpull`. https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/git.plugin.zsh#L64 – kalys.osmonov Sep 27 '12 at 10:54
  • Sadly `git fetch origin master` works *only if* you already have a local branch named `master`. If you're trying to fetch a branch whose name you (the human user) know but which has never been fetched on this computer before, that command will *not* work. Is there a foolproof way that works for both cases, but still fetches only the named branch (not all branches)? – Quuxplusone May 01 '15 at 21:42
  • Try this. ```git fetch origin your_branch:your_branch``` – kalys.osmonov May 02 '15 at 03:52
5
git fetch $(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | sed 's!/! !')
Flimm
  • 136,138
  • 45
  • 251
  • 267
  • Nice answer, anyone could help me puttin that into a git alias ? – Mathieu Westphal Sep 07 '17 at 10:41
  • I couldn't find how to escape sed stuff, so i did it like that : remandbranch = !sh -c 'echo "$(git config branch.`git name-rev --name-only HEAD`.remote) $(git name-rev --name-only HEAD)"' fetchres = !sh -c 'git fetch $(git remandbranch) && git reset --hard FETCH_HEAD' – Mathieu Westphal Sep 07 '17 at 11:09
3

Tying together a few things from the existing answers...

Get the name of the checked out branch:

git rev-parse --abbrev-ref HEAD

To answer your question, how do you fetch only the current branch? Here you go, as an alias called fetchthis. Use like git fetchthis.

git config --global alias.fetchthis '!bname=$(git rev-parse --abbrev-ref HEAD); git fetch origin $bname'

Tested in Git for Windows:

$ git --version
git version 2.25.1.windows.1
Mark
  • 3,357
  • 30
  • 31
Ryan Rodemoyer
  • 5,548
  • 12
  • 44
  • 54
  • To get the name of the checked-out branch, I can use `git branch` and get the name of the branch with a `*` next to it, correct? isn't that the same? – tarekahf Aug 25 '22 at 20:22
0

Per https://stackoverflow.com/a/12142066/25192 - you can use this to find the name of the current branch:

git rev-parse --abbrev-ref HEAD

...then substitute this into the fetch command as the refspec.

Community
  • 1
  • 1
thewoolleyman
  • 592
  • 6
  • 13
-6

Git is a decentralized VCS. Whe you do a fetch, you're synch'ing the two repositories entirely. Branches aren't nothing but labels attached on specific commits. I guess you mean git fetch which doesn't do any merges or stuff like that to any particular branches.

http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html

greut
  • 4,305
  • 1
  • 30
  • 49
  • 2
    The fact that git is decentralized has nothing to do with the question that was asked, and syncing with another repository does _not_ require that you sync them entirely including all branches. You can sync whatever head reference you want, and only those references will need to be transfered. – Caleb Aug 05 '16 at 12:01