Home > Software engineering >  Unable to publish changes from my branch - can only push changes to another branch, why?
Unable to publish changes from my branch - can only push changes to another branch, why?

Time:11-16

enter image description here

I have just created a new branch 'ie-eab-TD-IECC-25235-Junit'. I am trying to push my changes to the git repository on the same branch, however, this doesn't seem to be an option. --> See above picture which does not have the same branch name as an option for publishing commits to.

I can only push it to other branches, not the one I am on. Why is this happening and how do I push to the branch I am actually on?

My thoughts are that it is something to do with the upstream branch setup. But I am confused as I have followed the instructions for creating a new branch and then when I pull, thats when I have to set the upstream branch. And it might be this which is preventing me from pushing to itself (although I am not quite sure at the moment).

CodePudding user response:

The reason this is happening is because your branch is currently tracking a remote branch with a different name. You can see all of your local branches and their remote tracking branches with this command:

git branch -vv

To fix it, simply unset your currently checkout out branch with this command:

git branch --unset-upstream

Now most Git UI's will offer you the opportunity to push your branch out to a remote branch with the same name, and typically by default will also set it to track for you.

Why did this happen?

Either you set it to the wrong branch at creation, or perhaps when you pulled (as you mentioned).

When you created the branch from a different branch name, you may have elected to "track" the remote branch too. Many UIs have an option which is selectable for whether to "track" the branch, and if it is a different name, most of the time you want to deselect that option, to prevent the exact issue you had. If you are creating your branch from the command line, you can use the --no-track argument, for example:

git switch -c new-branch origin/existing-branch --no-track

or the equivalent old(er) way:

git checkout -b new-branch origin/existing-branch --no-track

If you set it when (or right before) you pulled, then you can just push first and set it then, or next time add an argument containing the branch to pull from, e.g. git pull origin some-branch-name instead of using just git pull, which without arguments, requires you to have an upstream set to something.

  • Related