The git push command is used to upload content from the local repository to a remote repository. Push is the way to transfer commits from your local repository to a remote repository. It is the counterpart of git fetch, but while sourcing imports commits to local branches, pushing exports commits to remote branches. Remote branches are configured using the git remote command. Pushing has the potential to overwrite changes, caution should be exercised when pushing. These issues are discussed below.
Using Git
push Insert the specified branch in , along with all required commits and internal objects. This creates a local branch in the target repository. To prevent you from overwriting commits, Git will not allow you to push when it results in a non-fast merge to the target repository.
Same as the previous command, but forces insertion even if it results in a merge that doesn’t move quickly. Don’t use the -force indicator unless you’re absolutely sure you know what you’re doing.
Insert all local branches into the specified remote control.
Tags are not inserted automatically when you insert a branch or use the -all option. The -tags flag sends all local tags to the remote repository.
Git push discussion Git push
is most commonly used to publish and upload local changes to a central repository. After you modify a local repository, you run an push to share the modifications with remote team members.
The diagram above shows what happens when your local main has progressed beyond the main of the central repository and publishes changes by running git push origin main. Notice how git push is essentially the same as running git merge main from inside the remote repository.
Git push and
synchronization git push is one of many components used in the overall Git “sync” process. Synchronization commands operate on remote branches that are configured by using the git remote command. Git Push can be considered and ‘upload’ command, while git fetch and git pull can be considered as ‘download’ commands. Once change sets have been moved through a download or upload, a git merge can be performed on the target to integrate the changes.
A
frequently used modern Git practice is to have a remotely hosted -bare repository as a central source repository. This source repository is often hosted offsite with a trusted 3rd party like Bitbucket. Since pushing messes with the remote branch structure, it is safer and more common to push repositories that have been created with the bare-flag. Bare repositories do not have a working directory, so an insert will not alter any WIP directory contents. To learn more about creating bare repositories, read about git init.
Force push
Git prevents you from overwriting the history of the central repository by rejecting push requests when they result in a non-fast merge. Therefore, if the remote history has deviated from your history, you need to extract the remote branch and merge it with the local one, then try pushing again. This is similar to how SVN makes you synchronize with the central repository through the svn update before committing a change set.
The -force flag overrides this behavior and matches the remote repository branch to the local branch, eliminating any upward changes that may have occurred since the last time you checked out the image. The only time you should need to force the push is when you realize that the commits you just shared weren’t quite correct and fix them with a git commit -amend or an interactive rebase. However, you should be absolutely sure that none of your teammates have made those confirmations before using the -force option.
Default git push
examples
The following example describes one of the standard methods for publishing local contributions to the central repository. First, you make sure your local primary is up to date by getting the copy from the central repository and re-basing your changes on them. The interactive rebase is also a good opportunity to clean up your confirmations before sharing them. The git push command then sends all commits on your local master to the central repository.
Since we already made sure the main local was up to date, this should result in a fast-forward merger, and git push shouldn’t complain about any of the non-fast issues discussed above.
Modified forced insert
The git commit command accepts a -amend option that will update the previous commit. A confirmation is often modified to update the confirmation message or add new changes. Once a commit is modified, the git push will fail because Git will see the modified commit and remote commit as divergent content. The -force option must be used to send a modified confirmation.
The previous example assumes that you are running in an existing repository with a commit history. git commit -amend is used to update the previous commit. The modified commit is forced to push using the -force option.
Deleting a remote branch or tag
Sometimes branches need to be cleaned for accounting or organizational purposes. When completely deleting a branch, it must be deleted locally and also remotely.
This will delete the remote branch
named branch_name passing a branch name with the colon to git push will delete the remote branch.