threads / discuss / 23904

git branch --track to link existing local branch to remote?

Subject: git branch --track to link existing local branch to remote?

## tl;dr

4 messages between May 25, 2010 and May 26, 2010.

replies: 3people: 4as markdown or json

skillzero@gmail.com· May 25, 2010, 19:40 UTC · lore

Is there a way to link a local branch, which already exists, to a remote branch without editing the git config file? I create a local repository and committed some changes, created a remote repository, added the remote to the local repository, and then pushed the local branch to the remote. Now the local master points to the same commit as origin/master. However, the branches aren't linked in the config file so I can't just do git pull.

I know I can manually edit the config file (as the error message on git pull says), but it seems like 'git branch --track master origin/master' should do that automatically, especially if both branches point to the same commit. It currently complains that the branch master already exists and -f doesn't let me replace the current branch. So what I've had to do is create a temp branch, switch to it, do git branch -f --track master origin/master, and then delete the temp branch. Is there a better way?

Björn Steinbrink· May 25, 2010, 19:53 UTC · re: skillzero@gmail.com · lore

Re: git branch --track to link existing local branch to remote?

On 2010.05.25 12:40:40 -0700, skillzero@gmail.com wrote:
> However, the branches aren't linked in the config file so I can't just
> do git pull.
[...]
> So what I've had to do is create a temp branch, switch to it, do git
> branch -f --track master origin/master, and then delete the temp
> branch. Is there a better way?

With git v1.7.0 or newer: git branch --set-upstream master origin/master

Björn
Michael J Gruber· May 26, 2010, 08:46 UTC · re: Björn Steinbrink · lore

[PATCH] completion: --set-upstream option for git-branch

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
On that note, I'm still wondering what's wrong with this one ;)
 contrib/completion/git-completion.bash |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 545bd4b..57245a8 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -797,6 +797,7 @@ _git_branch ()
 		__gitcomp "
 			--color --no-color --verbose --abbrev= --no-abbrev
 			--track --no-track --contains --merged --no-merged
+			--set-upstream
 			"
 		;;
 	*)
-- 
1.7.1.345.g59eb63
Joshua Jensen· May 25, 2010, 19:55 UTC · re: skillzero@gmail.com · lore

Re: git branch --track to link existing local branch to remote?

skillzero@gmail.com wrote:
> Is there a way to link a local branch, which already exists, to a
> remote branch without editing the git config file?
git branch --set-upstream
Josh

← back to recent threads