11

I am using GIT, and I have created a new branch (named foo) on my local (one that doesn't exist on the repository). Now, I have made some changes to my original project (master) files and committed all those changes in this new branch. What I would now like to do is push this new branch to the remote repository. How can I do that? If I just run git push while I am on the branch foo, will this push the entire branch to the repo. I don't want my changes to be pushed to master. I just want to push this new branch to the main repository.

John Dui
  • 541
  • 3
  • 8
  • 15
  • 2
    I don't see any accepted answer in your 10 questions: please read http://stackoverflow.com/help/accepted-answer and http://stackoverflow.com/help/why-vote – VonC Oct 28 '16 at 06:16
  • Possible duplicate of [How do I push a new local branch to a remote Git repository and track it too?](https://stackoverflow.com/questions/2765421/how-do-i-push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too) – Jannie Theunissen Jul 31 '17 at 12:15

6 Answers6

11

Yes. It'll prompt you to set the upstream.

Example

git branch --set-upstream origin yourbranch

It's same for everything except for the branch name. Follow the on screen guidelines.

iamgr007
  • 966
  • 1
  • 8
  • 28
Pranesh Ravi
  • 18,642
  • 9
  • 46
  • 70
10

To push a branch onto a remote repository you should use this syntax

git push (remote) (branch)

Usually the first remote (and often the unique) is named "origin", thus in your case you would run

git push origin foo

It is usually advisable to run a slightly more complex command

git branch --set-upstream-to origin/foo

because "--set-upstream-to" (abbreviated "-u") sets a tracking on that branch and will allow you to push future changes simply running

git push origin

"Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into." (cit. git documentation)

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
David
  • 2,987
  • 1
  • 29
  • 35
3

checkout in to your local branch and use: git push -u origin <branch>. this will create the new branch at the remote and push all the changes

gutte
  • 1,373
  • 2
  • 19
  • 31
1

git push origin foo should do the trick. Latest git versions only push single branches (so git push would work as long as you checked out foo ) unless you change push behaviour in git config.

Reference can be found here

Hevlastka
  • 1,878
  • 2
  • 18
  • 29
1

If the upstream is defined OK, run the following command:

git push origin foo:foo
yorammi
  • 6,272
  • 1
  • 28
  • 34
  • 1
    With commands like this, the whole `foo` `bar` syntax can be a bit too vague with annotating `foo(local)` `bar(remote)`. According to your example, the name has to be identical, which is not the case. – Abandoned Cart Mar 30 '18 at 17:29
1

use git remote -v to show your remote repo name and url:

origin   ssh://**/*.git (fetch)
origin   ssh://**/*.git (push)

origin is your remote repo name at local: You can use this command to push your new branch to origin remote repo:

git push origin [your-branch-name]

Like this:

git push origin foo
haiyang
  • 310
  • 3
  • 8