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:
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-tipsSwitched 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 truemakes a plaingit pushset 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:
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:
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:
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 -aTo /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?
- git pull
- git push -u origin <branch>
- git fetch --all
- git remote add origin
Which command deletes the branch old on the remote origin?
- git branch -d old
- git push origin --delete old
- git remote remove old
- git fetch --prune old