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

Asked

Viewed 3,809 times

10

I created a project and left it hosted remotely. Dai clone it locally, create the remote (remote), create a test.php file and then add/commit/push.

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

And now here’s what I wanted to ask: when do I go back to branch master, the message comes:

"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 don’t like to do things by way. So I want to understand what this is --unset-upstream and why he asks me this.

  • 1

    crio o remoto (remote) what exactly do you do at this stage? When you clone a project (git clone) it already comes with the correct remote (origin). You’re adding another?

  • Ricardo, I believe you killed the riddle. As I started with the recent git, I imagined that whenever I made a clone, I should already create a remote for the source I cloned, and in fact this remote is already created by default. Dai repeated the process using the already standard clone (origin) and the "error" did not happen anymore. Thanks.

1 answer

5

You have 2 options:

1/ Push to create the remote branch, then create the missing link:

git push origin master

2/ Breaks the local connection between master and origin/master:

git branch --unset-upstream

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

To summarize, --unset-upstream only deletes a connection locally, without deleting any data. You can then and at any time redo the connection:

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

Generally

When you create a remote Repospository (git init --bare), it has no branch but when you clone the remote in a local Repository, branch master has a connection (upstream) with origin/master, even if origin/master does not exist yet. 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, such as errors. First, update the remote Repository data:

git fetch origin

Checks which remote branches have in remote Repository, and checks connections:

git remote show origin
git branch -vv

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

git push origin :test

Browser other questions tagged

You are not signed in. Login or sign up in order to post.