14

I have a project with a few friends in GitLab, and there is of course the master branch, and there are some others too. When I cloned the repository, I created also an upstream with the command git remote add upstream ....

Then, I issued the git fetch upstream. Followed by git checkout upstream/test1. Now, if I type git branch -a, I get an output like this:

* (HEAD detached at upstream/test1)
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/upstream/test1
  remotes/upstream/master

This is all fine, but then I did some changes to the code in my upstream/test1 branch, and I want to push them to origin/test1 repository, I get the error message on the title. Please note that I follow the steps below to push:

git add .
git commit -m "Sample message"
git push -u origin test1

If I issue git show-ref, I get the following output:

refs/heads/master
refs/remotes/origin/HEAD
refs/remotes/origin/master
refs/remotes/upstream/test1
refs/remotes/upstream/master

I checked the following questions, but didn't find it helpful. Any ideas how to solve it?

typos
  • 5,932
  • 13
  • 40
  • 52

5 Answers5

15

You don't appear to have a local branch named test1. You have a remote branch named test1 associated with your upstream remote.

You shouldn't be editing the upstream/test1 branch directly. In fact, attempting to check that out should have yielded a warning:

$ git checkout upstream/test1

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

You should instead first check out a local branch that tracks the remote branch, which ought to look something like this:

$ git checkout test1
Branch test1 set up to track remote branch test1 from upstream by rebasing.
Switched to a new branch 'test1'

After doing this, an undecorated git push would push to the upstream remote, while git push origin test1 would push to your origin remote. Adding the -u flag there would switch the tracking branch so that instead of tracking upstream/test1, your branch would track origin/test1, so that future git pull and git push operations would refer to that remote branch (origin/test1) by default.

larsks
  • 277,717
  • 41
  • 399
  • 399
5

The following will create a new branch in local and in remote as well. I follow this when I create a new branch to work with.

git checkout master

git pull origin master

git checkout -b your-branch

Make the new changes

git push -u origin your-branch

Community
  • 1
  • 1
Akshay Kumar
  • 160
  • 7
0

In my case I had made changes in master branch and I was trying to push the changes to development branch and therefore getting the error. What I did was -

git checkout development

made my changes and then ran

git add .
git commit -m "my changes"
git push origin development
Filnor
  • 1,290
  • 2
  • 23
  • 28
vikash singh
  • 1,479
  • 1
  • 19
  • 29
0

For me the problem was I was trying to push to a branch name inside a flag/folder for types we have set up (feature / bugfix / etc) and the folder name wasn't showing because of the way I have my terminal prompt set up. I needed to run gpo feature/branch-name and was just absent-mindedly running gpo branch-name. (gpo is the alias I have set up for git push origin, btw).

Just in case this helps anyone.

toddmetheny
  • 4,405
  • 1
  • 22
  • 39
-1

I also faced the same issues but I was able to fix them later. $ git push –set-upstream origin master error: src refspec origin does not match any error: failed to push some refs to '–set-upstream' solution $ git remote add origin url $ git push -u --set-upstream origin master