1

After doing

git init
git remote add origin <some_origin>

You can's simply commit and push, since it says

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 origin master

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

Is there a way of setting the upstream branch without using git push?

Eg. something like:

git upstream origin master
  • 2
    `git branch --set-upstream-to=some-branch` – eftshift0 Feb 01 '23 at 08:10
  • 1
    "*You can't simply commit and push... without using git push*" -> Isn't there a contradiction in your question? In your first scenario, you *do* push. – Romain Valeri Feb 01 '23 at 08:26
  • eftshoft0 gave the answer : there is an option in `git branch` for that. There is a sanity check, which only allows to set it to a known branch, so you technically need to run `git fetch origin` at least once if you want to set `git branch -u origin/that_branch` – LeGEC Feb 01 '23 at 08:39
  • @eftshift0 That's not working for me. I've tried: `git branch --set-upstream-to=master`, `git branch --set-upstream-to=origin master`, `git branch --set-upstream-to=origin/master` and `git branch --set-upstream-to=remotes/origin/master` and it says the upstream branch doesn't exist or some other error. – Diego Alonso Feb 01 '23 at 08:48
  • `git branch --set-upstream-to=origin/master` should be the way forward for you.... but the branch must be known to exist for git to allow this to take place. Does the branch exist in the remote repo? If it does exist, did you fetch? – eftshift0 Feb 01 '23 at 09:03
  • @eftshift0 No, it's a new, blank, newly-created Github repo. How could I make that work in this case? – Diego Alonso Feb 01 '23 at 09:20
  • If you mean to create it from your local branch, just push with `-u`... if the branch has the same name as what you have in the remote: `git push some-remote some-branch -u`, then the upstream will be set for your local `some-branch` to `some-remote/some-branch` – eftshift0 Feb 01 '23 at 09:21
  • But that's exactly what I'm asking: how do I do that *without* `git push -u`. – Diego Alonso Feb 01 '23 at 09:22
  • 1
    **The branch needs to be known to git**... if the branch does not exist...... Anyway, if you feel like you are in the hacking mode, edit `.git/config` and set the upstream by providing `remote` and a `merge` setting for the local branch. So, a line with `remote = some-remote` and another with `merge = refs/heads/some-branch`. And you are on your own. :-) – eftshift0 Feb 01 '23 at 09:58
  • @DiegoAlonso: are you doing this operation (creating a local repo and linking it to a fresh, empty github repo) on a regular basis ? or are you asking out of curiosity ? – LeGEC Feb 01 '23 at 10:13

0 Answers0