3) Collaboration with Git & GitHub Lesson

Git Push

10 min to complete · By CodingNomads Team

Now that you've gotten some experience running the git pull command, it's time to learn the other side of things. In this lesson, you'll practice pushing in Git using git push by working through the following:

  • Git Push
  • How to Push to GitHub
  • Git Push: Set Upstream
  • Git Push Force

So with that, it's time to get pushy!

Pushing in Git

The git push command is used to push local commits to a remote repository. Pushing is how you transfer the changes on your local machine to the remote repository, e.g., Github.

Git Push

The command can be considered somewhat opposite of the git pull command. git pull imports commits to your local machine, while git push exports commits to the remote repository.

Git Push Command Syntax

Just like git pull, git push takes also two arguments:

  1. Remote Name: The name of the remote you're pushing to
  2. Branch Name: The name of the branch of your remote that you want to push to

Let's see the code for git push in an example:

# git push <Remote Name> <Branch Name>
$ git push origin main

In the example above, origin is the name of the remote, and main is the name of the branch on that remote that you want to push to.

How to Push to GitHub

For this practice run, you will use the push_pull_test repository that you created in the previous section. If you're just tuning in or you'd like to review how to pull in Git, check out the preceding lesson on using Git Pull.

On your CLI, move into the root of the push_pull_test repo:

$ cd  /<PATH>/<TO>/push_pull_test

Create a new file:

$ echo "I'm pushing updates to GitHub" >> ExampleFile.txt

Let's take a look at what has happened so far:

$ git status

Git will tell you that there is one untracked file called ExampleFile.txt. Add it to the staging area:

$ git add ExampleFile.txt

Next, commit the contents of your staging area to Git:

$ git commit -m "pushing ExampleFile1"

Let's make sure that you have a remote configured before you proceed:

$ git remote -v

Looking good? Then go ahead and push your updates to your GitHub repo:

$ git push origin main
Colorful illustration of a light bulb

Note: You may have to input your GitHub username and password if prompted

After the process has finished, return to the push_pull_test repo on your browser and refresh the page. You should now be able to see the new file added to your remote repository.

Well done! This completes the basic process of pushing changes from your local machine to a remote repository. Before you move forward from this lesson, let's discuss a few useful concepts related to the git push command.

Git Push: Set Upstream

Specifying origin main (or whatever name your remote and branch have that you are pushing to) every time can soon get tiresome. If you are always pushing to the same destination, you can set a default remote and branch like so:

git push -u origin main

The -u flag in this example is short for --set-upstream-to. In this case, Git considers origin main to be "upstream," meaning that you're pulling from and pushing to this upstream source.

The -u option sets the specified remote and branch, origin and main in this case, as the default remote and branch to track. This means that git push and git pull commands will use it automatically unless you specify something else.

With it, you don't need to specify the remote name and branch name if you want to continue pushing and pulling from and to that location.

Git Push Force

In general, Git prevents you from pushing changes to a repository without pulling all new updates first. This means that if the remote repository has changed and you still need to pull those changes to your machine, you need to pull the remote branch and merge it into your local machine to ensure that all the remote changes are synced. After that, you can try to push again.

The --force flag overrides this default behavior and will forcefully push your changes without requiring you to pull any remote changes first.

Colorful illustration of a light bulb

Note: Since this action can easily overwrite changes that were done to the remote, using it is not recommended

The Git force push allows you to push your local repository to your remote without dealing with potential conflicts. This is bad practice and therefore not recommended.

The -f short flag is used as an abbreviation of --force. Either of them works:

$ git push <remote> <branch> -f  
# or
$ git push <remote> <branch> --force  

This dangerous option, which you should always handle with caution, wraps up the lesson on using git push. For more information, check the git push documentation.

Summary: Pushing in Git

Great work! You learned how to use git push and some helpful tricks when pushing in Git. As a recap:

Git Push The git push command is used to push local commits to a remote repository.

Git Push Command Syntax Run the git push command using the following syntax:

$ git push <Remote Name> <Branch Name>

Git Push: Set Upstream Short for --set-upstream-to, the -u flag allows you to set the specified remote name and branch name you provide as the default remote and branch to track.

git push -u <Remote Name> <Branch Name>

Git Push Force The --force flag, or -f for short, overrides this default behavior and will forcefully push your changes without requiring you to pull any remote changes first. Performing a Git force push using the --force flag, or -f for short, allows you to push your local repository to your remote without dealing with potential conflicts.

$ git push <remote> <branch> -f  
# or
$ git push <remote> <branch> --force  

In the next lesson, you will learn about how to work with multiple remotes on a single local Git repository.