Signup/Sign In
LAST UPDATED: JULY 7, 2021

Git Push

We won't always be working alone on a project on just our system and we often have to share our work with others or let others contribute and collaborate with us on a project. This is where Git Push and Remote Repositories come into action. Pushing a repository is like uploading it on a platform where other developers can see our project and its version history and even collaborate with us. In this tutorial, we'll see how to push our local repository content on a remote repository.

Remote Repository

  • A remote repository is just a repository that is hosted on some code-sharing platform. GitHub and BitBucket are the most popular and widely used Git hosting services.
  • It can be used to share our code with others or to collaborate with other developers on the same project. We can see the changes that are made by others and can also share our own project versions.
  • It is a common practice to create a remote repository as a bare repository. This is because a bare repository does not have a working directory and so there won't ever be any files that are not committed that may get altered while pushing to the remote directory.

What is Git Push?

  • Once we are done committing changes to our projects on our local system, we can push those changes to a remote repository on a Git hosting website.
  • Git provides us with the git push command to push or upload our local repository files to the remote repository. Other developers can then see these changes and even download those new changes by using the git pull command.
  • Git push only updates that branch on the remote repository in which you are currently working on your local machine. So we need to make sure that we are working on the desired branch that we need to update in the remote repository.
  • It is also a good practice to pull before pushing as there may be someone else who pushed to or updated the remote repository before you and you may not be aware of that change.

Git Push and Git Pull illustration

How to Push?

When we are done working on our project and are ready to share it with other developers on our team, we will be pushing our local repository to the remote repository. To do this we first need to establish a connection with the remote repository and then push our repository. Let's look at the Git commands that help us to do that.

Git Remote Command

Git push is the command that lets us upload our local repositories to the remote repositories. However, it is not the only command that we need to use to push to a remote repository. We first need to add a new remote. A remote is just a connection between the local repository and the remote repository. This is just a way of telling Git where the remote directory is so that it can establishing a connection. Consider the following illustration in which Repo 1 has two remote connections - one to a central repository and another to Repo 2.

Remote connections with two repositories

This can be done by using the git remote add command.

$ git remote add <remote-name> <remote-repo-URL>

We can also remove and rename remotes using the git remote command. To remove a remote:

$ git remote rm <remote-name>

To rename a remote:

$ git remote rename <old-remote-name> <new-remote-name>

To check the remote connections that we have with other repositories:

$ git remote -v

Git Push Command

After establishing a remote connection we can push our repository to the remote repository. Make sure that you enter the current branch name in which you are working.

$ git push <remote-name> <branch-name>

We can also use -force flag along with the Git Push command to force a push. This is done to prevent Git from blocking our push. Git may block a push if it is overwriting an existing commit in the remote repository.

$ git push <remote-name> --force 

We can also use --all flag to push all the branches to the remote repo.

$ git push --all <remote-name>

Next, you will be asked to sign in to your account on the hosting site that you are using for the remote repository.

Enter Username and Password to push the local repository to remote repository

Summary

In this article, we learned about remote repositories and how they help developers to collaborate and work on a single project. We saw how to connect to a remote repository using the git remote add command and other git remote commands to remove and rename existing remotes. Next, we tried to understand how to push a local repository to a remote repository using the git push command and learned how to force a push using the --force flag that might otherwise be blocked by Git. We saw the --all flag which allows us to push all the branches on our local repository to the remote repository. I hope this tutorial was helpful and you learned something new from it. In the next tutorial, we will see how to download or pull these remote repositories to our local machine.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight