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?
A branch is like a separate timeline. You can work on a new feature without affecting the main branch. Later, you merge it back.
First, see which branches exist locally and which one you're on (* indicates current branch).
git branchfeature-1
* main
test-branch
To see remote branches (on GitHub): git branch -r or git branch -a (all).
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)Switched to a new branch 'feature-login'
git push -u origin feature-login to publish it.Move between branches to work on different features.
git checkout branch-name or git switch branch-name (newer syntax)Switched to branch 'main'
git status to check.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)* [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.
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:
- Go to your repository on GitHub.
- Click "Pull requests" tab → "New pull request".
- Select
base: mainandcompare: your-branch. - Review changes, add description, and click "Create pull request".
- After review, click "Merge pull request" → "Confirm merge".
Merge via command line (alternative):
git checkout main git merge branch-name$ 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.
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-nameDeleted branch feature-login (was e4f5g6h).
If the branch wasn't merged, use -D to force delete (careful!).
Branch commands cheat sheet
| Command | What it does |
|---|---|
git branch | List local branches (* current) |
git branch new-branch | Create branch (but stay on current) |
git checkout -b new-branch | Create and switch to new branch |
git switch new-branch | Switch to an existing branch |
git push -u origin branch | Push branch to GitHub (first time) |
git merge branch | Merge branch into current branch |
git branch -d branch | Delete local branch (safe) |
git push origin --delete branch | Delete remote branch |
git checkout -b feature-xyz → work + commits → git push -u origin feature-xyz → open PR on GitHub → merge → delete branch.
Branching FAQ – common questions
main for new repositories. Older repos may use master. Functionally identical.
git pull it locally.
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.
<<<<<<<. Open the file, decide which changes to keep, remove markers, then git add and git commit. It's normal!
git branch -a shows local and remote branches. Remote branches appear like remotes/origin/feature.
main so that no one can push directly. Changes must go through pull requests and pass checks. Great for team projects.
. to open web editor, or use the URL: https://github.com/user/repo/compare/branch1...branch2. Shows differences.
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.
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.
Branches give you superpowers – experiment freely, then merge with confidence.