Git Push Commands
Overview
The git push
command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repository. This document covers the basic and advanced usage of git push
.
Basic Push
git push
Pushes the current branch to the configured upstream branch of the remote repository.
Example
This command pushes your current branch to its upstream branch on the remote repository.
git push <remote> <branch>
Pushes a specific branch to a specific remote repository.
Example
This command pushes the main
branch to the origin
remote.
Setting Upstream Branch
git push -u <remote> <branch>
Sets the upstream branch for the current branch and pushes the branch.
Example
This command pushes the feature-branch
to the origin
remote and sets it as the upstream branch for the current branch.
Force Push
git push --force
Forces the push to the remote repository, overwriting the remote branch with your local branch.
Example
This command forcefully pushes the main
branch to the origin
remote, overwriting any conflicting changes.
git push --force-with-lease
Forces the push but only if the remote branch has not been updated since the last pull.
Example
This command forcefully pushes the main
branch to the origin
remote but only if there are no new commits on the remote main
branch.
Pushing Tags
git push --tags
Pushes all tags to the remote repository.
Example
This command pushes all your local tags to the remote repository.
Deleting a Remote Branch
git push <remote> --delete <branch>
Deletes a branch from the remote repository.
Example
This command deletes the feature-branch
from the origin
remote.
Pushing to Multiple Remotes
git push <remote1> <branch> && git push <remote2> <branch>
Pushes a branch to multiple remote repositories.
Example
This command pushes the main
branch to both the origin
and backup
remotes.
Summary
The git push
command is essential for transferring commits from your local repository to a remote repository. It can be used in various ways to suit different workflows, from basic pushing to force pushing and pushing tags. For more detailed information on each command, refer to the official Git documentation.