nha
nha

Reputation: 18005

Why do I need to do `--set-upstream-to` all the time when pulling?

I typically have to clone remote branches that I do not have in my git repo (ex. from coworkers)

While there is an easy setting for git push (see Why do I need to do `--set-upstream` all the time?) I haven’t found the same to be true for git pull.

Right now my workflow is:

Alas I am greeted with:

There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details.

git pull <remote> <branch>

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

git branch --set-upstream-to=origin/<branch> branch123

How can I make this easier?

Upvotes: 2

Views: 536

Answers (4)

Frosted Developer
Frosted Developer

Reputation: 129

Typically, you do:

git branch -u %remotename%/%branchname%

Replace %remotename% with your remote name which you set up, which mostly is called "origin". Replace %branchname% with the remote branch name.

Upvotes: 1

rubenvb
rubenvb

Reputation: 76579

If you are checking out a branch that only exists as origin/branchName (so git branch -l does not list branchName without the origin) like so:

git checkout branchName

You will create a new local branch that tracks origin/branchName automatically. Git should also inform you of this:

Branch branchName set up to track remote branch branchName from origin.
Switched to a new branch 'branchName'

When you use the -b flag, you'll need to add the -t flag as well to set up the tracking in one go:

git checkout -b branchName -t origin/branchName

This command is really only useful if you want your local branch to have a different name from the one on origin. In the simple case, just leave out the -b.

Alternatively, you can use the -u argument as mentioned in @Tobb's answer on your first push. This essentially does the same automagic mapping to the remote's branch of the same name that the plain git checkout does.

Upvotes: 2

Tobb
Tobb

Reputation: 12205

When you use the -b-flag for checkout you actually create a new local branch, not linked to any remote branch. The first time you push your new branch you can link it to the new remote branch by doing git push -u. If you are checking out an existing branch, just drop the -b.

Upvotes: 3

nha
nha

Reputation: 18005

Here is what I came up with:

  • oh I have to pull branch123 from projectABC from github
  • cd projectABC && git checkout -b branch123
  • git track <= this is the interesting bit
  • git pull

With the following alias in my .gitconfig

  track = "!f(){ branch=$(git name-rev --name-only HEAD); cmd=\"git branch --set-upstream-to ${1:-origin}/${2:-$branch} $branch\"; $cmd; }; f"

Credit: https://andre.arko.net/2012/05/29/track-remote-git-branches-with-ease/

Upvotes: 0

Related Questions