The git push
command uploads your local repository content to a remote repository. This allows others to see and collaborate on your work. Once connected to a remote repository using the git remote
command, you can push your changes to it.
Initial Example
To push changes from your local branch to a remote repository, use:
git push -u <short_name> <your_branch_name>
PUSH Options
Option | Description |
---|---|
-u, --set-upstream | Set the upstream (tracking) reference for the current branch. |
-f, --force | Force the push, even if it results in a non-fast-forward update. |
--all | Push all local branches to the remote repository. |
--tags | Push all tags to the remote repository. |
--atomic | Ensure that the push is atomic, meaning either all refs are updated or none are. |
--dry-run | Perform a trial run to show what would be pushed without making any actual changes. |
--receive-pack=<git-receive-pack> | Specify the git-receive-pack program to use. |
--repo=<repository> | Specify the remote repository to push to. |
--quiet | Suppress all output, except for errors. |
--verbose | Show more information about what is being pushed. |
--force-with-lease | Force the push, but only if the remote branch matches the expected commit. |
--no-verify | Skip pre-push, commit, and/or post-receive checks. |
--push-option=<string> | Pass a custom option to the remote repository. |
--signed=(true|false|if-asked) | Sign the push with GPG, if true, or skip signing if false. |
--force-if-includes | Force the push if the remote branch includes the commit being pushed. |
Examples
1. Pushing to a Remote Repository
To push your local branch feature_branch
to the remote repository named origin
, use:
git push -u origin feature_branch
This command uploads your local changes to the remote repository and sets origin
as the upstream for feature_branch
.
2. Setting Upstream Branch
If you haven’t set up an upstream branch yet, you can do so with:
git push --set-upstream <short_name> <branch_name>
For example:
git push --set-upstream origin feature_branch
This sets origin
as the upstream for feature_branch
, making future pushes simpler.
3. Pushing All Branches
To push all your local branches to the remote repository, use:
git push --all origin
This ensures that all your branches are updated on the remote repository.
4. Pushing Tags
To push tags to the remote repository, use:
git push origin --tags
This sends all your local tags to the remote repository.
5. Forcing a Push
If you need to overwrite the remote branch with your local branch, you can force the push using:
git push origin feature_branch --force
Be cautious when using this command, as it can overwrite changes in the remote repository.
6. Deleting a Remote Branch
To delete a branch in the remote repository, use:
git push origin --delete <branch_name>
For example, to delete a branch named feature_branch
, use:
git push origin --delete feature_branch