HOWTO · Git

How to Push to Specific Branch in Git

Learn how to push a local Git branch to a chosen remote branch, set its upstream, handle rejections safely, and verify the result.

On this page

To push one local branch to a chosen branch on a remote, use git push <remote> <local-branch>:<remote-branch>. The two branch names can differ. Check which branch and remote you intend to use before sending commits; then verify the destination after the push. The command is git push.

Choose the local branch and remote destination

Start by checking your working tree and current branch with git status --short --branch. The branch shown there is the source only if you plan to push the branch you currently have checked out. You can instead name another local branch explicitly in the push command. To switch first, use git switch feature/login, replacing that name with your branch.

List configured remotes with git remote -v. A remote name such as origin is common, but repositories can use other names or have several remotes. Choose the remote that points to the repository where the branch belongs. If you are unsure of its URL, inspect the list before pushing rather than assuming origin is correct.

Push a local branch to a specific remote branch

The refspec after the remote has the form local-branch:remote-branch: the name before the colon identifies the local source, and the name after it identifies the destination on the selected remote. For example, git push origin feature/login:develop sends local feature/login to remote origin as develop. This lets you publish a local branch under a different remote name without renaming your local branch.

Run this self-contained example in a Bash shell with Git installed. It creates a temporary local repository and bare remote, pushes feature/login as develop, then removes the temporary files. It is an executable illustration of the refspec. This packet contains no execution evidence for it; when run successfully, the script prints a confirmation after Git accepts the push.

set -eu
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT

git init --bare -q "$tmpdir/remote.git"
git init -q "$tmpdir/work"
git -C "$tmpdir/work" config user.name "Example User"
git -C "$tmpdir/work" config user.email "example@example.com"
git -C "$tmpdir/work" switch -q -c feature/login
printf '%s\n' 'login change' > "$tmpdir/work/README.md"
git -C "$tmpdir/work" add README.md
git -C "$tmpdir/work" commit -q -m "Add login change"
git -C "$tmpdir/work" remote add origin "$tmpdir/remote.git"
git -C "$tmpdir/work" push -q origin feature/login:develop
printf '%s\n' 'Pushed feature/login to origin as develop.'

A successful push updates the remote destination to the source branch’s commit. If the destination branch is absent, Git can create it when the remote permits the update. Server rules, permissions, or hooks can reject an otherwise valid refspec, so do not assume every new remote branch will be accepted. The example uses a temporary local bare remote and does not contact a hosting service.

Set the upstream branch for future pushes

An upstream (also called a tracking branch) records which remote branch a local branch follows for default push and pull behavior. To push the current branch and set its upstream in one operation, use git push --set-upstream origin feature/login (short form: git push -u origin feature/login). This form uses the same branch name at both ends. If the desired remote destination has another name, provide it explicitly as a refspec, for example git push --set-upstream origin feature/login:develop.

After an upstream is configured, a plain git push follows the default push behavior configured in Git. With the default push.default=simple setting, Git may refuse a plain push when the upstream branch has a different name from the local branch, as in feature/login tracking origin/develop. In that case, push explicitly with git push origin feature/login:develop. If you want to use a plain git push, configure a push mode that allows differently named upstream branches and understand its behavior first. Confirm the relationship with git branch -vv; the explicit remote and refspec make the intended destination clear.

Handle push rejections safely

A rejected push is a reason to read the message and inspect the repository state, not to retry with force by default. A non-fast-forward rejection commonly means the remote branch has commits that are not in your local branch. Fetch the remote updates with git fetch origin, then inspect the commits with git log --oneline --graph --decorate --all. Decide with your project workflow whether to merge or rebase the relevant remote-tracking branch, such as origin/develop, into your local branch. Resolve any conflicts, complete the merge or rebase, and then retry the intended push.

Other rejection messages can point to missing permission, a protected branch, a server-side hook, or a destination policy. Address the stated cause or ask the repository maintainer. Avoid git push --force unless you understand whose commits would be replaced and have authorization; where a history rewrite is intended, --force-with-lease adds a check against overwriting a remote update you have not seen, but it still rewrites shared history.

Push all local branches to a remote

git push --all origin pushes all local branches to the named remote. This is broader than pushing one source branch with a refspec, and it can publish work that was not part of your immediate task. Use it only when you intend to send every local branch. It does not mean “push every tag”; tags require a separate command such as git push --tags origin when that is specifically wanted.

Verify the result

After pushing, inspect the local tracking information with git branch -vv. To check what the remote advertises, use git ls-remote --heads origin develop, substituting your remote and destination branch. You can also fetch and compare the remote-tracking reference with git log --oneline --decorate feature/login origin/develop when the local repository has an up-to-date origin/develop reference.

Check that the remote name and destination branch match the intended refspec. A successful command only confirms the server accepted that update; it does not confirm a pull request, deployment, or any hosting-service workflow has completed.