w3docs logo


The git push command uploads the content of the local repository to the remote repository. Pushing is the opposite of fetching. If git fetch imports the content to the local branches, git push exports it to the remote branches.

Git push usage

The git push command is commonly used to publish the upload local changes to the central repository. After the changes are made in the local repository, you can git push to share the modification with other members of the team. The git pushcommand is one of the commands that are involved in the "syncing" process. These commands work on the remote branches that are configured with the git remote command. The commits are uploaded with git push and download with git fetch and git pull. After making changes in both cases, git merge is used to integrate changes.

The following diagrams show the progression of the local master past the central repository’s master and publishing changes by invoking git push origin master.

git push

Common options

git push <remote> <branch> Pushes the specified branch to <remote> with necessary commits creating a local branch in the destination repository.
git push <remote> --force Forces the push even if it results in a non-fast-forward merge. Be sure that nobody has pulled the commits before using the --force option.
git push <remote> --all Pushes all of the local branches to the remote repository.
-git push <remote> --tags Pushes the tags from the local branches to the remote repository. The --all won’t push the tags.

How to push to bare repositories

It is very safe to push to repositories that are created with --bare flag since it doesn’t allow to edit files and commit changes to that repository. It is recommended to create central repositories as bare ones otherwise pushing to a non-bare repository can overwrite changes.

Force pushing

Git can refuse your push request if the history of the central repository does not match the local one. In these cases, you should pull the remote branch and merge it into the local repository then push again. The --force flag matches the remote repository’s branch and local one cleaning the upstream changes from the last pull. Use force push when the shared commits are not right and are fixed with git commit --amend or an interactive rebase. Before using the --force option, be sure that nobody has pulled the commits. The interactive rebase is also a good way to clean up the commits before sharing. The git commit --amend option updates the previous commit. When the commit is amended git pushwill fail because Git will accept the amended commit and the remote commit as diverged content. Here, --force must be used to push the amended commit.

# make changes to a repo and git add
git commit --amend
# update the existing commit message
git push --force origin master

How to delete a remote branch

Here is an example of deleting the remote branch. The branch_name prefixed with a colon to git push will delete the remote branch:

git branch -D branch_name
git push origin :branch_name

Do you find this helpful?