TExcelTAD
Home › Git › Git Push Branch to GitHub

Git Push Branch to GitHub

A branch you create locally exists only on your computer. To back it up, share it, or open a pull request, push it to the remote.

Pushing a new branch

The first push of a new branch needs the remote and the branch name; -u sets up tracking for later pushes and pulls:

bash · try it yourself
git init -q --bare ~/github/blog.git
mkdir blog && cd blog && git init -q
echo "# My blog" > README.md && git add . && git commit -q -m "Start blog"
git remote add origin ~/github/blog.git && git push -q -u origin main

git switch -c post/git-tips
echo "Commit small, commit often." > git-tips.md
git add . && git commit -q -m "Draft Git tips post"
git push
echo "exit status: $?"
git push -u origin post/git-tips
Switched to a new branch 'post/git-tips'
fatal: The current branch post/git-tips has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin post/git-tips

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

exit status: 128
To /home/learner/github/blog.git
 * [new branch]      post/git-tips -> post/git-tips
branch 'post/git-tips' set up to track 'origin/post/git-tips'.

The first git push failed because the new branch has no upstream yet, and Git tells you the exact command. GitHub's server adds a line suggesting to open a pull request ("Create a pull request for 'post/git-tips' on GitHub by visiting…"); a plain bare repository doesn't.

Tip: git config --global push.autoSetupRemote true makes a plain git push set up the upstream automatically for new branches.

Pushing more commits

After the first push, the branch tracks origin/post/git-tips, so git push and git status work without arguments:

bash · try it yourself
git init -q --bare ~/github/blog.git
mkdir blog && cd blog && git init -q
echo "# My blog" > README.md && git add . && git commit -q -m "Start blog"
git remote add origin ~/github/blog.git && git push -q -u origin main
git switch -q -c post/git-tips
echo "Commit small, commit often." > git-tips.md && git add . && git commit -q -m "Draft Git tips post"
git push -q -u origin post/git-tips

echo "Write messages in the imperative." >> git-tips.md
git commit -q -am "Add tip about messages"
git status -sb | head -1
git push -q
git status -sb | head -1
git ls-remote --heads origin
## post/git-tips...origin/post/git-tips [ahead 1]
## post/git-tips...origin/post/git-tips
959777dc075acbf4e965a031b3a2d4f1cb0b35c0	refs/heads/main
4d5fe513b2ceff5943f9dfba56908f42af964341	refs/heads/post/git-tips

Pushing under a different name

git push origin local:remote pushes a local branch to a branch with another name on the server:

bash · try it yourself
git init -q --bare ~/github/blog.git
mkdir blog && cd blog && git init -q
echo "# My blog" > README.md && git add . && git commit -q -m "Start blog"
git remote add origin ~/github/blog.git && git push -q -u origin main
git switch -q -c wip
echo "draft" > draft.md && git add . && git commit -q -m "Draft"
git push -q origin wip:ada/experimental-layout
git branch -r
  origin/ada/experimental-layout
  origin/main

Deleting a remote branch

After a branch is merged, delete it on the server (GitHub has a Delete branch button on merged pull requests) and locally:

bash · try it yourself
git init -q --bare ~/github/blog.git
mkdir blog && cd blog && git init -q
echo "# My blog" > README.md && git add . && git commit -q -m "Start blog"
git remote add origin ~/github/blog.git && git push -q -u origin main
git switch -q -c post/git-tips
echo "tips" > git-tips.md && git add . && git commit -q -m "Add Git tips post"
git push -q -u origin post/git-tips
git switch -q main && git merge -q post/git-tips && git push -q

git push origin --delete post/git-tips
git branch -d post/git-tips
git branch -a
To /home/learner/github/blog.git
 - [deleted]         post/git-tips
Deleted branch post/git-tips (was 06059ad).
* main
  remotes/origin/main

Test yourself

You run git push on a brand-new local branch and get "has no upstream branch". What fixes it?

  1. git pull
  2. git push -u origin <branch>
  3. git fetch --all
  4. git remote add origin

Which command deletes the branch old on the remote origin?

  1. git branch -d old
  2. git push origin --delete old
  3. git remote remove old
  4. git fetch --prune old