1 minute read

The git push command is used to upload content from the local repository to a remote repository.

Submitting is the way to transfer commits from your local repository to a remote repository.

It’s the equivalent of git fetch, but instead of downloading commit imports to local branches, it sends commit exports to remote branches. Remote branches are configured using the git remote command.

Submissions have the ability to overwrite changes, so care must be taken when making them.

Analysis

git push is often used to publish and upload local changes to a central repository.

After modifying the local repository, a push command is run to share the modifications with remote team members.

git push example

The above diagram shows what happens when the progress of your local master branch has overtaken the master branch of the central repository and you publish the changes by running a git push origin master command.

Note that running git push is similar in essence to running git merge master from the remote repository.

Git push and syncing

git push is one of many components used in the overall Git sync process.

Synchronization commands work on remote branches that are configured using the git remote command.

git push can be thought of as an upload command, while git fetch and git pull are thought of as download commands.

After the change sets have been moved by download or upload, a git merge command can be run on the target to integrate those changes.

Default usage

The following example describes one of the standard methods for publishing local contributions to the central repository.

First, it ensures that your local master branch is up to date by retrieving the copy from the central repository and reorganizing your changes on that basis.

Interactive reorganization is also a good opportunity to clean up your commits before sharing.

Next, the git push command sends all commits from your local master branch to the central repository


git checkout master
git fetch origin master
git rebase -i origin/master
# Squash commits, fix up commit messages etc.
git push origin master

Since we have already ensured that the local master branch is up to date, this action should result in a fast forward merge, and the git push command should not crash due to the non-fast forward issues discussed earlier.