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?
The solution to this error is to either create a local and remote master branch that you can push the commit to or to push the commit to an existing branch – maybe main . These commands will create a master branch locally. And by pushing to origin master , the master branch will also be created remotely.
The “src refspec master does not match any” error occurs if you have forgotten to add the files you have changed to a commit and try to push those changes to a remote repository before you make the first commit in your repository.
The steps to follow in order to push new Git branches to remote repos such as GitHub, GitLab or Bitbucket are as follows: Clone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch.
Git Push Origin pushes all the branches to the main branch. Git Push Origin Master pushes your master branch to the origin. Command for this: git push origin.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With