Branches on GitHub: work without breaking things

Branches let you experiment, develop features, and fix bugs independently from the main codebase. Learn how to create, switch, merge, and delete branches – both on GitHub and from your terminal.

What is a branch?

main
feature-login
bugfix-nav

A branch is like a separate timeline. You can work on a new feature without affecting the main branch. Later, you merge it back.

Step 1: See your branches

First, see which branches exist locally and which one you're on (* indicates current branch).

git branch
$ git branch
  feature-1
* main
  test-branch

To see remote branches (on GitHub): git branch -r or git branch -a (all).

Step 2: Create a new branch

You can create a branch directly on GitHub or via command line. Both are easy.

On GitHub website:

  • Go to your repository.
  • Click the branch selector dropdown (usually says "main").
  • Type a new branch name (e.g., "feature-login") and press "Create branch".

From terminal:

git branch branch-name   or   git checkout -b branch-name (create + switch)
$ git checkout -b feature-login
Switched to a new branch 'feature-login'
Note: Creating a branch locally doesn't send it to GitHub yet. Use git push -u origin feature-login to publish it.
Step 3: Switch branches

Move between branches to work on different features.

git checkout branch-name   or   git switch branch-name (newer syntax)
$ git switch main
Switched to branch 'main'
Make sure you have committed or stashed changes before switching, otherwise you might lose work. Use git status to check.
Step 4: Push branch to GitHub

After making commits on your new branch, push it so it appears on GitHub (and others can see).

git push -u origin branch-name (first time)
then git push (later)
$ git push -u origin feature-login
* [new branch] feature-login -> feature-login
Branch 'feature-login' set up to track remote branch.

Now visit your GitHub repo and click the branch dropdown – you'll see your branch there.

Step 5: Merge branches – Pull Request

When your feature is ready, you want to bring changes into main. On GitHub, this is done via a Pull Request (PR).

How to create a PR:

  1. Go to your repository on GitHub.
  2. Click "Pull requests" tab → "New pull request".
  3. Select base: main and compare: your-branch.
  4. Review changes, add description, and click "Create pull request".
  5. After review, click "Merge pull request" → "Confirm merge".

Merge via command line (alternative):

git checkout main
git merge branch-name
$ git checkout main
$ git merge feature-login
Updating a1b2c3d..e4f5g6h
Fast-forward
login.html | 10 ++++++++++
1 file changed, 10 insertions(+)

Then push the updated main: git push origin main.

Step 6: Delete a branch (clean up)

After merging, you can delete the branch – it keeps the repository tidy.

On GitHub:

After merging a PR, GitHub shows a "Delete branch" button. Click it to remove the remote branch.

Locally:

Delete local branch:

git branch -d branch-name
$ git branch -d feature-login
Deleted branch feature-login (was e4f5g6h).

If the branch wasn't merged, use -D to force delete (careful!).

Branch commands cheat sheet

CommandWhat it does
git branchList local branches (* current)
git branch new-branchCreate branch (but stay on current)
git checkout -b new-branchCreate and switch to new branch
git switch new-branchSwitch to an existing branch
git push -u origin branchPush branch to GitHub (first time)
git merge branchMerge branch into current branch
git branch -d branchDelete local branch (safe)
git push origin --delete branchDelete remote branch
Typical branch workflow:
git checkout -b feature-xyz → work + commits → git push -u origin feature-xyz → open PR on GitHub → merge → delete branch.

Branching FAQ – common questions

What is the difference between main and master?
They are just names for the default branch. GitHub now uses main for new repositories. Older repos may use master. Functionally identical.
Can I create a branch directly on GitHub without Git?
Yes! Go to your repo, click the branch dropdown, type a name, and select "Create branch". It exists only on GitHub until you git pull it locally.
How do I rename a branch?
If you're on the branch: git branch -m new-name. From another branch: git branch -m old-name new-name. Then update remote: git push origin -u new-name and delete old remote branch.
What does "fast-forward" merge mean?
It means the branch you're merging into hasn't moved forward since you branched. Git simply moves the pointer forward. No extra merge commit is created.
I get "merge conflict" – what now?
Conflict means the same part of a file was changed on both branches. Git shows markers <<<<<<<. Open the file, decide which changes to keep, remove markers, then git add and git commit. It's normal!
Can I have multiple branches at the same time?
Absolutely. You can have dozens of branches. Only one branch is "checked out" (active) at a time. They're just pointers.
How do I see all branches (local + remote)?
git branch -a shows local and remote branches. Remote branches appear like remotes/origin/feature.
Should I delete a branch after merging?
Yes, it's good practice. It keeps the list clean. GitHub offers a "Delete branch" button after merging a PR. You can always recreate if needed.
What's a "protected branch"?
On GitHub, you can protect branches like main so that no one can push directly. Changes must go through pull requests and pass checks. Great for team projects.
Can I compare branches on GitHub?
Yes! Go to your repo, press . to open web editor, or use the URL: https://github.com/user/repo/compare/branch1...branch2. Shows differences.
I accidentally committed to main. How do I move that commit to a new branch?
Create a new branch from main (git branch feature), then reset main back: git reset --hard HEAD~1 (remove last commit). Switch to feature and push. Careful with --hard – it discards uncommitted changes.
What's the difference between git switch and git checkout?
git switch is a newer, simpler command for changing branches. git checkout is older and can also restore files. Both work. Use switch for branches to avoid confusion.
Previous: Git basics Next: Forks & PRs

Branches give you superpowers – experiment freely, then merge with confidence.