Push a Branch
git push origin main
origin is the name of the remote, usually the one you cloned from, and main is the branch. Git sends the commits the remote does not have yet and moves the remote branch forward. Check which remotes exist and where they point:
git remote -v
To point a remote at a new address, see how to change a Git remote URL.
The First Push of a New Branch
git push -u origin feature/login
The push creates the branch on the remote, and -u (long form --set-upstream) records it as the upstream of your local branch. After that, git push and git pull without arguments use it. git branch -vv shows each branch's upstream and how many commits it is ahead or behind.
Push Settings for New Branches
When you run git push with no arguments, the setting push.default decides what is pushed. Its default, simple, pushes only the current branch to the branch of the same name on the remote; when you push to the remote you pull from, the branch must also have an upstream with that same name. Leave it at that unless you have a reason.
To skip the -u step for new branches, recent Git versions offer:
git config --global push.autoSetupRemote true
With it, the first plain git push of a new branch creates the remote branch and sets the upstream.
Push Tags
A normal push does not send tags. Push them on purpose:
| Command | What it pushes |
|---|---|
git push origin v1.2.0 | One tag |
git push origin --tags | All local tags |
git push --follow-tags | Commits plus their annotated tags |
Creating and deleting tags is covered in git tag: create, push, and delete tags.
Delete a Remote Branch
git push origin --delete feature/old
This removes the branch on the remote only. Your local branch stays until you delete it as well; see Git delete branch: local and remote.
A Rejected Push
If someone else pushed to the branch since your last fetch, the remote refuses your push because it is not a fast-forward: accepting it would drop their commits. Get their work first, then push again:
git fetch origin
git rebase origin/main
git push
Use git merge origin/main in place of the rebase if your team prefers merge commits. git pull does the fetch and the integration in one step, but on current Git versions it only fast-forwards by default and stops when the branches have diverged, until you set pull.rebase or pull.ff. The difference between the two commands is in git pull vs git fetch.
Do not answer a rejection with a force push on a shared branch. When rewriting a branch is really needed, follow how to force push safely.
Authentication
- SSH key: use a remote address such as
ssh://git@your_git_host/user/project.gitand add your public key to your account on your Git hosting platform. The steps are in GitHub SSH Key: Set Up on a Linux Server. - HTTPS with a token: with an address such as
https://example.com/user/project.git, most Git hosting platforms expect a personal access token in place of your account password. Give the token only the access it needs.
To avoid typing the token on every push, a credential helper can remember it. git config --global credential.helper cache keeps it in memory, by default for 15 minutes. The store helper writes it unencrypted to ~/.git-credentials, protected only by file permissions, so use it only on a machine nobody else can read.
Check Before You Push
git log --oneline origin/main..main
git push --dry-run
The first line lists the commits that a push would send; the second shows what would happen without sending anything.