Home Software Engineering How To Push Git Branch To Remote

How To Push Git Branch To Remote

by schkn

In Git, branches are commonly used in order to develop features independently from the main workflow.

Git is a decentralized versioning system : as a consequence, you have local and remote branches on your repository.

When you are working locally, you are committing to your local branch, but what if you wanted to share your changes with your colleagues?

In order to share changes, you will need to push your Git branch to the remote repository.

In this tutorial, we are going to see how you can easily push a Git branch remotely.

Push Branch To Remote

In order to push a Git branch to remote, you need to execute the “git push” command and specify the remote as well as the branch name to be pushed.

$ git push <remote> <branch>

For example, if you need to push a branch named “feature” to the “origin” remote, you would execute the following query

$ git push origin feature
git push branch to remote

If you are not already on the branch that you want to push, you can execute the “git checkout” command to switch to your branch.

If your upstream branch is not already created, you will need to create it by running the “git push” command with the “-u” option for upstream.

git push upstream branch to remote
$ git push -u origin feature

Congratulations, you have successfully pushed your branch to your remote!

Push Branch to Another Branch

In some cases, you may want to push your changes to another branch on the remote repository.

In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.

$ git push <remote> <local_branch>:<remote_name>

As an example, let’s say that you have created a local branch named “my-feature”.

$ git branch

  master
* my-feature
  feature

However, you want to push your changes to the remote branch named “feature” on your repository.

In order to push your branch to the “feature” branch, you would execute the following command

$ git push origin my-feature:feature

Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 513 bytes | 513.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/SCHKN/repo.git
   b1c4c91..9ae0aa6  my-feature -> feature

In order to push your branch to another branch, you may need to merge the remote branch to your current local branch.

In order to be merged, the tip of the remote branch cannot be behind the branch you are trying to push.

Before pushing, make sure to pull the changes from the remote branch and integrate them with your current local branch.

$ git pull

$ git checkout my-feature

$ git merge origin/feature

$ git push origin my-feature:feature

Note : when merging the remote branch, you are merging your local branch with the upstream branch of your local repository.

Congratulations, you pushed your branch to another branch on your repository!

Push Branch to Another Repository

In order to push a branch to another repository, you need to execute the “git push” command, and specify the correct remote name as well as the branch to be pushed.

$ git push <remote> <branch>

In order to see the remotes defined in your repository, you have to execute the “git remote” command with the “-v” option for “verbose”.

$ git remote -v

origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)
custom  https://github.com/user/custom.git (fetch)
custom  https://github.com/user/custom.git (push)

In the previous examples, we pushed our branch to the “origin” remote but we can choose to publish it to the “custom” remote if we want.

$ git push custom feature

Awesome, you pushed your branch to another remote repository!

Troubleshooting

In some cases, you may run into errors while trying to push a Git branch to a remote.

Failed to push some refs

git push branch troubleshooting

The error message states that the a pushed branch tip is behind its remote (references are behind)

In order to fix this, you need first to pull the recent changes from your remote branches with the “git pull” command.

$ git pull

When pulling the changes, you may run into merge conflicts, run the conflicts and perform a commit again with your results.

Now that the files are merged, you may try to push your branch to the remote again.

$ git push origin feature

Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 513 bytes | 513.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/SCHKN/repo.git
   b1c4c91..9ae0aa6  feature -> feature

Conclusion

In this tutorial, you learnt how you can push a Git branch to a remote with the “git push” command.

You learnt that you can easily specify your branch and your remote if you want to send your changes to other repositories.

If you are interested in Software Engineering or in Git, we have many other tutorials on the subject, so make sure to check it out!

You may also like

7 comments

Links 16/2/2020: MX Linux 19.1 and MyPaint 2.0 | Techrights February 16, 2020 - 8:32 pm

[…] How To Push Git Branch To Remote […]

Reply
fred April 2, 2020 - 12:41 pm

Hello, great tuto! When you say that, on/from which branch do you execute the git pull? Thanks

$ git pull
$ git checkout my-feature
$ git merge origin/feature
$ git push origin my-feature:feature

Reply
Abhay Srivastava May 26, 2020 - 1:29 am

Hi Mate,
Very very helpful post. I was stuck with “error: failed to push some refs to” for one day and searched everywhere only to land here and get it resolved within no time.

Thanks a ton.

Reply
schkn May 26, 2020 - 7:38 pm

Glad it helped!

Reply
As'ad Saleh Umar August 4, 2020 - 11:18 am

Thanks mate, i figure it out !

Reply
daniel December 6, 2021 - 3:21 pm

when you sya origin? is that a git thing ? cus im trying to push to this git branch from my local branch and i am having errors about not being relational

Reply
Alex December 30, 2021 - 1:12 pm

Great article, thanks so much! I was struggling with a ‘fast forward error’ and this completely solved it

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.