I created a local branch which I want to \'push\' upstream. There is a similar question here on Stack Overflow on how to track a newly created remote branch.
Howeve
Now with git, you can just type, when you are in the correct branch
git push --set-upstream origin <remote-branch-name
>
and git create for you the origin branch.
I have used two ways to create branch
If you are using TortoiseGit follow these steps:-
1.Create Branch using TortoiseGit
Right click on your project >>> TortoiseGit >>> Create Branch >>> write the name of branch and select the base branch then press ok
2.Push the branch
Right click on your project >>> TortoiseGit >>> push >>> click ok
3.Switch to new branch
Right click on your project >>> TortoiseGit >>> Switch/Checkout >>> select newly created branch and press ok
If you are using command prompt follow these steps:-
1.Create branch using command prompt
$git checkout -b new_branch_name
2.Push the branch
$git push origin new_branch_name
3.Switch to new branch it will already switched to new_branch_name otherwise you can use
$git checkout new_branch_name
Heres an example I only have two branches that were local first: origin and mobile-test.
Nothing for me worked until I used this in command line to actually show my updated files in a remote branch.
git push --set-upstream origin mobile-test
First, you create your branch locally:
git checkout -b <branch-name> # Create a new branch and check it out
The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can do:
git push <remote-name> <branch-name>
Where <remote-name>
is typically origin
, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.
Note however that formally, the format is:
git push <remote-name> <local-branch-name>:<remote-branch-name>
But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name>
(with the colon), or the remote branch will be deleted!
So that a subsequent git pull
will know what to do, you might instead want to use:
git push --set-upstream <remote-name> <local-branch-name>
As described below, the --set-upstream
option sets up an upstream branch:
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.
As of Git 2.0, the behavior has become simpler:
You can configure git with push.default = current
to make life easier:
I added this so now I can just push a new branch upstream with
$ git push -u
-u
will track remote branch of the same name. Now with this configuration, you will auto-guess the remote reference to git push. From git.config documentation:
push.default
Defines the action git push should take if no refspec is explicitly given.
push.default = current
- push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.
For me, this is a good simplification of my day-to-day Git workflow. The configuration setting takes care of the 'usual' use case where you add a branch locally and want to create it remotely. Also, I can just as easily create local branches from remotes by just doing git co remote_branch_name
(as opposed to using --set-upstream-to
flag).
I know this question and the accepted answers are rather old, but the behavior has changed so that now configuration options exist to make your workflow simpler.
To add to your global Git configuration, run this on the command line:
$ git config --global push.default current
You can do it in 2 steps:
1. Use the checkout
for create the local branch:
git checkout -b yourBranchName
2. Use the push
command to autocreate the branch and send the code to the remote repository:
git push -u origin yourBranchName
There are multiple ways to do this but I think that this way is really simple.