12

I am on a master branch 'master' and I have 1 commit ahead I want to create a new remote branch called 'new_remote' and push my commit there?

$ git branch
* master
$ git remote
old_remote

$ git status
# On branch master
# Your branch is ahead of 'old_remote/master' by 1 commit.

I want to push my commit to a new branch on remote called 'new remote' Thank you.

5 Answers 5

22

If you are currently working on local branch master, and the new remote branch has not been created yet:

git checkout -b new_branch     // creates a local branch (as a copy of the current)

git push origin new_branch // push it to the remote server
9
  • How to create this 'new remote branch'?
    – michael
    Jan 15, 2011 at 0:44
  • After I create a local branch using the first command, how can I get my commit from my master branch to this new local branch?
    – michael
    Jan 15, 2011 at 0:48
  • @michael This new branch is already a copy of the current branch you were in. Jan 15, 2011 at 0:49
  • Thanks. But why you use 'origin' in your command 'git push origin new_branch' I tried it. It said 'fatal 'origin' does not appear to be a git repository.
    – michael
    Jan 15, 2011 at 0:53
  • @michael Are you on currently logged on a directory that has a git repo? git status Jan 15, 2011 at 0:55
8

If you want to push your master branch into a newbranch on the remote repository called origin then you can run:

git push origin master:newbranch
2

Although what you are trying is perfectly legal in git, from a general best practice standpoint (when you have many parallel lines of development) I'd suggest to create a local tracking branch and push it to your remote.

git branch --track local_branch remote_branch
1
git push origin localBranchName:master

More generally,

git push remote local_branch_Name:remote_branch_name
0

I think you just want to push your changes, so:

git push old_remote master

should be enough for you. The first parameter for git push is the remote you want to update (in your case that's old_remote') and the second is the branch you want to push.

Instead of specifying branch with name, you can use --all like this:

git push old_remote --all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.