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 just 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. Or they can reach your branch, by doing:

git fetch
git checkout origin/your_branch

Note however that formally, the format is:

git push origin <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!

Upstream

So that a subsequent git pull will know what to do, you might instead want to use:

git push --set-upstream origin <local-branch-name> 

or ( Short-form)

git push -u origin <local-branch-name> 

As described below, the --set-upstream option sets up an upstream branch. git sets the new branch as a remote track branch (tracking):

Adding a remote tracking branch means that git yet knows what you want to do when you git fetch, git pull, or git push in the future. This assumes that you want the local branch and the remote branch to be in sync.

it means now we can simply write command (example: “git fetch” ) without any option.

git push origin <local-branch-name> 

can now be (short) written as :

git push 

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *