mirror of
https://github.com/mintlify/docs.git
synced 2026-09-14 13:35:46 +08:00
202 lines
3.3 KiB
Plaintext
202 lines
3.3 KiB
Plaintext
---
|
|
title: git push
|
|
description: Update remote refs along with associated objects
|
|
---
|
|
|
|
Upload local commits to a remote repository.
|
|
|
|
## Syntax
|
|
|
|
```bash
|
|
git push [options] [<repository>] [<refspec>]
|
|
```
|
|
|
|
## Description
|
|
|
|
`git push` transfers commits from your local repository to a remote repository. This makes your changes available to others and can trigger automated deployments.
|
|
|
|
## Common usage
|
|
|
|
Push to the default remote and branch:
|
|
|
|
```bash
|
|
git push
|
|
```
|
|
|
|
Push and set upstream tracking:
|
|
|
|
```bash
|
|
git push -u origin main
|
|
```
|
|
|
|
Push a specific branch:
|
|
|
|
```bash
|
|
git push origin feature-branch
|
|
```
|
|
|
|
## Options
|
|
|
|
<ParamField path="-u, --set-upstream" type="flag">
|
|
Set upstream tracking for the branch
|
|
|
|
```bash
|
|
git push -u origin main
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--force" type="flag">
|
|
Force push, overwriting remote history (use with caution)
|
|
|
|
```bash
|
|
git push --force
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--force-with-lease" type="flag">
|
|
Safer force push that checks for unexpected changes
|
|
|
|
```bash
|
|
git push --force-with-lease
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--all" type="flag">
|
|
Push all branches
|
|
</ParamField>
|
|
|
|
<ParamField path="--tags" type="flag">
|
|
Push all tags
|
|
</ParamField>
|
|
|
|
<ParamField path="--delete" type="flag">
|
|
Delete a remote branch
|
|
|
|
```bash
|
|
git push origin --delete branch-name
|
|
```
|
|
</ParamField>
|
|
|
|
## Examples
|
|
|
|
### First push of a new branch
|
|
|
|
```bash
|
|
git push -u origin feature-x
|
|
```
|
|
|
|
The `-u` flag sets up tracking so future pushes only need `git push`.
|
|
|
|
### Push to a different remote
|
|
|
|
```bash
|
|
git push staging main
|
|
```
|
|
|
|
### Push all branches
|
|
|
|
```bash
|
|
git push --all origin
|
|
```
|
|
|
|
### Push tags
|
|
|
|
```bash
|
|
git push --tags
|
|
```
|
|
|
|
### Delete a remote branch
|
|
|
|
```bash
|
|
git push origin --delete old-branch
|
|
```
|
|
|
|
### Force push (use carefully)
|
|
|
|
```bash
|
|
# Safer option
|
|
git push --force-with-lease
|
|
|
|
# Standard force push
|
|
git push --force
|
|
```
|
|
|
|
<Warning>
|
|
Force pushing rewrites history on the remote. Only use it when you're certain no one else is working with the same branch.
|
|
</Warning>
|
|
|
|
## Common scenarios
|
|
|
|
### First time pushing a branch
|
|
|
|
```bash
|
|
git checkout -b new-feature
|
|
git add .
|
|
git commit -m "Add new feature"
|
|
git push -u origin new-feature
|
|
```
|
|
|
|
### Regular workflow
|
|
|
|
```bash
|
|
git add .
|
|
git commit -m "Update feature"
|
|
git push
|
|
```
|
|
|
|
### Pushing after rebase
|
|
|
|
After rebasing, the branch history has changed:
|
|
|
|
```bash
|
|
git rebase main
|
|
git push --force-with-lease
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Rejected push
|
|
|
|
If your push is rejected, someone else pushed changes:
|
|
|
|
```bash
|
|
git pull --rebase
|
|
git push
|
|
```
|
|
|
|
### Branch not tracking remote
|
|
|
|
Set upstream tracking:
|
|
|
|
```bash
|
|
git push -u origin current-branch
|
|
```
|
|
|
|
### Permission denied
|
|
|
|
Check your authentication:
|
|
|
|
```bash
|
|
# For HTTPS
|
|
git remote -v
|
|
git remote set-url origin https://github.com/username/repo.git
|
|
|
|
# For SSH
|
|
git remote set-url origin git@github.com:username/repo.git
|
|
```
|
|
|
|
## Best practices
|
|
|
|
- Always pull before pushing: `git pull` then `git push`
|
|
- Use `--force-with-lease` instead of `--force`
|
|
- Don't force push to shared branches like `main`
|
|
- Set up upstream tracking with `-u` on first push
|
|
- Push commits frequently to back up your work
|
|
|
|
## Related commands
|
|
|
|
- [git pull](/git/pull) - Fetch and merge remote changes
|
|
- [git fetch](/git/fetch) - Download remote changes
|
|
- [git commit](/git/commit) - Create commits to push
|
|
- [git remote](/git/remote) - Manage remote repositories
|