Associar Projeto com GitHub

22 respostas
tiagotsa

Ao comitar projeto para GitHub apresenta essa mensagem de erro:
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

22 Respostas

Lucas_Camara

Parece que vc já tem o remote adicionado como origin porém a branch master local ainda não está associada com a master do remoto.

Pela mensagem, basta vc executar esse comando que deve funcionar:

git push --set-upstream origin master
tiagotsa

Deu esse erro:

error: src refspec master does not match any
error: failed to push some refs to ‘github.com:Tiagotsa/dsmovie.git’

Lucas_Camara

Ah sim, o Github não está mais usando o nome master como branch. Agora vc deve usar main. Que é a branch que está no seu repositório do github.

Parece que isso é por conta da referência com a escravatura (master-slave). E o nome main fica mais apropriado.

tiagotsa

Ainda continua com erro, segue todo processo:

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git commit -m "Projeto React"
[main c1f06aa] Projeto React
 1 file changed, 1 insertion(+), 1 deletion(-)

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:master

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$  git push origin HEAD:master
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 545 bytes | 272.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To github.com:Tiagotsa/dsmovie.git
   40cd3fa..c1f06aa  HEAD -> master

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git push origin HEAD
To github.com:Tiagotsa/dsmovie.git
 ! [rejected]        HEAD -> main (non-fast-forward)
error: failed to push some refs to 'github.com:Tiagotsa/dsmovie.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git pull
Already up to date.

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
Lucas_Camara

Posta o resultado completo do comando git status pra gente ver.

tiagotsa

é esse resultado mesmo

Lucas_Camara

O git status deve retornar algo parecido com isso:

lucas@lucas-G3-3579:~/Documentos/git/flutter/probabilidades_brasileirao$ git status
No ramo main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (utilize "git add <arquivo>..." para atualizar o que será submetido)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   android/app/build.gradle
	modified:   android/build.gradle
	modified:   pubspec.lock

nenhuma modificação adicionada à submissão (utilize "git add" e/ou "git commit -a")
tiagotsa

não entendi muito bem, como usar o comando, fiz assim.
git restore <index.html>
bash: syntax error near unexpected token `newline’

Lucas_Camara

não é git restore, é git status.

Abra o terminal, digite git status, copie o resultado, e poste aqui pra gente.

tiagotsa

git status
On branch main
Your branch is ahead of ‘origin/master’ by 1 commit.
(use “git push” to publish your local commits)

nothing to commit, working tree clean

tiagotsa

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git status
On branch main
Your branch is ahead of ‘origin/master’ by 1 commit.
(use “git push” to publish your local commits)

nothing to commit, working tree clean

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use

git push origin HEAD:master

To push to the branch of the same name on the remote, use

git push origin HEAD

To choose either option permanently, see push.default in ‘git help config’.

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)

$ git push origin HEAD:main

To <a href="http://github.com">github.com</a>:Tiagotsa/dsmovie.git

! [rejected]        HEAD  main (non-fast-forward)

error: failed to push some refs to <a href="http://github.com">github.com</a>:Tiagotsa/dsmovie.git

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: git pull …’) before pushing again.

hint: See the Note about fast-forwards in git push --help’ for details.
tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)

$ git push origin HEAD

To <a href="http://github.com">github.com</a>:Tiagotsa/dsmovie.git

! [rejected]        HEAD  main (non-fast-forward)

error: failed to push some refs to <a href="http://github.com">github.com</a>:Tiagotsa/dsmovie.git

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: git pull …’) before pushing again.

hint: See the Note about fast-forwards in git push --help’ for details.

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$

Lucas_Camara

Ah sim, sua branch local main está associada com a branch remota master. Tente executar esse comando para alterar a branch associada:

git branch main --set-upstream-to origin/main
tiagotsa

erro:

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git branch main --set-upstream-to origin/main
branch ‘main’ set up to track ‘origin/main’.

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git status
On branch main
Your branch and ‘origin/main’ have diverged,
and have 7 and 1 different commits each, respectively.
(use “git pull” to merge the remote branch into yours)

nothing to commit, working tree clean

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git add .

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git status
On branch main
Your branch and ‘origin/main’ have diverged,
and have 7 and 1 different commits each, respectively.
(use “git pull” to merge the remote branch into yours)

Changes to be committed:
(use “git restore --staged …” to unstage)
modified: index.html

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$ git commit -m “Projeto”
[main dc088d0] Projeto
1 file changed, 1 insertion(+), 1 deletion(-)

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)

$ git push

To <a href="http://github.com">github.com</a>:Tiagotsa/dsmovie.git

! [rejected]        main  main (non-fast-forward)

error: failed to push some refs to <a href="http://github.com">github.com</a>:Tiagotsa/dsmovie.git

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: git pull …’) before pushing again.

hint: See the Note about fast-forwards in git push --help’ for details.

tiago@DESKTOP-9ENN7S4 MINGW64 /c/projetos/dsmovie/vendas (main)
$

Lucas_Camara

A branch main do remoto possui commits que sua main local não tem. Tem que ver o que está valendo, se é as mudanças que vc fez localmente, ou os commits que estão no remoto.

tiagotsa

como faz isso?

Lucas_Camara

Veja lah no github se as mudanças que estão lá são as que vc quer, em vez das mudanças locais. Se for, vc pode descartar as mudanças locais usando o git reset.

Agora se as mudanças que estão localmente forem mais importantes do que as que estão lá no github vc pode forçar o push com a opção -f: git push -f origin main.

Outra coisa seria fazer o pull, ver se vai dá conflito e, se der, vc corrige o conflito e faz o push normalmente.

Lucas_Camara

Pelo que vi, no github, só tem um arquivo index.html. Vc tem mais arquivos criados/alterados localmente?

tiagotsa

sim

Lucas_Camara

Então vc pode forçar o push mesmo.

tiagotsa

Mano deu certo, obrigado!

tiagotsa

estava quebrando a cabeça com isso
valeu…

Lucas_Camara

Muito bom!

Criado 14 de março de 2022
Ultima resposta 14 de mar. de 2022
Respostas 22
Participantes 2