Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Git & GitHub IntroductionChapter 6. Remote Collaboration

Upload to Remote Repository – Git Push

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 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.

Practice

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

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.

A pushed file in the GitHub repository

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.

Editing a file in a GitHub repository

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.

Creating a new commit directory in a GitHub repository 1

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.

Creating a new commit directory in a GitHub repository 2

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.

Git local repository and GitHub remote repository status illustration

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:

  • A – Branch_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.

Selecting the master branch on GitHub

Click the delete button on Branch_A as shown below.

Deleting a branch on GitHub 1

You can see that Branch_A was successfully deleted:

Deleting a branch on GitHub 2

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.

Selecting another branch on GitHub

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


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Launching Apache Web Server on Linux

Launch Apache Web Server

Adjusting Social Login for Django Production

Social Login for Production

Creating User Groups in Linux

groupadd (Add Group)

Setting Up Social Login with Google via Django Allauth

Django Allauth (6) – Social Login with Google

Basics of Django App Deployment

Overview of Django App Deployment (1)

Launching Apache Web Server on Linux

Launch Apache Web Server

Adjusting Social Login for Django Production

Social Login for Production

Creating User Groups in Linux

groupadd (Add Group)

Setting Up Social Login with Google via Django Allauth

Django Allauth (6) – Social Login with Google

Basics of Django App Deployment

Overview of Django App Deployment (1)

Tags:

Remote Repository

Push

Remote Collaboration

Git Key Commands

Git & GitHub Introduction
Course Content

Chapter 1. Git & GitHub Overview

What Is Git?

What Is Version Control?

How To Save Versions in Git?

Collaborating on Git & GitHub – Remote Repository

Collaborating on Git & Git Hub – Branch

Git & GitHub Basic Life Cycle

Chapter 2. Git & GitHub Initial Settings

Git & GitHub Initial Settings Overview

Key Tool Preparation (1) – Mac

Key Tool Preparation (2) – Windows

Key Tool Preparation (3) – Linux Remote Server

Git User Settings – git config

Create GitHub Account

GitHub Access Authentication Settings

Generating PAT (Personal Access Token)

GitHub SSH Setup

Chapter 3. Git & GitHub Project Setup

Three Cases in Git & GitHub Project Setup

Git & GitHub Project Setup Overview in Different Cases

Building Remote Collaboration Practice Environment

Project Initiator – Key Steps To Launch Git Project

Project Initiator – Create Local Repository (git init)

Project Initiator – Make the First Commit

Project Initiator – .gitignore File

Project Initiator – Create Remote Repository

Project Initiator – Link Between Remote and Local Repositories (git remote add)

Project Initiator – Upload Local Repository to Remote Repository (git push)

Project Initiator – Grant Remote Repository Access to Project Members

Project Member – Start Project As Collaborator

Project Member – Create Copy of Project Code on Local Computer (git clone)

Non-Member – Start Project With Replica of Existing Repository (Fork)

Fork vs. Clone

Chapter 4. Edit & Commit

Git Regular Workflow – Edit & Commit

Edit and Commit Overview (1)

Add Files to Staging Area – git add

Commit Files – git commit

HEAD and INDEX

Check Status of Working Tree and Staging Area – git status

Check Commit Histories – git log

Check Differences – git diff

Restore Files to Working Tree – git restore

Undo Changes – git reset

Delete Files – git rm

Edit and Commit Overview (2)

Chapter 5. Work With Branches

Git Regular Workflow – Work With Branches

What Is Branch?

Branch Operation Basic Life Cycle

Create Branch and Check Branch Status – Git Branch

Switch Current Branch (1) – Git Checkout

Switch Current Branch (2) – Git Switch

Merge Branches – Git Merge

Fast-Forward Merge

Non-Fast-Forward Merge (No Option)

Non-Fast-Forward Merge (--no-ff Option)

Squash Merge

Rebase Branch – Git Rebase

Managing Conflict

Stash Changes – Git Stash

Chapter 6. Remote Collaboration

Git Regular Workflow – Remote Collaboration

Remote Collaboration Overview

Link With Remote Repository – Git Remote

Upload to Remote Repository – Git Push

Download Remote Repository and Merge to Local Repository – Git Pull

Get Remote Repository Information to Local Repository – Git Fetch

Pull vs. Fetch

Request for Review and Merge – Pull Request

Merge Operation Using GitHub

Chapter 7. Supplemental Topics

Git Key Commands and GitHub Key Features

Git & GitHub Glossary

GitHub Other Features

Chapter 1. Git & GitHub Overview

What Is Git?

What Is Version Control?

How To Save Versions in Git?

Collaborating on Git & GitHub – Remote Repository

Collaborating on Git & Git Hub – Branch

Git & GitHub Basic Life Cycle

Chapter 2. Git & GitHub Initial Settings

Git & GitHub Initial Settings Overview

Key Tool Preparation (1) – Mac

Key Tool Preparation (2) – Windows

Key Tool Preparation (3) – Linux Remote Server

Git User Settings – git config

Create GitHub Account

GitHub Access Authentication Settings

Generating PAT (Personal Access Token)

GitHub SSH Setup

Chapter 3. Git & GitHub Project Setup

Three Cases in Git & GitHub Project Setup

Git & GitHub Project Setup Overview in Different Cases

Building Remote Collaboration Practice Environment

Project Initiator – Key Steps To Launch Git Project

Project Initiator – Create Local Repository (git init)

Project Initiator – Make the First Commit

Project Initiator – .gitignore File

Project Initiator – Create Remote Repository

Project Initiator – Link Between Remote and Local Repositories (git remote add)

Project Initiator – Upload Local Repository to Remote Repository (git push)

Project Initiator – Grant Remote Repository Access to Project Members

Project Member – Start Project As Collaborator

Project Member – Create Copy of Project Code on Local Computer (git clone)

Non-Member – Start Project With Replica of Existing Repository (Fork)

Fork vs. Clone

Chapter 4. Edit & Commit

Git Regular Workflow – Edit & Commit

Edit and Commit Overview (1)

Add Files to Staging Area – git add

Commit Files – git commit

HEAD and INDEX

Check Status of Working Tree and Staging Area – git status

Check Commit Histories – git log

Check Differences – git diff

Restore Files to Working Tree – git restore

Undo Changes – git reset

Delete Files – git rm

Edit and Commit Overview (2)

Chapter 5. Work With Branches

Git Regular Workflow – Work With Branches

What Is Branch?

Branch Operation Basic Life Cycle

Create Branch and Check Branch Status – Git Branch

Switch Current Branch (1) – Git Checkout

Switch Current Branch (2) – Git Switch

Merge Branches – Git Merge

Fast-Forward Merge

Non-Fast-Forward Merge (No Option)

Non-Fast-Forward Merge (--no-ff Option)

Squash Merge

Rebase Branch – Git Rebase

Managing Conflict

Stash Changes – Git Stash

Chapter 6. Remote Collaboration

Git Regular Workflow – Remote Collaboration

Remote Collaboration Overview

Link With Remote Repository – Git Remote

Upload to Remote Repository – Git Push

Download Remote Repository and Merge to Local Repository – Git Pull

Get Remote Repository Information to Local Repository – Git Fetch

Pull vs. Fetch

Request for Review and Merge – Pull Request

Merge Operation Using GitHub

Chapter 7. Supplemental Topics

Git Key Commands and GitHub Key Features

Git & GitHub Glossary

GitHub Other Features