Definition

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

The command git push origin :branch_name deletes the specified branch (branch_name) from the remote repository (origin) by pushing an empty reference to it.

Here's how it works:

  1. git push: push changes from your local Git repository to a remote repository
  2. origin: the name of the remote repository
  3. :branch_name: a refspec that represents an empty reference, effectively deleting the specified branch from the remote repository

So when you run git push origin :branch_name, Git will delete the branch_name branch from the origin remote repository.

Note that this command can be dangerous if used incorrectly, as it will delete the branch without any confirmation or possibility of recovery. Make sure you double-check the branch name and that you really want to delete it before running this command.

Use -u flag for the first push on a branch

When you have a local branch and you want to push it to the remote repository for the first time, you should specify which branch of the remote repository you mean. In Git, the -u flag is used with the git push command to set the upstream branch for the current branch. When you use the -u flag, Git will create a link between your local branch and the remote branch. This link is useful for simplifying the git pull and git push commands in the future, as it allows Git to remember which remote branch corresponds to your local branch.

The -u flag is short for --set-upstream. When you use this flag, you'll typically see it used like this:

git push -u origin your-branch-name

Examples of some mostly used flags:

Here are some examples of how to use some of the git push flags:

-f flag:

git push -f origin master

This will force push the local master branch to the origin remote repository, overwriting any changes that may have been made to the master branch in the remote repository. Use this flag with caution, as it can cause problems if multiple people are working on the same branch.

--tags flag:

git push --tags origin

This will push all tags to the origin remote repository. Tags are used to label important points in your project's history, such as releases or milestones.

--all flag:

git push --all origin

This will push all local branches to the origin remote repository.

--dry-run flag:

git push --dry-run origin master

This will simulate a push to the master branch of the origin remote repository, without actually pushing anything. This can be useful for testing and checking what changes will be pushed before actually pushing them.

--force-with-lease flag:

git push --force-with-lease origin master

This will force push the local master branch to the origin remote repository, while ensuring that you don't overwrite changes that may have been made to the master branch in the remote repository since your last pull. This is a safer alternative to using the -f flag.

Practice Your Knowledge

Which statements are true regarding the 'git push' command?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.