Git & GitHub ยท Chapter 26 of 42

git push

`git push` uploads your local commits to a remote repository, such as GitHub, making them available to others. You typically push to a specific branch on a specific remote.

The first time you push a new local branch, you use `-u` (or `--set-upstream`) to link it to a remote branch, so future pushes can just use `git push` without extra arguments.

Syntax
git push origin <branch>
git push -u origin <branch>

Pushing commits

Run `git push origin main` to upload commits on your local main branch to the origin remote's main branch.

Setting the upstream branch

Use `git push -u origin branch-name` the first time you push a new branch, which links it so future pushes and pulls just work with plain `git push`/`git pull`.

Example 1 (bash)
git push -u origin feature-login
Output
Branch 'feature-login' set up to track remote branch 'feature-login' from 'origin'.

Pushes a new branch to GitHub and links it for future push/pull commands.

Example 2 (bash)
git push
Output
To github.com:user/repo.git
   a3f5c9e..9c2e1aa  main -> main

Pushes new commits on the tracked branch without needing to specify remote or branch name again.

Key points

  • git push uploads local commits to a remote repository.
  • -u sets up tracking so future pushes need no extra arguments.
  • You push to a specific remote and branch, like origin main.
  • Push can be rejected if the remote has commits you don't have locally.
๐Ÿ’ก Note: If push is rejected, run git pull first to integrate remote changes, then push again.

๐Ÿ“ Quick Quiz

1. What does git push do?

2. What does the -u flag do on first push?

3. Why might git push be rejected?