How can you git pull only the current branch?

GitConfigPull

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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 - Git

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.

Solution 3 - Git

UPDATE

The old answer i've add does not work any more :/. But after receive some upvotes about the PUSH version I've placed, to me means this answer is actually helping someone that ending coming here from search engines, so I'll keep this answer.

Try this for the new version of git:

$ git config --global push.default current

Solution 4 - Git

The --set-upstream flag is deprecated and will be removed. Hence, use --track or --set-upstream-to

Example: If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=<remote>/<branch> develop

Solution 5 - Git

Yes, there is a config which can be changed in .gitconfig, for example:

[push]
  default = current

which would push the current branch to update a branch with the same name on the receiving end.

Check by:

git config --global --get push.default

See: git-config.

Solution 6 - Git

Here's a git alias that doesn't assume the remote is origin and handles if the branch isn't tracking a remote.

pullh = "!f() { set -e; set -o pipefail; arr=($(git rev-parse --abbrev-ref @{u} | sed 's/\\//\\n/')); git pull ${arr[0]} ${arr[1]}; }; f"

(Disclaimer: I'm very much a bash novice and the above could probably be simplified a lot.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionqodeninjaView Question on Stackoverflow
Solution 1 - GitSeth RobertsonView Answer on Stackoverflow
Solution 2 - GitaykeView Answer on Stackoverflow
Solution 3 - GitBruno CasaliView Answer on Stackoverflow
Solution 4 - GitbiniamView Answer on Stackoverflow
Solution 5 - GitkenorbView Answer on Stackoverflow
Solution 6 - GitscobiView Answer on Stackoverflow