Git push usage
git push <remote> <branch>
Push the specified branch to
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.
Git push and syncing
Pushing to bare repositories
Git prevents you from overwriting the central repository’s history by refusing push requests when they result in a non-fast-forward merge. So, if the remote history has diverged from your history, you need to pull the remote branch and merge it into your local one, then try pushing again. This is similar to how SVN makes you synchronize with the central repository via
svn update before committing a changeset.
--force flag overrides this behavior and makes the remote repository’s branch match your local one, deleting any upstream changes that may have occurred since you last pulled. The only time you should ever need to force push is when you realize that the commits you just shared were not quite right and you fixed them with a
git commit --amend or an interactive rebase. However, you must be absolutely certain that none of your teammates have pulled those commits before using the
Default git push
The following example describes one of the standard methods for publishing local contributions to the central repository. First, it makes sure your local master is up-to-date by fetching the central repository’s copy and rebasing your changes on top of them. The interactive rebase is also a good opportunity to clean up your commits before sharing them. Then, the
git push command sends all of the commits on your local master to the central repository.
git checkout main
git fetch origin main
git rebase -i origin/main
# Squash commits, fix up commit messages etc.
git push origin main
Since we already made sure the local master was up-to-date, this should result in a fast-forward merge, and
git push should not complain about any of the non-fast-forward issues discussed above.
Amended force push
# make changes to a repo and git add
git commit --amend
# update the existing commit message
git push --force origin main
The above example assumes it is being executed on an existing repository with a commit history.
git commit --amend is used to update the previous commit. The amended commit is then force pushed using the
Deleting a remote branch or tag
Sometimes branches need to be cleaned up for book keeping or organizational purposes. The fully delete a branch, it must be deleted locally and also remotely.
git branch -D branch_name
git push origin :branch_name
The above will delete the remote branch named branch_name passing a branch name prefixed with a colon to
git push will delete the remote branch.