Chapter 6. Remote Collaboration

Upload to Remote Repository – Git Push

Upload to Remote Repository – Git Push
Tag:

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.

Upload-to-Remote-Repository--Git-Push

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 or git fetch command. You can also ask one of the collaborators to review and merge their code with a main branch (e.g., master branch) by triggering a pull request.

For a better understanding, please go through the following practice section.

bloovee-round-icon.pngDeveloper A (Project Owner Role)

Practice

Objective:
Learn how to push branches

All actions described below are done from Developer A's (project owner's) point of view

1. Prepare a practice file

Use the same file as in the previous practice on the previous page (the git remote page). We already made one commit on the master branch. If you haven't created the file, go back to the previous page.

2. Push a file to an existing branch

As the URL of the Remote Repository is already registered, you are ready to run the push command.

Command Line - INPUT
git push origin master

When you run the command, the command line may ask you to type in your password depending on the SSH or HTTPS settings. For HTTPS connection, you need to use PAT (Personal Access Token).

Command Line - RESPONSE
Enumerating objects: 5, done.
:
To https://github.com/bloovee/git_remote_practice.git
 * [new branch]      master -> master

Go to the Remote Repository on the GitHub website. After refreshing the browser, you can see that the project directory git_remote_practice is successfully uploaded to the Remote Repository as shown below.

Upload-to-Remote-Repository--Git-Push

3. Create conflicted commits on the Local and Remote Repositories

This section is just for practice purposes. We'll explain under what conditions you cannot execute the push command.

Edit the file on the Remote Repository

First, make a new commit directly on the Remote Repository.

In the Remote Repository, select the git_remote_practice.html file

You can see the content of the HTML file as shown below. Press the pen icon on the right to edit the file.

Upload-to-Remote-Repository--Git-Push

Edit the file like below (adding <h1>M2RA</h1>).

git_remote_practice.html (master)
<!-- Master Branch-->
<h1>M1LA</h1>
<h1>M2RA</h1>
<!-- /Master Branch-->

Go to the bottom of the site and add a commit message. In this practice, put "M2RA". M2RA indicates the following for practice purposes;

  • M – Master branch
  • 2 – 2nd commit on the branch
  • R – Committed in the Remote Repository
  • A – Committed by Developer A

Select "commit directly to the master branch" and click the green Commit changes button to commit the change.

Upload-to-Remote-Repository--Git-Push

Edit the file on the local computer

Now, go back to the editor on your local computer and edit like shown below (the same edit as the one on the Remote Repository).

git_remote_practice.html (master)
<!-- Master Branch-->
<h1>M1LA</h1>
<h1>M2LA</h1>
<!-- /Master Branch-->

Commit the change with the commit message of "M2LA". M2LA indicates the following for practice purposes:

  • M – Master branch
  • 2 – 2nd commit on the branch
  • L – Committed in the Local Repository
  • A – Committed by Developer A

And run the git log command.

Command Line - INPUT
git commit -am "M2LA"
git log --oneline

You can see the commit history like shown below.

Command Line - RESPONSE
77e4ddf (HEAD -> master) M2LA
4b30a1c (origin/master) M1LA

The current situation is similar to Case A explained at the beginning of this page.

Run the git push command to see the result.

Command Line - INPUT
git push origin master

As there is commit M2RA in the Remote Repository already, the push command is rejected.

Command Line - RESPONSE
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.

4. Push a file to a new branch

As we cannot push the change to the master branch, create a new branch Branch_A and checkout to the branch. To confirm the commit history on the branch, also run the git log command.

Command Line - INPUT
git checkout -b Branch_A
git log --oneline

You can see that the status of Branch_A is the same as the status of the master branch.

Command Line - RESPONSE
Switched to a new branch 'Branch_A'
77e4ddf (HEAD -> Branch_A, master) M2LA
4b30a1c (origin/master) M1LA

Currently, Branch_A doesn't exist in the Remote Repository. Push Branch_A to see the result.

Command Line - INPUT
git push origin Branch_A

You can see that Branch_A was successfully pushed to the Remote Repository although the code of the master branch and Branch_A are the same. This is because you can avoid clashing code on the same branch and give room to reconcile on the Remote Repository later.

Command Line - RESPONSE
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 10 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 561 bytes | 561.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
remote: 
remote: Create a pull request for 'Branch_A' on GitHub by visiting:
remote:      https://github.com/bloovee/git_remote_practice/pull/new/Branch_A
remote: 
To https://github.com/bloovee/git_remote_practice.git
 * [new branch]      Branch_A -> Branch_A

Go to the Remote Repository on the GitHub website; exit the code browsing mode by clicking the git_remote_practice repository. You can confirm that Branch_A was created in the browser as shown below.

Upload-to-Remote-Repository--Git-Push

5. Clean up the master branch conflict

As we intentionally created conflicting commits – commit M2LA and M2RA in the Local Repository and the Remote Repository – let's clean them up before going to the next practice.

Upload-to-Remote-Repository--Git-Push

First, clean up Branch_A

Currently, Branch_A and the master branch are the same. We want to create a unique code for Branch_A for practice purposes.

Reset commit histories to commit M1LA on Branch_A. This is for recreating Branch_A from the first commit of the master branch because commit M2LA conflicts with M2RA in the Remote Repository. To execute git reset, use a Commit Hash generated on your computer for commit M1LA.

Command Line - INPUT
git reset --hard 4b30a1c

You can see that the HEAD is back to M1LA.

Command Line - RESPONSE
HEAD is now at 4b30a1c M1LA

Now create a correct version of Branch_A. Edit the HTML file like below (adding <h1>A1LA</h1> after <!-- Branch A-->.

A1LA indicates the following for practice purposes:

  • ABranch_A
  • 1 – 1st commit on the branch
  • L – Committed in the Local branch
  • A – Committed by Developer A
git_remote_practice.html (Branch_A)
<!-- Master Branch-->
<h1>M1LA</h1>
<!-- /Master Branch-->

<!-- Branch A-->
<h1>A1LA</h1> 
<!-- /Branch A-->

Commit the change with the commit message of "A1LA" and check the log.

Command Line - INPUT
git commit -am "A1LA"
git log --oneline

You can see that the HEAD of Branch_A is now A1LA and it has diverged from the master branch at commit M1LA.

Command Line - RESPONSE
545ffbb (HEAD -> Branch_A) A1LA
4b30a1c (origin/master) M1LA


As Branch_A on the Remote Repository is still in its old status, go to the Remote Repository and delete it to avoid a conflict. This is for practice purposes. You need to carefully manage the process when deleting a branch on the Remote Repository.

To delete a branch, press the branch selection pull-down on the left and click View all branches.

Upload-to-Remote-Repository--Git-Push

Click the delete button on Branch_A as shown below.

Upload-to-Remote-Repository--Git-Push

You can see that Branch_A was successfully deleted:

Upload-to-Remote-Repository--Git-Push

To update Branch_A on the Remote Repository, push Branch_A.

Command Line - INPUT
git push origin Branch_A

You can see that Branch_A was pushed successfully.

Command Line - RESPONSE
Enumerating objects: 6, done.
:
 * [new branch]      Branch_A -> Branch_A

You can also confirm that the latest commit is A1LA on the Remote Repository as shown in the image below.

Upload-to-Remote-Repository--Git-Push

Next, clean up the master branch

Switch the current branch to the master branch and check the log.

Command Line - INPUT
git checkout master
git log --oneline

You can see that M2LA, which conflicts with the commit on the Remote Repository, is still the HEAD of the master branch as we reset it only on Branch_A.

Command Line - RESPONSE
77e4ddf (HEAD -> master) M2LA
4b30a1c (origin/master) M1LA

Reset the master branch to commit M1LA. Use the Commit Hash generated on your computer for commit M1LA.

Command Line - INPUT
git reset --hard 4b30a1c

You can see that the HEAD of the master branch is now back to M1LA.

Command Line - RESPONSE
HEAD is now at 4b30a1c M1LA

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.

Upload-to-Remote-Repository--Git-Push

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 or git fetch command. You can also ask one of the collaborators to review and merge their code with a main branch (e.g., master branch) by triggering a pull request.

For a better understanding, please go through the following practice section.

bloovee-round-icon.pngDeveloper A (Project Owner Role)

Practice

Objective:
Learn how to push branches

All actions described below are done from Developer A's (project owner's) point of view

1. Prepare a practice file

Use the same file as in the previous practice on the previous page (the git remote page). We already made one commit on the master branch. If you haven't created the file, go back to the previous page.

2. Push a file to an existing branch

As the URL of the Remote Repository is already registered, you are ready to run the push command.

Command Line - INPUT
git push origin master

When you run the command, the command line may ask you to type in your password depending on the SSH or HTTPS settings. For HTTPS connection, you need to use PAT (Personal Access Token).

Command Line - RESPONSE
Enumerating objects: 5, done.
:
To https://github.com/bloovee/git_remote_practice.git
 * [new branch]      master -> master

Go to the Remote Repository on the GitHub website. After refreshing the browser, you can see that the project directory git_remote_practice is successfully uploaded to the Remote Repository as shown below.

Upload-to-Remote-Repository--Git-Push

3. Create conflicted commits on the Local and Remote Repositories

This section is just for practice purposes. We'll explain under what conditions you cannot execute the push command.

Edit the file on the Remote Repository

First, make a new commit directly on the Remote Repository.

In the Remote Repository, select the git_remote_practice.html file

You can see the content of the HTML file as shown below. Press the pen icon on the right to edit the file.

Upload-to-Remote-Repository--Git-Push

Edit the file like below (adding <h1>M2RA</h1>).

git_remote_practice.html (master)
<!-- Master Branch-->
<h1>M1LA</h1>
<h1>M2RA</h1>
<!-- /Master Branch-->

Go to the bottom of the site and add a commit message. In this practice, put "M2RA". M2RA indicates the following for practice purposes;

  • M – Master branch
  • 2 – 2nd commit on the branch
  • R – Committed in the Remote Repository
  • A – Committed by Developer A

Select "commit directly to the master branch" and click the green Commit changes button to commit the change.

Upload-to-Remote-Repository--Git-Push

Edit the file on the local computer

Now, go back to the editor on your local computer and edit like shown below (the same edit as the one on the Remote Repository).

git_remote_practice.html (master)
<!-- Master Branch-->
<h1>M1LA</h1>
<h1>M2LA</h1>
<!-- /Master Branch-->

Commit the change with the commit message of "M2LA". M2LA indicates the following for practice purposes:

  • M – Master branch
  • 2 – 2nd commit on the branch
  • L – Committed in the Local Repository
  • A – Committed by Developer A

And run the git log command.

Command Line - INPUT
git commit -am "M2LA"
git log --oneline

You can see the commit history like shown below.

Command Line - RESPONSE
77e4ddf (HEAD -> master) M2LA
4b30a1c (origin/master) M1LA

The current situation is similar to Case A explained at the beginning of this page.

Run the git push command to see the result.

Command Line - INPUT
git push origin master

As there is commit M2RA in the Remote Repository already, the push command is rejected.

Command Line - RESPONSE
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.

4. Push a file to a new branch

As we cannot push the change to the master branch, create a new branch Branch_A and checkout to the branch. To confirm the commit history on the branch, also run the git log command.

Command Line - INPUT
git checkout -b Branch_A
git log --oneline

You can see that the status of Branch_A is the same as the status of the master branch.

Command Line - RESPONSE
Switched to a new branch 'Branch_A'
77e4ddf (HEAD -> Branch_A, master) M2LA
4b30a1c (origin/master) M1LA

Currently, Branch_A doesn't exist in the Remote Repository. Push Branch_A to see the result.

Command Line - INPUT
git push origin Branch_A

You can see that Branch_A was successfully pushed to the Remote Repository although the code of the master branch and Branch_A are the same. This is because you can avoid clashing code on the same branch and give room to reconcile on the Remote Repository later.

Command Line - RESPONSE
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 10 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 561 bytes | 561.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
remote: 
remote: Create a pull request for 'Branch_A' on GitHub by visiting:
remote:      https://github.com/bloovee/git_remote_practice/pull/new/Branch_A
remote: 
To https://github.com/bloovee/git_remote_practice.git
 * [new branch]      Branch_A -> Branch_A

Go to the Remote Repository on the GitHub website; exit the code browsing mode by clicking the git_remote_practice repository. You can confirm that Branch_A was created in the browser as shown below.

Upload-to-Remote-Repository--Git-Push

5. Clean up the master branch conflict

As we intentionally created conflicting commits – commit M2LA and M2RA in the Local Repository and the Remote Repository – let's clean them up before going to the next practice.

Upload-to-Remote-Repository--Git-Push

First, clean up Branch_A

Currently, Branch_A and the master branch are the same. We want to create a unique code for Branch_A for practice purposes.

Reset commit histories to commit M1LA on Branch_A. This is for recreating Branch_A from the first commit of the master branch because commit M2LA conflicts with M2RA in the Remote Repository. To execute git reset, use a Commit Hash generated on your computer for commit M1LA.

Command Line - INPUT
git reset --hard 4b30a1c

You can see that the HEAD is back to M1LA.

Command Line - RESPONSE
HEAD is now at 4b30a1c M1LA

Now create a correct version of Branch_A. Edit the HTML file like below (adding <h1>A1LA</h1> after <!-- Branch A-->.

A1LA indicates the following for practice purposes:

  • ABranch_A
  • 1 – 1st commit on the branch
  • L – Committed in the Local branch
  • A – Committed by Developer A
git_remote_practice.html (Branch_A)
<!-- Master Branch-->
<h1>M1LA</h1>
<!-- /Master Branch-->

<!-- Branch A-->
<h1>A1LA</h1> 
<!-- /Branch A-->

Commit the change with the commit message of "A1LA" and check the log.

Command Line - INPUT
git commit -am "A1LA"
git log --oneline

You can see that the HEAD of Branch_A is now A1LA and it has diverged from the master branch at commit M1LA.

Command Line - RESPONSE
545ffbb (HEAD -> Branch_A) A1LA
4b30a1c (origin/master) M1LA


As Branch_A on the Remote Repository is still in its old status, go to the Remote Repository and delete it to avoid a conflict. This is for practice purposes. You need to carefully manage the process when deleting a branch on the Remote Repository.

To delete a branch, press the branch selection pull-down on the left and click View all branches.

Upload-to-Remote-Repository--Git-Push

Click the delete button on Branch_A as shown below.

Upload-to-Remote-Repository--Git-Push

You can see that Branch_A was successfully deleted:

Upload-to-Remote-Repository--Git-Push

To update Branch_A on the Remote Repository, push Branch_A.

Command Line - INPUT
git push origin Branch_A

You can see that Branch_A was pushed successfully.

Command Line - RESPONSE
Enumerating objects: 6, done.
:
 * [new branch]      Branch_A -> Branch_A

You can also confirm that the latest commit is A1LA on the Remote Repository as shown in the image below.

Upload-to-Remote-Repository--Git-Push

Next, clean up the master branch

Switch the current branch to the master branch and check the log.

Command Line - INPUT
git checkout master
git log --oneline

You can see that M2LA, which conflicts with the commit on the Remote Repository, is still the HEAD of the master branch as we reset it only on Branch_A.

Command Line - RESPONSE
77e4ddf (HEAD -> master) M2LA
4b30a1c (origin/master) M1LA

Reset the master branch to commit M1LA. Use the Commit Hash generated on your computer for commit M1LA.

Command Line - INPUT
git reset --hard 4b30a1c

You can see that the HEAD of the master branch is now back to M1LA.

Command Line - RESPONSE
HEAD is now at 4b30a1c M1LA

Tag: