Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you git pull only the current branch?

Tags:

git

config

pull

Is there a config way to set this up without having to specify which branch?

like image 306
qodeninja Avatar asked May 18 '11 23:05

qodeninja


People also ask

How do I pull an existing branch in git?

In case you are using the Tower Git client, pulling from a remote is very easy: simply drag the remote branch and drop it onto your current HEAD in the sidebar - or click the "Pull" button in the toolbar.

Does git pull pull from current branch?

git pull updates your current local working branch, and all of the remote tracking branches.

Does git pull only affect current branch?

When we put all these facts together, we end up seeing that git pull 's second command is what affects the current branch. And, it only affects the current branch, because git merge and git rebase work only on the current branch. (The first command—the git fetch step—affects remote-tracking branches.


2 Answers

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.

Of course, all remote tracking branches and all refs for the remote will be updated, but only your local tracking branch will be modified.

Useful Bash alias to cut down on typing of this common operation:

# Add an alias to pulling latest git changes into your same branch alias pullhead='git pull origin $(git rev-parse --abbrev-ref HEAD)' 

Powershell function that does the same:

Function pullhead {     $cur_head="$(git rev-parse --abbrev-ref HEAD)"     & git pull origin ${cur_head} } 
like image 146
Seth Robertson Avatar answered Sep 22 '22 01:09

Seth Robertson


I just did it this way:

git pull origin "$(git branch | grep -E '^\* ' | sed 's/^\* //g')" 

or

git pull origin $(git rev-parse --abbrev-ref HEAD) 

This extracts the current branch from git branch, and pulls that branch from remote origin.

Note, that like Seth Robertson said, when no arguments are given only the current branch is modified but all remote branches are fetched. I don't want to fetch all remote branches, so I did it this way.

like image 28
ayke Avatar answered Sep 22 '22 01:09

ayke