4

I followed vogella tutorial about GIT, section 17 exercice "Working with (local) remote repository". when executing step 17.3, I got this error :

The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream ../remote-repository.git master

The steps executed are:

   $repo01>git clone --bare . ../remote-repository.git
   Cloning into bare repository '../remote-repository.git'...
   done.
   $mkdir repo02
   $\repo02>git clone ../remote-repository.git .
   Cloning into '.'...
   done.
   $\repo01>git status
   On branch master
   Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test01
        modified:   test02.txt

    no changes added to commit (use "git add" and/or "git commit -a")
  $repo01>git commit -a -m "Some changes"
  $\repo01>git push ../remote-repository.git
  fatal: The current branch master has no upstream branch.
  To push the current branch and set the remote as upstream, use

    git push --set-upstream ../remote-repository.git master

What can be the reason?

SarahData
  • 769
  • 1
  • 12
  • 38

2 Answers2

4

As git tells you: The current branch master has no upstream branch. Thus, git does not know to which branch of your remote-repository it is supposed to push your changes.

I can not reproduce this; if I execute your steps, the upstream branch is set. However, to fix this you can do exactly what git tells you: git push --set-upstream ../remote-repository.git master. This tells git that the branch you're currently working on (your local master) pulls from and pushes to the master branch of your remote-repository by default. If this is once set, push will automatically know where to push to in the future.

Did you do anything else than the commands you provided in your question?

Edit: I probably cannot reproduce this because of my custom settings of push.default: I would advice to set it to current with $ git config --global push.default current. This means that git pushes only the current branch and automatically pushes to a remote branch with the same name if it exists. See here at the push.default section for details.

kowsky
  • 12,647
  • 2
  • 28
  • 41
  • No I didn't anything else than the commands that the tutorial provided, so I don't why the upstream branch is not set automatically. The proposed git command worked for me. – SarahData Mar 01 '17 at 10:53
  • See my edit above for an explanation. Depending your `push.default` configuration, your upstream branch was obviously not set automatically. – kowsky Mar 01 '17 at 10:58
  • I found this in documentation : "When pushing to a remote that is different from the remote you normally pull from, work as current. This is the safest option and is suited for beginners. This mode has become the default in Git 2.0". And my git version is: 2.9.2. So it should be already set as default value. – SarahData Mar 01 '17 at 11:16
  • You can check with `$ git config --global push.default`. – kowsky Mar 01 '17 at 12:37
  • @SarahM: for the `simple` setting of `push.default` to act like the `current` setting, there must still be a configured upstream. (More precisely, there must be a configured `branch..remote`.) I would not advise changing `push.default` if you're still learning Git; just use the `--set-upstream` option, which may be abbreviated `-u`, or use `git push :`, i.e., write the branch name twice with a colon in between. The two-names-with-colon form means "my branch is the name on the left, please ask them to set their branch using the name on the right." – torek Mar 01 '17 at 19:59
-1
git push origin master

origin is your remote git branch name, master is your local branch.

ashanrupasinghe
  • 758
  • 1
  • 10
  • 22
  • 1
    `origin` is an alias in your locale for the remote repository – smarber Mar 01 '17 at 10:21
  • remote origin in my case points to the cloned repository, which is repo01, isn't it? What it means master is my local branch? you mean repo01 master? Otherwise, I executed the command but didn't work, I get: `code` 'origin' does not appear to be a git repository fatal: Could not read from remote repository. – SarahData Mar 01 '17 at 10:24
  • 1
    http://stackoverflow.com/questions/23401652/fatal-the-current-branch-master-has-no-upstream-branch – ashanrupasinghe Mar 01 '17 at 10:38
  • 1
    http://stackoverflow.com/questions/6089294/why-do-i-need-to-do-set-upstream-all-the-time – ashanrupasinghe Mar 01 '17 at 10:39