Git Push
The git push
command is used to publish local commits to a remote repository. It uploads the content of the local repository to the remote repository. Pushing is the opposite of fetching while get fetch publish commits to the remote branches; git push
imports commits to local branches. The git remote command is used to configure remote repositories.
Note: The
git push
command can overwrite changes in the remote server, so caution should be taken when using this command.
Git push usage
The git push
command is generally used to upload the local changes to a remote repository. After changes are made to the local repository, we can execute the git push
command to share modifications with other members.
The following illustration shows the result of using the git push
command:

The above illustration shows the local master
ref progression past the remote repository master
ref. After running the git push origin master
, both the local and remote master
ref point to the same last commit.
Git push options
The git push
command has several options. The following list shows different use cases:
git push <remote> <branch>
: It will push the specified to with all commits and internal objects. This process will create a local branch in the remote repository. Git has a mechanism to prevent overwriting commits, so it will only accept a push if it results in a fast-forward merge in the destination repository.$ git push <remote> <branch>
git push <remote> --force
: It will force the push to the even if it results in a non-fast-forward merge. Avoid using the--force
unless you know what you are doing.$ git push <remote> --force
git push <remote> --all
: It will push all the local branches to the specified .$ git push <remote> -all
git push <remote> --tags
: It will push tags from the local branches to the repository. Tags are not automatically pushed even when using the--all
option. This is why the--tags
came in handy to upload tags to the remote.$ git push <remote> --tags
Git push and syncing
The git push
command is one of the commands associated with the "syncing" process. The syncing commands work on the remote branches that are configured using the git remote command. The git push
command upload commits to the remote and both the git fetch and git pull download from the remote. After changesets have been moved with a download or an upload, the git merge is required at the destination to integrate the modifications.
How to push to bare repositories
Generally, a central remote repository is created with the --bar
flag as a bare repository. Since bare repositories don't have a working directory, it is safe to push to bare repositories because we are sure that the push will not alter any in-progress working directory. To have more information about the bare repository, see the page about git init.
Force Pushing
Git may refuse a push request to a central repository if the result is a non-fast-forward merge. So if the history of the central repository does not match with the local meaning if there are some new commits in the central repository that are not yet downloaded to the local repository, you should pull the remote branch and merge it into the local repository, then run the git push
again.
To override this behavior, you can use the --force
flag to make the remote repository branch match the local repository by removing any upstream changes since the last pull. The only time you should use the git push
with the --force
flag is when the shared commits are not right, and they are fixed with the git commit --amend
or the interactive rebase. Before using the --force
, be sure that no one has pulled those commits.
The Default git push
The default steps to use the git push
command first ensure that the local master is up-to-date by fetching the central remote repository copy using the git fetch command. Then rebase the new changes on top of the local master branch. You can also use the interactive rebase to clean up commits before sharing them. And finally, the git push
command to send all of the commits on the local master branch to the central remote repository.
$ git checkout master
$ git fetch origin master
$ git rebase -i origin/master
# Squash commits
$ git push origin master
Using the interactive rebase, we had made sure that the local master is up-to-date, so running the git push
command will not be blocked by the central repository. And the git push
command will result in a fast-forward merge.
Amended force push
The --amend
flag is used with the git commit command to modify the previous commit. We use the git commit --amend
command to update the commit message or add new changes. After amending a commit, the git push
command will fail because the local branch will diverge from the remote branch. To push the local branch even if there is a divergence, you can use the --force
flag as follows:
# make changes to the repository, stage, and commit the modification
$ git commit --amend
# amend the last commit by updating its commit message
$ git push --force origin master
How to delete a remote branch or tag
In some situations, we need to remove branches. To fully delete a branch, it must be removed locally and also remotely.
$ git branch -D <branch_name>
$ git push origin :<branch_name>
The above commands will first delete the branch locally, and through passing the <branch_name>
prefixed with a colon to the git push
command, Git will delete the remote branch.