Upload to Remote Repository – Git Push

How to Upload Changes with git push

Push is used when you want changes made to the Local Repository to be reflected in the Remote Repository. After establishing the link between the Remote Repository and the Local Repository using the git remote add command or the git clone command, you can upload your changes into the linked Remote Repository by running the git push command.

When you push a branch, you need to specify the branch name after the Remote Repository name. Typically, origin is used for a Remote Repository name. The following is an example of pushing the master branch to the Remote Repository.

git push origin master

Push Operation

Mainly, there are two cases in the push operation.

1. Push an existing branch

This is the case when the branch exists in both the Local Repository and the Remote Repository. In this case, when you run the push command, the branch is uploaded to the Remote Repository, and Fast-Forward merge is triggered unless the pushed branch doesn't create conflicts.

2. Push a new branch

This is the case when the branch is created in the Local Repository; however, it doesn't exist in the Remote Repository. In this case, when you run the push command, the pushed branch is generated in the Remote Repository.

Conflicts

When you push a branch to the Remote Repository, Git checks if the pushed branch creates a conflict. If there is any conflict, the push operation is terminated.

You'll see an error message like the one below when you encounter a conflict.

To https://github.com/bloovee/git_remote_practice.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/bloovee/git_remote_practice.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

For example, you'll face a conflict when an advanced commit already exists on the same branch in the Remote Repository like in Case A in the illustration below: commit M3R already exists in the Remote Repository while commit M3L in the Local Repository is being pushed.

When you are pushing the code to a new branch, it is safer. It will be like Case B in the illustration below: commit A1 in Branch A (new branch) doesn't conflict with commit M3R in the master branch unless you trigger the merge operation.

Git Push illustration: Comparison between pushing to an existing branch and pushing to a new branch

IdeaThe -u option

In a typical Git operation, the git push command is often used to upload edited files on the same branch in the same Remote Repository. To avoid typing a Remote Repository name and a branch name every time, you can use the -u option or –set-upstream option (the meaning of upstream will be explained on the git fetch page). After running the command with the -u option, you don't need to specify a Remote Repository name and a branch name again.

For the first push command, run:

git push -u origin master

After the first command (no need to type origin and master anymore), run:

git push

After the push operation

When you push your code to the Remote Repository, other developers who have access to the Remote Repository (collaborators) can see it on the GitHub web platform and download it to their local computer by the git pull