what is Git Push Command? All you need to know [ OverView ]
Last updated on 16th Jul 2020, Blog, General
- A git push command, when executed, pushes the changes that the user has made on the local machine to the remote repository. Once the users have cloned the remote repository and have made the necessary changes in their local device, these changes need to be pushed to the remote repository.
- The reason being, so that they are shared and used by other users. Git push command does it. These changes represent commitments performed on the repository and not the uncommitted changes (if any).
- In addition to this, the changes that the user makes onto the local system are of no worth to the contributors and viewers if the GitHub cloud does not reflect it. Imagine a user working on modifying some software (third-party repository), and merging of the changes done is not easy.
- For instance, if you are working in a team on a single project, all the team members can push their code changes to the Github remote repository. Additionally, other members can fork and pull the changes from that remote repository. Therefore, it becomes effortless for multiple users to share their codes change with all the team members.
- Consider Git push as a part of the synchronization process in Git. The synchronization happens between the local and remote repository where the source and the receiver may vary.
- There are a lot of other parts to sync and git push is one of the parts because it uploads the changes done on the local repository to keep the remote repository up to date. There is nothing complicated about it, and the concept is simple, just like its syntax.
The above image suffices the concept in a nutshell.
- The user clones a repository as a first step to make some changes in the repository.
- Thereafter, they proceed to make the changes to the local system and add these changes to the staging area.
- Upon finalizing all the changes, the user then commits all the changes to the local repository.
- After their satisfaction, they push these changes to the remote server. Finally, it synchronizes the local and remote repository.
Syntax of Git Push command in Git:
Git push <remote_repo> <branch_name>
- remote_repo: This is the name (or alias) of the remote repository to which we are pushing the changes.
- branch_name: This is the branch the user is pushing to the remote repository.
- We will talk about branches in the Branches in the GitHub tutorial. But till then imagine that a branch in Git is similar to the branches in a tree. Every branch represents a new feature or modification under development.
- Additionally, the main branch is the stable code like the trunk of the tree, also called a master branch. Which, in turn, helps the unstable code of branches to stay away from the stable main code.
- The 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.
- Moreover, we can say the push updates the remote refs with local refs. 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
- The repository is the destination of a push operation. It can be either a URL or the name of a remote repository.
- It specifies the destination ref to update source object.
- The word “all” stands for all branches. It pushes all branches.
- 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.
- 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 tests the commands. It does all this except originally update the repository.
- It pushes all local tags.
Get JOB Oriented Git Training By Experienced Instructors
- Instructor-led Sessions
- Real-life Case Studies
- It deletes the specified branch.
- It creates an upstream tracking connection. It is very useful if you are going to push the branch for the first time.
Git push usage
Git push <remote> <branch>
- Push the specified branch to <remote>, along with all of the necessary commits and internal objects. This creates a local branch in the destination repository.
- To prevent you from overwriting commits, Git won’t let you push when it results in a non-fast-forward merge in the destination repository.
Git push <remote> –force
- Same as the above command, but force the push even if it results in a non-fast-forward merge. Do not use the –force flag unless you’re absolutely sure you know what you’re doing.
Git push <remote> –all
- Push all of your local branches to the specified remote.
Git push <remote> –tags
- Tags are not automatically pushed when you push a branch or use the –all option. The –tags flag sends all of your local tags to the remote repository.
Git push discussion
- git push is most commonly used to publish an upload local changes to a central repository. After a local repository has been modified a push is executed to share the modifications with remote team members.
- The above diagram shows what happens when your local master has progressed past the central repository’s master and you publish changes by running git push origin master. Notice how git push is essentially the same as running git merge master from inside the remote repository.
Options Available in Git Push command in Git
- As mentioned in the last section, there are many options available in Git push command that helps us achieve certain specific tasks with just one execution.
- In this section, we will take you through the essential and most used options in the git push command.
Prune Option in Git Push
- prune option in git push command will delete branch XYZ from the remote repository if there does not exist any branch with the same name in the local repository.
Usage: git push –prune remote XYZ
Dry Run Option in Git Push
- This option will perform and show the execution of the git push command but will not send any updated to the remote repository.
Usage: git push –dry-run <remote> <local_branch>
Atomic Option in Git Push
- Atomic option in Git Push provides an atomic operation on the remote repository, i.e., either every reference updates or nothing at all.
Git push –atomic <remote_repo> <working_branch>
All Option in Git Push
All options will push all the branches and their committed changes to the remote repository.
Usage: git push –all <remote>
- By this, a Succesful push operation completes. Don’t relax just now; the job does not finish here. It is not just enough to push the changes to the GitHub. The verification of these changes onto the account should happen.
- In addition to this, an analysis happens which tells how these changes get reflected. Let’s analyze the difference and see if they update or not, in the next tutorial.