Log In

Git Push: How It Works and How to Use It

Git Push: How It Works and How to Use It
14.02.2024
Reading time: 5 min
Hostman Team
Technical writer

The git push command sends recent commits from the local repository to the remote repository on a server. It allows developers to update the main branch by adding new features or changing existing ones.

It is one of the four main commands for managing remote repositories. The other three are pull, fetch, and remote.

When and why do I need git push?

After a series of commits with important changes, you need to update the codebase in the remote repository. There are two reasons for this:

  • Keeping a backup copy on the server in case of data loss on the local machine.

  • Sending changes to members of the development team to synchronize the codebase.

Preparation before use

For the git push command to work, you must first link the local repository to the remote repository.

git remote add repository link

Instead of repository, enter the name of the remote repository. It can be arbitrary since it is only used on the local machine. However, most often, it is just origin.

Instead of link, enter the link to the remote repository. Its type depends on the access protocol: HTTPS or SSH.

How to use git push?

Remember that before pushing updates to a remote repository, you must commit all local changes using the git commit command.

You may not have any changes at the moment. To find out, use git status. If there are commits that you have not sent to the server, you will see a similar message in the console: 

Your branch is ahead of 'origin/master' by 3 commits.

If that's the case, push the latest changes:

git push repository branch

Here, repository is the name of the remote repository, and branch is the name of the branch to be pushed. If there is no such branch on the server, it will be automatically created. Most often, the main branch is called master.

git push origin master

The short form of the command sends the current (i.e., active) branch to the remote repository.

git push

For example, if the current branch is master, that's what will be sent to the server. So always perform a check via git status to make sure you are on the target branch.

You can perform the same operation by using a longer form of the command with a HEAD pointer that references the active branch, so you don't have to remember its name.

git push origin HEAD

If you check the status via git status after submitting changes, you'll see that the branch is up-to-date:

Your branch is up-to-date with 'origin/master'.

Git push to another branch

Sometimes, you want to push a local branch to a completely different branch in a remote repository. To do this, enter the branches' names consecutively, with a colon between them.

git push origin master:remote_branch

In this case, master is the name of the local branch, and remote_branch is the same branch in the remote repository but with a different name. As with regular push, if remote_branch doesn't exist, Git will create it. So, basically, this command renames a local branch in a remote repository.

Git push all branches at once

To push all branches with unsaved commits to a remote repository at once, use the --all flag instead of the branch name.

git push origin --all

Git will send fresh commits to the corresponding branches in the remote repository. If some of them don't exist, Git will create them.

Force push in Git (be careful!)

Sometimes, it is impossible to perform a quick branch merge in a remote repository. Therefore, it is quite common to overwrite the branch with a force push.

However, for security reasons, Git does not allow this operation by default. For example, if a remote repository has more recent third-party commits, you must first pull them and merge.

So, to overwrite the remote branch, you need to add the --force flag to the push command. After that, the commit history from the local repository will be copied to the remote repository, replacing all previous entries.

git push origin master --force

For this reason, the --force flag requires extra caution. Forcing an overwrite can remove features added by other developers. Therefore, using this flag is considered undesirable. Forced overwriting should be taken only as a last resort.

By the way, if the previous commit of the local repository was fixed using the --amend flag, you will not be able to do a push; the fixed commit will be different from the one in the remote repository.

Therefore, you will have to use the same --force flag, which will forcibly overwrite the fixed commit in the remote repository.

Pushing tags

If your project uses tags to mark codebase versions, you may need to git push a tag to a remote repository. The thing is that Git does not push tags to remote servers on its own.

Therefore, just as with branches, use the git push command for this purpose.

git push origin tagname

Instead of tagname, enter your tag. For example, if you have a v1.5 tag showing the product version, the command to send it to the repository would look like this:

git push origin v1.5

You can use the --tags flag to send all tags to a remote repository at once.

git push origin --tags

These tags will now be available to anyone who clones your repository via git clone or gets fresh updates via git pull.

Deleting a branch or tag in a remote repository

Oddly enough, to erase a branch in a remote repository, you also need to perform git push, but with different parameters.

The format is the same as renaming a branch (i.e., sending the local branch to the server under a different name) but without the local branch name before the colon.

git push origin :branch_name

To avoid confusion, imagine that you are sending an emptiness (as you didn't specify a local branch name) to the remote branch, thus clearing it.

However, as of Git v1.7.0, you can use the --delete flag followed by the name of the branch you need to remove.

git push origin --delete branch_name

Deleting a tag is no different than deleting a branch: just enter the tag's name. Both the colon method and the --delete flag work.

By the way, Git allows you to delete multiple branches or tags simultaneously.

git push origin --delete branch_name1 branch_name2 branch_name3

Conclusion

This article has shown you the basic uses of the git push command. With them, you can accomplish most of the tasks related to sending data to the server, and only in rare cases will you need to use additional flags.

You can find more information about the parameters of this command in the official Git documentation.


Share