Как исправить ошибку fatal the current branch my branch has no upstream branch

I’m trying to push one of my projects to github, and I keep getting this error:

peeplesoft@jane3:~/846156 (master) $ git push

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

So I tried it and got this:

peeplesoft@jane3:~/846156 (master) $ git push --set-upstream origin master

fatal: Authentication failed

Another stackoverflow thread suggested I try the following, with disappointing results.

peeplesoft@jane3:~/846156 (master) $ git push -u origin master

fatal: Authentication failed

Then I tried this:

peeplesoft@jane3:~/846156 (master) $ git config remote.origin.push HEAD

peeplesoft@jane3:~/846156 (master) $ git push

fatal: Authentication failed

Any hints?

alex's user avatar

alex

477k200 gold badges876 silver badges980 bronze badges

asked May 1, 2014 at 3:38

user1524361's user avatar

7

You fixed the push, but, independently of that push issue (which I explained in «Why do I need to explicitly push a new branch?»: git push -u origin master or git push -u origin --all), you need now to resolve the authentication issue.

That depends on your url (ssh as in ‘git@github.com/yourRepo, or https as in https://github.com/You/YourRepo)

For https url:

If your account is protected by the two-factor authentication, your regular password won’t work (for https url), as explained here or here.

Same problem if your password contains special character (as in this answer)

If https doesn’t work (because you don’t want to generate a secondary key, a PAT: personal Access Token), then you can switch to ssh, as I have shown here.


As noted by qwerty in the comments, you can automatically create the branch of same name on the remote with:

git push -u origin head 

Why?

  • HEAD (see your .gitHEAD file) has the refspec of the currently checked out branch (for example: ref: refs/heads/master)
  • the default push policy is simple

Since the refpec used for this push is head: (no destination), a missing :<dst> means to update the same ref as the <src> (head, which is a branch).

That won’t work if HEAD is detached though.


Or you can use Git 2.37 (Q3 2022) and the new global option push.autoSetupRemote:

git config --global push.autoSetupRemote true
git push

answered May 1, 2014 at 4:42

VonC's user avatar

VonCVonC

1.2m516 gold badges4339 silver badges5154 bronze badges

4

Also you can use the following command:

git push -u origin master

This creates (-u) another branch in your remote repo. Once the authentication using ssh is done that is.

answered Jul 19, 2014 at 0:40

TantrajJa's user avatar

TantrajJaTantrajJa

1,9872 gold badges14 silver badges9 bronze badges

2

If you define the action git push it should take it if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line.

Just do it:

git config --global push.default current

then

git push

answered Sep 11, 2019 at 16:25

DariusV's user avatar

DariusVDariusV

2,60714 silver badges21 bronze badges

3

Apparently you also get this error message when you forget the --all parameter when pushing for the first time. I wrote

git push -u origin

which gave this error, it should have been

git push -u origin --all

Oh how I love these copy-paste errors …

answered Nov 27, 2014 at 8:55

TheEye's user avatar

TheEyeTheEye

9,2302 gold badges42 silver badges58 bronze badges

1

Please try this scenario

git push -f --set-upstream origin master

Ajmal Sha's user avatar

Ajmal Sha

8764 gold badges17 silver badges36 bronze badges

answered May 2, 2018 at 12:10

hoochanlon's user avatar

hoochanlonhoochanlon

2612 silver badges5 bronze badges

5

use this command first before you push in to the branch

git config --global push.default current

After executing the above command use git push command.

Paul Roub's user avatar

Paul Roub

36.3k27 gold badges83 silver badges92 bronze badges

answered Apr 19, 2021 at 13:50

KileMax's user avatar

KileMaxKileMax

2032 silver badges10 bronze badges

1

You need to configure the remote first, then push.

git remote add origin url-to-your-repo

Actual Instructions

answered Mar 30, 2017 at 7:51

Santosh Pillai's user avatar

Santosh PillaiSantosh Pillai

8,0891 gold badge31 silver badges27 bronze badges

It means that you don’t have your branch(the branch that you want to push) in your remote repository, in other words it does not exist in your remote repository(it wasn’t created yet)…
So use this Code:

git push -u origin 'your branch name'

this code will create your branch in your remote repository and will push it…

Enos Okello's user avatar

answered Apr 13, 2021 at 11:30

jamal zare's user avatar

jamal zarejamal zare

99911 silver badges12 bronze badges

1

on a very simple side, once you have other branches, you can’t just use for pushing a branch

git push

But you need to specify the branch now, even if you have checkout the branch you want to push, so

git push origin <feature_branch>

Where can be even the master branch

answered Nov 15, 2018 at 9:16

Abhishek's user avatar

AbhishekAbhishek

3,3174 gold badges32 silver badges51 bronze badges

Well, I was having the same trouble while uploading and I resolved it by
doing the same thing which it says to do:
Earlier I was trying to push through terminal to my repository in linux by https
like

git push https://github.com/SiddharthChoudhary/ClientServerCloudComputing.git

But was not getting any result and hence I went down deeper and tried:

git push --set-upstream https://github.com/SiddharthChoudhary/ClientServerCloudComputing.git master

And it worked. Thus then you will get prompted with username and password.
I also generated a token and instead of Password I pasted the token and thus, being done successfully.

  1. To generate a token go to your Github account and in Developer Settings and then create another token.
  2. After getting that, copy
    that token and paste in the password prompt when it’s been asked.

zar's user avatar

zar

11.2k13 gold badges92 silver badges176 bronze badges

answered Oct 16, 2018 at 16:47

Siddharth Choudhary's user avatar

2

I made the simple error of forgetting to commit:

git commit -m "first commit"

then git push origin master worked.

answered Jul 13, 2018 at 23:44

yl_low's user avatar

yl_lowyl_low

1,1692 gold badges17 silver badges26 bronze badges

I had the same problem

enter image description here

I resolved it that used below command

$ git branch --set-upstream develop origin/develop

and it will add a config in the config file in the .git folder.

enter image description here

answered Jan 7, 2019 at 2:33

bluetata's user avatar

bluetatabluetata

5596 silver badges12 bronze badges

1

First use git pull origin your_branch_name
Then use git push origin your_branch_name

answered Feb 16, 2017 at 3:20

FRabbi's user avatar

FRabbiFRabbi

1351 silver badge6 bronze badges

2

There is a simple solution to this which worked for me on macOS Sierra.
I did these two commands:

git pull --rebase git_url(Ex: https://github.com/username/reponame.git)
git push origin master

If it shows any fatal error regarding upstream after any future push then simply run :

git push --set-upstream origin master

answered Sep 10, 2017 at 12:18

theRana's user avatar

theRanatheRana

7049 silver badges10 bronze badges

If you constantly get the following git error message after attempting a git push with a new local branch:

fatal: The current branch has no upstream branch.

To push the current branch and set the remote as upstream, use

git push --set-upstream origin <branchname>

Then the issue is that you have not configured git to always create new branches on the remote from local ones.

The permanent fix if you always want to just create that new branch on the remote to mirror and track your local branch is:

git config --global push.default current

Now you can git push without any errors!

https://vancelucas.com/blog/how-to-fix-git-fatal-the-current-branch-has-no-upstream-branch/

fcdt's user avatar

fcdt

2,3315 gold badges12 silver badges26 bronze badges

answered Oct 19, 2020 at 23:07

Angie Cristina's user avatar

I was getting this error because I was trying to push the code without write access, I only had read access.

In Bitbucket, you can check access on the right side when you open your repo (Repository details) :

answered Sep 21, 2022 at 6:53

stackich's user avatar

stackichstackich

3,3433 gold badges14 silver badges39 bronze badges

For me, it was because I had deleted the hidden .git folder.

I fixed it by deleting the folder, re-cloning, and re-making the changes.

answered Sep 10, 2019 at 2:18

AmmarBaali's user avatar

1. A computer and your github associated. Use SSH. Computer code so you do not need to submit verified enter image description here

2. git can not manage empty folder. So you have to write such a readme.md saved in a file. Otherwise you will not find the file.

3. Your local project is nothing new projects move over. Please

git init

git remote add origin +"githublink"

git add .

git commit -m "" go again.

4. then git pull origin master (the key)

5. At last git push origin master (solve all problem).

http://my.oschina.net/psuyun/blog/123005 参考链接

Nishant Rajput's user avatar

answered Jul 15, 2016 at 13:30

王逍遥's user avatar

If you are trying to push your code direct to the master branch then use command

git push origin master

It helps me.

answered Apr 26, 2018 at 6:02

HitechZa's user avatar

I also got the same error.I think it was because I clone it and try to push back.
$ git push -u origin master
This is the right command.Try that

Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (8/8), 691 bytes | 46.00 KiB/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.

  • [new branch] master -> master
    Branch master set up to track remote branch master from origin.

    It was successful. Try to create new u branch 
    

answered May 11, 2018 at 16:08

Malsha's user avatar

I had the same problem, the cause was that I forgot to specify the branch

git push myorigin feature/23082018_my-feature_eb

answered Aug 23, 2018 at 9:25

Black's user avatar

BlackBlack

17.6k39 gold badges153 silver badges266 bronze badges

For me, I was pushing the changes to a private repo to which I didn’t had the write access.
Make sure you have the valid access rights while performing push or pull operations.

You can directly verify via

answered Jul 26, 2019 at 8:14

mayank dhawariya's user avatar

1

If you are on any branch, you can use this:

git push origin head -u

This will automatically create new branch of the same name on the remote.

answered May 15, 2020 at 10:17

Qwerty's user avatar

QwertyQwerty

28.3k21 gold badges108 silver badges130 bronze badges

commit your code using

git commit -m "first commit"

then config your mail id using

git config user.email "example@example.com"

this is work for me

Boken's user avatar

Boken

4,70710 gold badges32 silver badges42 bronze badges

answered Jul 14, 2020 at 8:11

Bala visakh's user avatar

The thing that helped me:
I saw that the connection between my directory to git wasn’t established —
so I did again:
git push -u origin main

ghilesZ's user avatar

ghilesZ

1,4921 gold badge17 silver badges30 bronze badges

answered Jan 28, 2021 at 12:12

mi re la's user avatar

Different case with same error (backing up to external drive), the issue was that I’d set up the remote repo with clone. Works every time if you set the remote repo up with bare initially

cd F:/backups/dir
git init --bare
cd C:/working/dir
git remote add backup F:/backups/dir
git push backup master

answered Feb 1, 2021 at 0:29

Dave's user avatar

DaveDave

1,3792 gold badges17 silver badges14 bronze badges

In my mind, this is just a wrong default git behavior.
Having reviewed all options support by git, also reviewed the relevant git code:

https://github.com/git/git/blob/140045821aa78da3a80a7d7c8f707b955e1ab40d/builtin/push.c#L188

The best solution I would suggest it simply override the push default command:

git config --global alias.pu 'push -u'

This basically changes the default behavior of push so that makes sense.

answered Mar 8, 2021 at 7:23

JAR.JAR.beans's user avatar

JAR.JAR.beansJAR.JAR.beans

9,5594 gold badges44 silver badges57 bronze badges

Encountered just about the same problem, but not from the master branch.
I tried to push two (2) branches to my remote repository, by using the $ git push command; which unfortunately threw up the:

fatal: The current branch <branch-name> has no upstream branch. To push the current branch and set the remote as upstream, use

 git push --set-upstream origin <branch-name>

I got it fixed with this command below:

$ git push -u origin --all

PS: The solution provided here should, i believe, make it easier for git to track out branches remotely; this could come in-handy someday, when working on projects with couple of branches.

answered Jul 25, 2021 at 22:14

Bravo Stack's user avatar

1

In my case, I have a local branch called Master, whereas master is on Github.
I simply rename my Master -> master and checkout to master.
And then push it. It works for me.

answered May 1, 2022 at 13:39

UC57's user avatar

UC57UC57

3594 silver badges16 bronze badges

It is better, Clone this repo newly. Maybe it just lost track.

answered Oct 31, 2022 at 5:16

Mujahidul Islam's user avatar

Fatal Git Error: Current branch has no upstream

Branch based development is one of Git’s most beneficial features. It allows developers to experiment in isolated development spaces that nobody else can see unless the branch is explicitly shared with others. Unfortunately, when a developer creates a new branch and pushes to their shared GitHub, GitLab or BitBucket repository, they often encounter the following, dreaded error:

[email protected]  /c/branch/push (new-branch)
$ git push origin
fatal: The current branch has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin new-branch

Set GitHub or GitLab as an upstream

Git’s upstream branch error messages is somewhat self-explanatory. Git’s error message also fairly succinctly describes the solution to the problem. Before you push a new branch to GitHub or GitLab, you must tell your local Git installation about the remote Git repository your branch should be pushed to. The simple solution to the current problem is easily solved by issuing the following Git push upstream command:

[email protected]  /c/branch/push (new-branch)
$ git push --set-upstream origin new-branch
Enumerating objects: 3, done.
* [new branch] new-branch -> new-branch
Branch 'new-branch' set up to track remote branch 'new-branch' from 'origin'.

A quick refresh of the account which represents origin in this upstream branch example shows the new branch on GitHub with the all of the content from the latest commit.

git no upstream branch error fix

Git’s ‘no upstream branch’ error can easily be fixed by setting the upstream branch to origin.

Branch has no upstream fix commands

The full set of commands performed in this Git push example that fixes the no upstream-branch error are as follows:

[email protected]  /c/branch/push (new-branch)
git push --set-upstream origin new-branch
cd git*
git switch -c new-branch
git branch -a
touch devo.html
git add .
git commit -m "Are we not men?"
git push --set-upstream origin new-branch
touch echo.html
git add .
git commit -m "Echo done"
git push origin

Most modern software development teams only have one central repository to which developers push and pull, and the alias for that central team repository is almost always origin. So this step seems set the upstream repository the first time a new Git branch is pushed seems both unnecessary and non-intuitive to many. But as confusing as this required step may be, just think of it as a reminder of the fact that Git is a very powerful, distributed version control tool that has the ability to push and pull to many upstream repositories, not just the one, central, GitHub or GitLab server most teams use for their project.

If you see the message “the current branch has no upstream” in your command window, it means you’ve tried to git push to a remote repository from a local branch that has not yet been connected to a remote branch. In other words there is no upstream branch. That means git doesn’t know where to push your changes to. In this post I’ll explain how to set the upstream branch to your local branch.

If you get an error when you git push, then it could well be because your current branch has not yet been ‘linked’ to a remote branch. The error you see will look like this:

$ git push
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

git push to remote repo, no upstream branch

How to set the upstream branch in git

Make sure you are already on the correct local branch, by using git status if you need to check which branch you are already on. Read one of my other posts if you need to use git switch to change branches. Then to set the upstream branch for that local branch, use these flags along with your git push command :

git push --set-upstream origin master

This would set the upstream branch of the current branch to ‘master’. I would always recommend keeping the name of your remote branch exactly the same as your local branch. So if your current local branch was called my-feature-branch then you would type this instead:

git push --set-upstream origin my-feature-branch

What is git push -u ?

Often in git there are several ways of writing each flag. In this scenario:

--set-upstream

is the same as

-u.

So these two commands will do exactly the same thing:

git push --set-upstream origin my-feature-branch

git push -u origin my-feature-branch

For any other information about flags to use with the git push command, read the official git push documentation. Once you’ve set the upstream branch, other commands will automatically use it too without you having to specify the remote branch name. For instance:

git pull

If your remote is not called the default name ‘ORIGIN’

What if your remote is not called the default name ‘ORIGIN’? Let’s say it’s called MY-REMOTE, and the branch is called master, then you would type the following command :

git push --set-upstream MY-REMOTE master

You can read more about what Git Origin is here.

Summary

You will now know what to do if you see the message “git fatal: The current branch has no upstream branch”, and have example commands to set the upstream branch in git, no matter what your branch name or remote name.

Get updates next time we post

Команда push требует чтобы вы сообщили гиту куда отправить и что отправить: git push <remote_repo> <local_branch>
Если только ранее это ещё не было сделано с -u.

Иначе вылезет ваша ошибка

➜ git push -u -v
fatal: The current branch root has no upstream branch.
To push the current branch and set the remote as upstream, use

git push —set-upstream origin root

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

Но можно включить режим автоматической привязки
git config push.autoSetupRemote true

Тогда простой git push даже без -u будет текущую новую ветку отправлять в remote origin с таким же именем ветки и автоматически связывать локальную и внешнюю ветку.

Почему сработало на другом компе?

Предположу что на другом компьютере git pull -u сработал потому, что он там ранее уже выполнялся с полными параметрами. Затем на гитхабе ветку удалили, но локальный репозиторий связь всё ещё «помнил»
Ситуацию бы прояснил вывод команды git branch -vv

* main        4e0c267 Commit Message
  some-branch 4e0c267 [origin/some-branch: отсутствует] Commit Message

Так показывается ситуация, когда связь есть, но внешней ветки уже нет.
Но предварительно нужно скачать обновления через git fetch --prune -v
только так локальный репозиторий узнает что связанная ветка удалена (отсутствует)

Если я переключусь на ветку some-branch и напишу git push, то гит отправит ветку в на прежнее место и тем самым восстановит её на гитхабе.

An upstream branch is a remote branch that is being tracked by a local branch.

Depending on how the push.default config is set, you may encounter the following error.

# attempt to push while the current branch 
# does not track any upstream branch
$ git push
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

The fix is rather simple and is already suggested by the failing command: use --set-upstream option when pushing

$ git push --set-upstream origin master
...
To https://url-to-remote-origin
   1561943..f8357d9  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

This is only needed once. From now on, our local master branch is set to pair with the upstream master branch. When we push, Git will know where to push the changes:

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 349 bytes | 349.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To https://url-to-remote-origin
   f8357d9..c29db9a  master -> master

Check the Full Official Documentation for more options.

Понравилась статья? Поделить с друзьями:

Не пропустите также:

  • Как найти ubermacht sentinel xs
  • Как правильно составить опись документов для сдачи в архив
  • Как найти качественные продукты
  • Как найти производную функции при изменении
  • Как исправить косяк на обоях

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии