Explain Git Push Commands with Examples

Git is a free and open-source software created in 2005 by Linus Torvalds, the leading developer of the Linux kernel. Its primary purpose is to track changes in different computer files and coordinate work among other people working on a project. In addition, Git is a Version Control System (VCS) that also tracks progress over time.

A version control system (VCS) helps a team of developers keep track of what all changes have been made to the source code. In addition, it keeps track of who made the changes and what all changes have been made. Git is a distributed VCS, thus providing each developer with their local repository and entire commit history.

There are two ways to use Git, i.e., in the form of Command Line and GUI. Git commands are the most common method, and they include various commands like:

  • Git config
  • Git add
  • Git init
  • Git pull
  • Git push
  • Git branch
  • Git merge

So let’s learn in detail about various “Git Push Commands “, each of them explained with a Git Push example.

Git Push

Git Push is the command used to transfer the contents (commits) of a local repository to a remote repository. This allows remote users to access the files and is often used when modifications have been made in the local repository.

Note: One of the many repositories hosting services is Github, which is most popular within the community. Its web-based GUI offers many features and thus helps keep the project work organized and transparent.

Push command can be explained as a command which updates the remote refs with the local refs. It is capable of overwriting the changes and thus should be used with utmost care. While pushing the commits, if we do not mention the remote repository, then Git will push them to the default location at the origin master.

The basic syntax of pushing commits to remote repositories is shown below. It accommodates all the various tags which can be used and performed.

$ git push <option> [<Remote_URL> <branch_name> <refspec> … ]

Git Push Tags

Some of the common tags and their usage are written below.

  1. <repository>: This command tells where the content has been pushed to. It is the name of the remote repository and can even be a URL.
  2. <refspec>: It is the destination ref which will update the source object.
  3. –all: When this option is used in the command line, all the commits/branches are pushed to the remote repository.
  4. –prune: It removes the remote counterparts of files that do not exist in the local repository.
  5. –delete: It is used for deleting a branch in the repository.
  6. –u: It establishes a connection and is useful when pushing for the first time.

There are various other tags in addition to these mentioned above.

Git Push Commands with Examples

The various Git push commands used in a project are explained below. The usage of these commands varies from situation to situation. We have provided a conventional usage method of these commands.

Git Push Origin Master

It is the most special Git push command that is useful when working with many repositories and branches. This command helps you in identifying the main remote repository and branch you want to work on.

By default, the name of the remote repository is the origin, and the main branch’s name is master. Thus, using this simple command helps you transfer the commits to the remote repository through the main branch.

The usage of this command is as:

$ git push origin master

Git Force Push

It is a command used to push commits to remote repositories without facing any conflicts. This means that if there is any issue or error in transferring, the system will deal independently without your interference. For example, it may replace the work you want to keep without consulting you if there are any duplicates. Therefore, the use of the Git Push Force command is often discouraged.

However, if you are using it carefully, the command is the same as previous, just with the addition of -f or –force.

$ git push <remote_repository> <branch_name> -f


$ git push <remote_repository> <branch_name> –force

This command can also be used without mentioning the branch name.

$ git push <remote_repository> -f

You can even skip the repository name, but it will perform the default behavior in that case.

Git Push Force With Lease

Git Push Force command is used to complete the transfer even if there were any conflicts. However, if you want to take care of your existing files and prefer to use the Force command, you can go for “Force With Lease.” In this case, if there is any untracked commit that can raise a conflict, then the command will make the transfer fail on its own.

$ git push <remote_repository> <branch_name> –force-with-lease

Git Push Verbose

Verbose is a handy command as it makes the command-line prompt give a descriptive output. It provides a vivid description of what is happening in place of coded text syntax and file names.

$ git push -v


$ git push –verbose

The output of a verbose command is shown in this picture.

Git Push Delete 

Furthermore, the Git Push command removes or deletes a particular branch from a specific repository. An example is shown below.

$ git push origin -delete branch2

In this case, we are deleting the remote branch named branch2 from the repository origin.

Git Push to Rename Branches

You can even use Git Push commands to rename the branch of your remote repositories. The syntax for this is as shown below.

$ git push <remote_repository> <branch_name>:<new_branch_name>

Git has become more popular than its other competitors. This owes mainly to the feature of the Distributed Version Control System offered by the software. Whenever a user has to make changes, they need to create a new branch. This helps in ensuring that the main source code is always of good quality. Also, users get their local repository with a complete history of commits.

Git helps in the smooth and efficient running of business activities while maintaining proper coordination between the departments.

Article by Born Realist