git push <remote> <branchname>

Upload local commits for a specific branch to a chosen remote repository.

This command is useful to share your local work with others or back it up remotely by sending the commits from a given local branch to a specific remote repository and branch.

This command tells Git to send the commits from the local branch named <branchname> to the remote repository identified by <remote>, updating the corresponding branch on that remote if it already exists or creating it if it does not; here <remote> is usually something like origin (the default remote added when you clone), and <branchname> is a local branch such as main or feature/login-form. The first argument, <remote>, specifies which remote repository to push to, and the second argument, <branchname>, specifies exactly which local branch to push instead of pushing all branches. Under the hood, Git compares your local branch history with the remote branch, then sends only the new commits and updates the remote reference, which is why this command will fail if the remote has new commits you don't have yet, prompting you to pull or fetch and merge/rebase first.

You can add flags to change how this command behaves, for example --set-upstream (or -u) turns it into git push -u <remote> <branchname>, which also links your local branch with the remote one so later you can just run git push or git pull with no extra arguments; --force (or -f) creates git push -f <remote> <branchname>, which overwrites the remote branch history with your local branch (useful only when you explicitly want to discard remote commits), and --force-with-lease does a safer version of the same thing by refusing to overwrite if someone else has pushed in the meantime. Closely related variants include git push <remote> (push all matching branches that already have an upstream set), git push on its own (push the current branch to its configured upstream), and git push --tags to upload all local tags; complementary commands are git fetch <remote> to download updates without merging them, and git pull <remote> <branchname> to fetch and immediately merge new commits from a remote branch into your current branch.

Examples:

  • git push origin main
  • git push origin feature/login-form
  • git push -u origin my-new-branch
Manual page
git push
Related commands

Welcome to GitExamples!

Sign in to gitexamples