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

$1 Domain Names – Register yours today!
  • Simple reg­is­tra­tion
  • Premium TLDs at great prices
  • 24/7 personal con­sul­tant included
  • Free privacy pro­tec­tion for eligible domains

What is Git Push?

With Git you have the pos­si­bil­i­ty to work on a project in peace and without any risk, while at the same time your team members also continue their re­spec­tive tasks. The use of in­di­vid­ual repos­i­to­ries 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 repos­i­to­ry to the remote repos­i­to­ry, which in turn is ac­ces­si­ble to the other de­vel­op­ers. 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­spec­tive­ly. While these two commands allow you to pull content from a remote repos­i­to­ry to your local repos­i­to­ry, Git Push goes the other way. Through these commands, many op­er­a­tions take place si­mul­ta­ne­ous­ly - in this way, Git differs from SVN. Once your changes are complete, you then simply upload them to the remote repos­i­to­ry. It is important to note that Git Push over­writes other changes. The command should be used with care ac­cord­ing­ly.

Syntax and func­tion­al­i­ty of Git Push

Now to explain the syntax of Git Push. First you write the command itself, then you specify the remote repos­i­to­ry 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 repos­i­to­ry and uploads all content, commits, and internal objects. However, as a pro­tec­tive 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­den­tal­ly 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 repos­i­to­ry, tested them ex­ten­sive­ly and found them to be good. The next step is to have these changes ready for the remote repos­i­to­ry 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 repos­i­to­ry matches the state of the central repos­i­to­ry. The Git Rebase command merges the state of the central repos­i­to­ry with that of your local repos­i­to­ry. 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 repos­i­to­ry.

Further uses for the command

In addition to the default command explained above, there are several other pos­si­bil­i­ties 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 repos­i­to­ry. 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 repos­i­to­ry. Tags are not included in an ordinary Git push.

Force Git Push

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

This command cor­re­sponds 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 Repos­i­to­ry

To ensure that larger projects are not corrupted by Git Push, it is rec­om­mend­ed to push only to empty repos­i­to­ries. You create these with the “—bare” option. When you push to empty repos, the changes and progress of other de­vel­op­ers, as well as the structure of the remote repos­i­to­ry, remain un­af­fect­ed.

Delete branches with Git Push

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

git branch -d alter_branch
git push origin :alter_branch

To delete the alter_branch branch from the remote repos­i­to­ry.

Tip

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

Go to Main Menu