13

I've been working on a local clone of a remote git repository, committing my changes to my local master branch. Now, I want to push my commits to the remote repository. However, I want to keep my local commits separate from the remote master branch, so that I don't break anything. How can I push my local commits to a new remote branch?

Daenyth
  • 35,856
  • 13
  • 85
  • 124
Evan Kroske
  • 4,506
  • 12
  • 40
  • 59
  • 1
    Why not merge to a local tracking branch and then push the local branch to the remote branch? – Eric Walker Jul 14 '10 at 03:36
  • Does this answer your question? [How do I push a local Git branch to master branch in the remote?](https://stackoverflow.com/questions/5423517/how-do-i-push-a-local-git-branch-to-master-branch-in-the-remote) – Tom Hale Jun 30 '23 at 13:55

3 Answers3

21

You should run git help push, which will tell you about the syntax for the refspec that you push to. In short, git push <remotename> <local_branch_name>:<remote_branch_name>

Daenyth
  • 35,856
  • 13
  • 85
  • 124
  • 1
    For me I had to do `git remote` (get the remote name, mine was origin) `git push -f origin UI-Addition:UI-Addition` W/o the `-f` option it would not allow the operation. – Danuofr Aug 24 '16 at 23:15
  • This fails for me. It says `error: unable to push to unqualified destination: temp2 The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref.` – John Henckel Jun 07 '18 at 20:57
2

I was not able to do this with a single command. First I commit all my changes to my local master. Then I create a new local branch called "mybranch" using

git checkout -b mybranch

and then I pushed that using

git push -u origin mybranch

in my case origin is the remote name. Your remote name might be different. You can use git remote -v to see what your remote name should be.

After the push, if you want, you can get rid of your local branch using these two commands

git checkout master
git branch -d mybranch

hope that helps.

John Henckel
  • 10,274
  • 3
  • 79
  • 79
0

What I did was creatE a new local branch for example name it test1

> git checkout -b test1

This command will create a branch and switch to it directly, and then push your new local branch to your remote repository either GitHub or GitLab by typing

> git push origin test1

don't forget to check the correct link by typing.

> git remote --v

benhorgen
  • 1,928
  • 1
  • 33
  • 38
Fady Ayoub
  • 391
  • 2
  • 11