5. Collaborating Via Branches

Branches are not just local pointers; they can also be published to GitHub. Publishing feature branches to the cloud is how developers collaborate, share code reviews, and trigger automated tests. In this chapter, we will learn how branches behave on GitHub.

1. Pushing Local Branches to GitHub

When you create a new local branch (e.g., feature/login) and want to make it visible on GitHub so others can see it:

# Create and switch to local feature branch
git switch -c feature/login

# Push branch and set upstream tracking on GitHub
git push -u origin feature/login

Once pushed, this branch appears in the **Branch dropdown list** on your GitHub repository page. Other developers can now clone or download your branch to test it locally.

2. Tracking Remote Branches

If a teammate has created a branch named feature/dark-mode and pushed it to GitHub, and you want to work on it locally:

# Fetch latest branches from server
git fetch origin

# Switch to the branch (Git will automatically detect origin/feature/dark-mode and set up a tracking local branch)
git switch feature/dark-mode

3. Default Branch Settings (main vs. master)

Every repository has a **Default Branch** (historically master, now commonly main). This is the branch that is shown immediately when someone visits your repository URL, and it is the base target for all pull requests.

To change your default branch on GitHub:

  1. Navigate to your repository page and click the Settings tab.
  2. Click Branches in the left-hand sidebar.
  3. Click the **swap icon** next to the default branch name to switch it to another active branch.
  4. Click **Update** and confirm.

4. Managing and Deleting Remote Branches

To see a list of all local and remote branches in your terminal:

git branch -a

Remote branches appear prefixed with remotes/origin/.

Deleting a Remote Branch

Once a feature has been merged into main, you should delete the remote branch to keep your repository tidy. You can click the trash icon next to the branch on the GitHub website, or run this command in your terminal:

git push origin --delete feature/login
Pro Tip: When you delete a remote branch on GitHub, your local Git still remembers it. To prune outdated remote branch pointers from your local machine, run:
git fetch origin --prune