Sub topic 8
Pushing your first branch
This chapter assumes you have a local repository with at least one commit, an empty GitHub repository, a remote named origin added with git remote add, and working authentication (SSH key, token, or GitHub CLI login). Here you send your first branch to GitHub.
What a push does
A push copies commits that the remote lacks from a local branch to a branch on the remote. Git does not upload your working files. It uploads commit objects, trees, and file contents, then asks the remote to move a branch name to your newest commit.
Step 1: Check the branch name
GitHub shows main as the default branch in its setup instructions. Your local branch may still be called master, depending on your Git version and configuration. Check it first.
git branch --show-current
git statusIf the name is not main, rename it.
git branch -M mainThe -M flag renames the branch even if a branch called main already exists. On a fresh repository that is safe. The name does not have to be main, but matching GitHub's default avoids extra configuration later.
Step 2: Push and set the upstream
git push -u origin mainThe command has three parts:
originis the remote name you chose in the previous chapter.mainis the local branch to send. Git creates a branch with the same name on the remote.-u(short for--set-upstream) links your localmaintoorigin/main.
A successful push prints the number of objects sent and a line such as * [new branch] main -> main. If you use HTTPS, Git may ask for your username and a personal access token here, not your account password. With SSH, the first connection asks you to confirm GitHub's host fingerprint.
The upstream link means later you can run git push and git pull with no arguments. The next chapter covers what -u stores.
Step 3: Verify the result
Check three places:
git status
git branch -vv
git log --oneline --decorate -n 3git status should say your branch is up to date with origin/main. git branch -vv shows [origin/main] beside main. git log --decorate shows origin/main on the same commit as main. Then refresh the repository page on GitHub. Your files and commit history should appear.
origin/main is a remote-tracking branch. It is a local bookmark of where main was on GitHub the last time you communicated with it. It does not update by itself.
Pushing other branches
Each new branch needs its own first push.
git switch -c feature/login
git commit --allow-empty -m "Start login work"
git push -u origin feature/loginPushing main does not push other branches. A plain git push with no upstream on a new branch fails, and Git prints the exact --set-upstream command to use.
If the push fails
The common failures, covered fully in a later chapter:
| Message | Likely cause |
|---|---|
| `src refspec main does not match any` | The local branch is not named `main`, or there are no commits yet. |
| `rejected ... fetch first` | GitHub already has commits you lack, such as an auto-generated README. |
| `Permission denied (publickey)` | The SSH key is missing or not added to your GitHub account. |
| `Authentication failed` | The HTTPS token is wrong or expired. |
Exercise
You run git push origin main and it succeeds. A teammate then asks why a plain git push on your machine still prints "The current branch main has no upstream branch." What did you leave out, and what single command fixes it without pushing new commits?
Show answer
You omitted -u (--set-upstream), so Git never linked local main to origin/main. Run git branch --set-upstream-to=origin/main main, or run git push -u origin main again, which pushes nothing new but records the link.
Exercise
After a successful first push, what does origin/main represent, and when does it change?
Show answer
It is a remote-tracking branch, a local record of where main was on GitHub at your last communication with it. It changes when you push, fetch, or pull, not when someone else pushes to GitHub.