How to push a local git branch to remote repository?

If the new branch already exists in your remote repository and you already checked into the new branch, simply use git push command. However if the new branch does not exist yet in the remote repository or you are not checked into the new branch, first checkout to the new branch using git checkout <branch_name> command and then push and create the new branch in remote repository using git push -u <remote_name> <branch_name>

Example:

Create a new local git repository:

Create your 1st git commit:

Create a new remote git repository and add it to your local git repository:

Now push your local default branch to the remote repository:

Next, create a new local branch:

Add some change and commit to the feature branch:

Finally, push your branch from local repository to remote repository:

Next time onwards you do not need to specify the remote name or branch name to push.

Add another change and push to the remote repository:

Leave a Reply