Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git fetch only for current branch

Tags:

git

fetch

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.

like image 453
Mot Avatar asked Jul 05 '12 15:07

Mot


People also ask

Can you git fetch a specific branch?

Git fetch commands and options Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository. Same as the above command, but only fetch the specified branch. The --dry-run option will perform a demo run of the command.

How do I pull just the current branch?

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.

Does git fetch update current branch?

You can do a git fetch at any time to update your remote-tracking branches under refs/remotes/<remote>/ . This command does not change any of your own local branches under refs/heads , and is safe to do without changing your working copy.

How do I fetch a specific remote branch?

just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.


1 Answers

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
like image 79
Ryan Rodemoyer Avatar answered Sep 28 '22 03:09

Ryan Rodemoyer