The Git Push command uploads content from a local re­pos­it­ory to a central re­pos­it­ory. Because this command over­writes changes, it should only be executed with an empty branch as the target.

Cheap domain names – buy yours now
  • Free website pro­tec­tion with SSL Wildcard included
  • Free private re­gis­tra­tion for greater privacy
  • Free Domain Connect for easy DNS setup

What is Git Push?

With Git you have the pos­sib­il­ity to work on a project in peace and without any risk, while at the same time your team members also continue their re­spect­ive tasks. The use of in­di­vidu­al re­pos­it­or­ies makes it possible. However, when you have completed your changes and want to integrate them into the entire project, the Git Command Git Push helps you to do this. Using this command, you upload content from your local re­pos­it­ory to the remote re­pos­it­ory, which in turn is ac­cess­ible to the other de­velopers. Git Push is a very important tool for the success of your project.

The command works as a coun­ter­part to Git Fetch and Git Pull re­spect­ively. While these two commands allow you to pull content from a remote re­pos­it­ory to your local re­pos­it­ory, Git Push goes the other way. Through these commands, many op­er­a­tions take place sim­ul­tan­eously - in this way, Git differs from SVN. Once your changes are complete, you then simply upload them to the remote re­pos­it­ory. It is important to note that Git Push over­writes other changes. The command should be used with care ac­cord­ingly.

Syntax and func­tion­al­ity of Git Push

Now to explain the syntax of Git Push. First you write the command itself, then you specify the remote re­pos­it­ory and after that the local branch you want to upload it to. This looks like this when written out:

git push <remote-repository> <local-branch></local-branch></remote-repository>

Git then creates a local branch in the target re­pos­it­ory and uploads all content, commits, and internal objects. However, as a pro­tect­ive feature, the version control system blocks the Git Push if the upload cannot be performed in a fast-forward merge. This prevents commits from being ac­ci­dent­ally over­writ­ten by the command.

Examples of Git Push

In the following example, we will show you what a Git Push looks like in practice. For this we assume that you have made changes in your local re­pos­it­ory, tested them ex­tens­ively and found them to be good. The next step is to have these changes ready for the remote re­pos­it­ory so that the other members of your team can access them as well. To do this, we now use various commands that you always have at hand in the handy Git Cheat Sheet for PDF download.

git checkout main
git fetch origin main
git rebase -I origin/main
# Squash commits, fix up commit messages etc.
git push origin main

Using the command Git Checkout, we’ll switch to the main branch. With Git Fetch we make sure that the local re­pos­it­ory matches the state of the central re­pos­it­ory. The Git Rebase command merges the state of the central re­pos­it­ory with that of your local re­pos­it­ory. This makes it possible to perform a fast-forward merge later, since the base of the central and local repos are the same. Then you perform the actual Git Push, which pushes your current state, including all changes, to the central re­pos­it­ory.

Further uses for the command

In addition to the default command explained above, there are several other pos­sib­il­it­ies to use Git Push well.

Upload all branches

git push <remote-repository> --all</remote-repository>

This command is similar to the default, but instead of uploading a selected branch, it uploads all local branches directly to the central re­pos­it­ory. This way, you do not have to do this work piece by piece.

Upload Tags

git push <remote-repository> --tags</remote-repository>

With this option you send all local tags to the remote re­pos­it­ory. Tags are not included in an ordinary Git push.

Force Git Push

git push <remote-repository> --force</remote-repository>

This command cor­res­ponds to the standard version, but also performs the Git push if no fast-forward merge is possible. Since this increases the risk that you might overwrite changes and cause problems, you should avoid using this variant if possible.

Git Push in an empty Re­pos­it­ory

To ensure that larger projects are not corrupted by Git Push, it is re­com­men­ded to push only to empty re­pos­it­or­ies. You create these with the `—bare´ option. When you push to empty repos, the changes and progress of other de­velopers, as well as the structure of the remote re­pos­it­ory, remain un­af­fected.

Delete branches with Git Push

You can also use Git Push to delete a branch locally and remotely. The cor­res­pond­ing command looks like this:

git branch -d alter_branch
git push origin :alter_branch

To delete the alter_branch branch from the remote re­pos­it­ory.

Tip

Deploy websites and apps online in just three steps! With Deploy Now from IONOS, you’ll benefit from a fast setup and maximum scalab­il­ity! Book now and try Deploy Now free for a month.

Go to Main Menu