Pushing commits to a remote repository
Usegit push
to push commits made on your local branch to a remote repository.
In this article
Aboutgit push
Thegit push
command takes two arguments:
- A remote name, for example,
origin
- A branch name, for example,
main
For example:
git push REMOTE-NAME BRANCH-NAME
As an example, you usually rungit push origin main
to push your local changesto your online repository.
Renaming branches
To rename a branch, you'd use the samegit push
command, but you would addone more argument: the name of the new branch. For example:
git push REMOTE-NAME LOCAL-BRANCH-NAME:REMOTE-BRANCH-NAME
This pushes theLOCAL-BRANCH-NAME
to yourREMOTE-NAME
, but it is renamed toREMOTE-BRANCH-NAME
.
Dealing with "non-fast-forward" errors
If your local copy of a repository is out of sync with, or "behind," the upstreamrepository you're pushing to, you'll get a message sayingnon-fast-forward updates were rejected
.This means that you must retrieve, or "fetch," the upstream changes, beforeyou are able to push your local changes.
For more information on this error, seeDealing with non-fast-forward errors.
Resolving blocked commits
To maintain the security of the repository you're pushing to, GitHub's push protection automatically protects you from accidentally committing secrets to public repositories on GitHub.com. Exposed secrets can pose serious security risks to your repository and your supply chain.If GitHub detects that the commit you're attempting to push contains a supported secret, it blocks the push. In order to resolve the block, you should either:
- Remove the secret from your commit(s). For more information, seeResolving a blocked push.
- Follow the provided URL to see options to allow the push. For more information, seeBypassing push protection
To learn more about push protection, seePush protection for users.
Pushing tags
By default, and without additional parameters,git push
sends all matching branchesthat have the same names as remote branches.
To push a single tag, you can issue the same command as pushing a branch:
git push REMOTE-NAME TAG-NAME
To push all your tags, you can type the command:
git push REMOTE-NAME --tags
Deleting a remote branch or tag
The syntax to delete a branch is a bit arcane at first glance:
git push REMOTE-NAME :BRANCH-NAME
Note that there is a space before the colon. The command resembles the same stepsyou'd take to rename a branch. However, here, you're telling Git to pushnothingintoBRANCH-NAME
onREMOTE-NAME
. Because of this,git push
deletes the branchon the remote repository.
Remotes and forks
You might already know thatyou can "fork" repositories on GitHub.
When you clone a repository you own, you provide it with a remote URL that tellsGit where to fetch and push updates. If you want to collaborate with the originalrepository, you'd add a new remote URL, typically calledupstream
, toyour local Git clone:
git remote add upstream THEIR_REMOTE_URL
Now, you can fetch updates and branches fromtheir fork:
git fetch upstream#Grab the upstream remote's branches>remote: Counting objects: 75, done.>remote: Compressing objects: 100% (53/53), done.>remote: Total 62 (delta 27), reused 44 (delta 9)>Unpacking objects: 100% (62/62), done.>From https://github.com/OCTOCAT/REPO> * [new branch] main -> upstream/main
When you're done making local changes, you can push your local branch to GitHubandinitiate a pull request.
For more information on working with forks, seeSyncing a fork.