How Does The Git Push Command Work?

Tim Mouskhelichvili
Tim Mouskhelichvili
4 minutes to read

Git makes it simple to collaborate on a repository for multiple developers. It provides many easy-to-use commands to sync local changes with the remote repository. One of those commands is the git push command. The git push command updates the remote server with your local changes.

To push a current branch to a remote server, use:

bashgit push

To push a specific branch to a specific remote server, use:

bashgit push <remote> <branch>

This article will explain the git push command and its different options, show various use-cases, and answer some of the most common questions.

Let's get to it 😎.

git push

The definition

The git push command pushes local commits to a remote branch.

Additionally, this command can:

Before running this command, you must first commit your local changes with the git commit command.

To run this command, you need to open a terminal from your Git repository's location and type:

bashgit push

By default, the git push command pushes the current branch. To view the current branch, use the git status command.

If you want to update a different branch than the current one, specify the branch's name.

bashgit push <remote> <branch>

The options

The git push command supports many different options to alter its behavior.

OptionDescription
--allPushes all the branches.
--pruneRemoves all the remote branches that don't have a local equivalent.
-n
--dry-run
Git will do everything except send the update. This command is used to verify that everything works before sending an update.
-d
--delete
The listed refs will be DELETED from the remote.
--tagsThe git tags will be pushed to the remote.
-f
--force
Forces a push that is blocked otherwise. It will overwrite a remote branch with a local branch.
--force-with-lease=<refname>Safer option than the force option because it will not overwrite any work on the remote branch.
-q
--quiet
Hides the output of the command.
-v
--verbose
Runs verbosely (with logs).
--no-verifyBypasses the pre-push hook.
-u
--set-upstream
Adds an upstream tracking reference on the remote. Useful when pushing a new local branch to the remote server.

How to push a specific branch?

To push to a specific branch, you need to specify the branch name with the git push command.

bashgit push <remote> <branch>

How to push all the local branches?

To push all the local branches, use the git push --all command.

bashgit push --all

How to push a new local branch to a remote?

To push a new local branch to a remote, you will need to add a new upstream tracking reference with the git push -u command.

bashgit push -u <remote> <branch>

Or you can use the equivalent --set-upstream option.

bashgit push --set-upstream <remote> <branch>

This command will set up tracking information for the branch on the remote server.

How to force push?

git push force

Sometimes you want to ignore changes made on the remote repository and override them with local changes.

That's why the force option exists.

bashgit push --force <remote> <branch>

Be careful when using git push with the force option because it may create irreversible damage.

A safer alternative to the force option is the force with a lease option. This option has the same behavior as the force option but will not override remote changes.

How to fix "non-fast-forward" errors?

Sometimes another developer pushes a commit on the same branch as you. If you do not pull the changes before pushing, you will get a "non-fast-forward" error when you try to push your changes.

git push non fast forward error

You will need to fetch and merge the remote repository's changes to fix this error.

You can fetch and merge remote changes by using those two commands:

bash> git fetch <remote>
> git merge <remote> <branch>

Or by using the git pull command that performs both commands at once.

bashgit pull origin <branch>

I wrote a complete guide on the difference between git pull vs git fetch here.

How to rename a git branch?

To rename a git branch, use the git push command with the new branch name.

bashgit push <remote> <local_branch>:<remote_branch_name>

This command will push the branch called local_branch to the remote but change its name to remote_branch_name.

How to push tags?

To push local git tags to the remote repository, use the git push --tags command.

bashgit push --tags

This command will push all the local tags that aren't on the remote repository to the remote server.

My git push hangs, what do I do?

Your git push can hang or be stuck for many different reasons.

Read this article, to debug this error.

Final thoughts

git push

As you can see, it is very easy to update the remote repository with your local changes. The git push command offers a simple way to do it and offers many different options to customize its behavior.

Comments (0)
Reply to: