Git Push Command

Git Push Command

The git push term refers to upload local repository content to a remote repository. Pushing is an act of transfer commits from your local repository to a remote repository. Pushing is capable of overwriting changes; caution should be taken when pushing.

Every time you push into the repository, it is updated with some interesting changes that you made. If we do not specify the location of a repository, then it will push to default location at origin master.

The “git push” command is used to push into the repository. The push command can be considered as a tool to transfer commits between local and remote repositories. The basic syntax is given below:

$ git push <option> [<Remote URL><branch name><refspec>…]  

Push command supports many additional options. Some options are as follows under push tags.

Git Push Tags

<repository>: The repository is the destination of a push operation. It can be either a URL or the name of a remote repository.

<refspec>: It specifies the destination ref to update source object.

–all: The word “all” stands for all branches. It pushes all branches.

–prune: It removes the remote branches that do not have a local counterpart. Means, if you have a remote branch say demo, if this branch does not exist locally, then it will be removed.

–mirror: It is used to mirror the repository to the remote. Updated or Newly created local refs will be pushed to the remote end. It can be force updated on the remote end. The deleted refs will be removed from the remote end.

–dry-run: Dry run tests the commands. It does all this except originally update the repository.

–tags: It pushes all local tags.

–delete: It deletes the specified branch.

-u: It creates an upstream tracking connection. It is very useful if you are going to push the branch for the first time.

Git Push Origin Master

Git push origin master is a special command-line utility that specifies the remote branch and directory. When you have multiple branches and directory, then this command assists you in determining your main branch and repository.

Generally, the term origin stands for the remote repository, and master is considered as the main branch. So, the entire statement “git push origin master” pushed the local content on the master branch of the remote location.

Syntax:

$ git push origin master

Let’s understand this statement with an example.

Let’s make a new commit to my existing repository, say GitExample. I have added an image to my local repository named abcd.jpg and committed the changes.

$git add abcd.jpg

$git commit -m “Added new image abcd.jpg to the project”

The git status command will be performed as follows:

$ git status  

It shows the status of the image abc.jpg.

The image is wholly tracked in the local repository. Now, we can push it to origin master as:

$ git push origin master  

The file abc.jpg is successfully pushed to the origin master. We can track it on the remote location. I have pushed these changes to my GitHub account. I can track it there in my repository.

Git Force Push

The git force push allows you to push local repository to remote without dealing with conflicts. It is used as follows:

$ git push <remote><branch> -f  

Or

$ git push <remote><branch> -force

The -f version is used as an abbreviation of force. The remote can be any remote location like GitHub, Subversion, or any other git service, and the branch is a particular branch name. For example, we can use git push origin master -f.

Git push -v/–verbose

The -v stands for verbosely. It runs command verbosely. It pushed the repository and gave a detailed explanation about objects. Suppose we have added a newfile2.txt in our local repository and commit it. Now, when we push it on remote, it will give more description than the default git push. Syntax of push verbosely is given below:

Syntax:

$ git push -v  

Or

$ git push –verbose  

 

Delete a Remote Branch

We can delete a remote branch using git push. It allows removing a remote branch from the command line. To delete a remote branch, perform below command:

Syntax:

$ git push origin -delete edited  

In the above output, the git push origin command is used with -delete option to delete a remote branch. I have deleted my remote branch edited from the repository.

For more Git Related post click here.

  1. Git init Command
  2. Git add Command
  3. Git Commit Command
  4. Git Clone Command
  5. Git Config Command
  6. Git Alias Command
  7. Git Checkout Command
  8. Git Pull Command
  9. Git Fetch Command
  10. Git Status Command
  11. Git Revert Command
  12. Git Remove (rm) Command
  13. Git LOG Command
  14. Git Cheat Sheet