Why use "git branch --unset-upstream"?

7

I created a project and left it hosted remotely. I have the following code:

So far so good. Now I do a branch called test, and I already do add / commit / push.

And now what I wanted to ask is: When I go back to the branch master, the message:

  

"Your branch is based on 'origin / master', but the upstream is gone.     (use "git branch --unset-upstream" to fixup) "

Why do I need to run this --unset-upstream ? What does this command mean? Did I do something wrong in the previous steps? Then the branchs end up working, but I do not like doing things in the right direction. So I want to understand what this --unset-upstream is and why he asks me to do it.

    
asked by anonymous 09.12.2016 / 19:33

1 answer

2

You have 2 options:

1 / Make a push to create the remote branch, then create the missing link:

git push origin master

2 / Break local binding between master and origin/master :

git branch --unset-upstream

The consequence of this is that in git fetch|pull|push , you will have to manually specify the local branch parameters and the remote branch to make the connection. Also, git status will no longer show the divergence between the local and remote branch.

To summarize, --unset-upstream only deletes a link locally, without erasing any data. You can then re-connect at any time:

git branch master --set-upstream-to=origin/master

Generally

When you create a remote repository ( git init --bare ), it does not have any branches, but when you clone the remote in a local repository, the master branch has an upstream link with origin/master , even if origin/master does not yet exist. Example:

# Criação d'um repository remoto
$ mkdir remote.git && cd remote.git && git init --bare && cd ..
Initialized empty Git repository in /tmp/remote.git/

# Criação de um clone locale
$ git clone remote.git local && cd local
Cloning into 'local'...
warning: You appear to have cloned an empty repository.
done.

# Criação do primeiro commit, necessario para ter um 'git status' normal.
$ echo "txt" > file.txt && git add file.txt && git commit -m "Initial commit"

# A mensagem vá aparecer
$ git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)
nothing to commit, working directory clean

# Verificando a ligacao 'master' -> 'origin/master'
$ git branch -vv
* master 8cbd8af [origin/master: gone] Initial commit

In your case, you have made other modifications, perhaps mistakes. First, it updates the data from the remote repository:

git fetch origin

It checks which remote branches it has in the remote repository, and checks the connections:

git remote show origin
git branch -vv

If you have created a bad branch, you can delete it with (example with branch test ):

git push origin :test
    
13.01.2017 / 15:03