How can you git pull only the current branch?
Solution 1:
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}
}
Solution 2:
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.