Use git push to send the commits you made locally to a remote repository such as GitHub. The basic form is git push origin main, which uploads the commits of your local main branch to the remote named origin. Git only sends commits the remote does not have yet, and it refuses the push if that would overwrite someone else's work.
Before you push, the repository needs a remote and you need permission to write to it. This article covers the step that publishes your commits.
Every commit you create lives only in your local repository until you push it. git push compares your branch with the matching branch on the remote, finds the commits the remote is missing, uploads them, and then moves the remote branch to point at your newest commit.
The command takes two arguments: the name of the remote and the branch to send.
Here origin is the default name Git gives the remote you cloned from or added, and main is the branch. You can list your remotes with git remote -v.
Follow these steps for a new local project.
main.The -u flag (short for --set-upstream) links your local main to origin/main. After that, a plain git push and git pull on this branch know where to go.
Typical output looks like this:
GitHub asks you to authenticate on the first push. Over HTTPS you use a personal access token, and over SSH you use an SSH key; both have their own articles.
Once the upstream is set, daily work is short.
Other forms you will use:
| Command | Effect |
|---|---|
git push | Pushes the current branch to its upstream |
git push origin feature/login | Pushes a named branch, creating it on the remote if needed |
git push -u origin feature/login | Same, and sets the upstream for later pushes |
git push --tags | Pushes all local tags |
git push origin v1.0.0 | Pushes a single tag |
git push --all origin | Pushes every local branch |
The most common error is a non-fast-forward rejection.
It means someone (or you, from another machine) pushed commits that are not in your local branch. Git refuses to push because that would discard them. The fix is to bring those commits in first.
git pull fetches and integrates the remote changes. If both sides edited the same lines you resolve the conflict, finish the rebase or merge, and then push again.
Sometimes you rewrite history, for example after git commit --amend or an interactive rebase on your own feature branch. Git then rejects a normal push because your history no longer extends the remote one. You can overwrite the remote branch with a force push, but use the safer variant.
--force-with-lease only overwrites the remote branch if it still points where you last saw it. A plain --force overwrites whatever is there, including a teammate's new commits. Never force push to a shared branch such as main; many teams turn on branch protection on GitHub to block it.
git commit records a snapshot in your local repository only. git push uploads those local commits to a remote so others can see them.
You are using an HTTPS remote URL. GitHub no longer accepts account passwords for Git operations, so enter a personal access token, or switch the remote to SSH with git remote set-url origin [email protected]:your-name/demo.git.
It sets the upstream tracking branch, so later git push and git pull commands work without naming the remote and branch.
Yes. Use git push origin local-name:remote-name. The first part is your local branch and the second part is the branch to update on the remote.
git push publishes local commits to a remote. Use git push -u origin main the first time, then plain git push. If Git rejects the push, pull the remote changes first. When you must rewrite history on your own branch, prefer --force-with-lease over --force.