What a branch is and why you need one

A branch in GitHub is a separate copy of your project where you can make changes without affecting the main version. Think of it like working on a draft of a document while the original stays untouched. When your changes are ready, you merge the branch back into the main project — usually called "main" or "master" — and GitHub combines your work with everyone else's.

Branches let multiple people work on the same project at the same time without stepping on each other's toes. One person can fix a bug on their branch while another adds a new feature on theirs. Each branch is isolated until you decide to merge it.

You create a branch when you're about to start work on something — a bug fix, a new feature, or an experiment. You delete it after the work is merged and no longer needed. Most teams require branches for any change, even if you're the only person working on the project.

Key Takeaways

  • Create a branch from the main branch by clicking the branch dropdown on GitHub's code page and typing a new branch name.
  • Branch names should be short and describe what you're working on, like "fix-login-bug" or "add-dark-mode".
  • Make your changes on the branch, commit them with a message explaining what you changed, and push them to GitHub.
  • When your work is done, create a pull request to ask for your changes to be reviewed and merged into main.
  • Delete the branch after it's merged so your repository stays clean and organized.

Creating a branch through GitHub's web interface

The fastest way to create a branch is directly on GitHub's website. Go to your repository, click the branch dropdown button near the top left (it shows the current branch name, usually "main"), and type the name of your new branch in the text field that appears. GitHub automatically creates the new branch based on whatever branch you were viewing.

Choose a branch name that describes the work you're doing. Use lowercase letters, hyphens instead of spaces, and keep it short: "fix-typo-readme", "update-dependencies", "add-user-profile-page". Avoid names like "test" or "new-branch" because they don't tell anyone what the branch is for.

After you type the name and press Enter, GitHub creates the branch and switches you to it. You'll see the branch name in the dropdown now shows your new branch instead of main. Any changes you make and commit from this point go to your new branch, not to main.

Creating a branch from the command line

If you work on your computer using Git commands, you create a branch locally and then push it to GitHub. Open your terminal or command prompt in your project folder and run git checkout -b branch-name, replacing "branch-name" with your actual branch name. This command creates the branch and switches you to it in one step.

After you've made changes and committed them with git commit -m "your message", push the branch to GitHub with git push origin branch-name. The first time you push a new branch, Git may ask you to set the upstream branch — follow the instruction it gives you, or run git push -u origin branch-name to set it up automatically.

You can see all your branches locally by running git branch. To see branches on GitHub as well, run git branch -a. Switch between branches with git checkout branch-name (or git switch branch-name in newer Git versions).

Making changes and committing on your branch

Once you're on your branch, make the changes you need to make. Edit files, add new files, delete old ones — whatever your work requires. Your changes only affect this branch, so main stays exactly as it was.

After you've made changes, you need to commit them. A commit is a snapshot of your work with a message explaining what you changed. On the command line, run git add . to stage all your changes, then git commit -m "describe what you changed". Write a clear message: "Fix login button not responding on mobile" is better than "fixed stuff".

If you're using GitHub's web interface, you can edit files directly and commit each change as you go. Click the pencil icon on any file, make your edits, scroll down, write a commit message, and click "Commit changes". Each edit becomes its own commit, which is fine for small changes but can get messy for larger work.

Pushing your branch to GitHub

If you created your branch on your computer, you need to push it to GitHub so others can see it and so you have a backup. Run git push origin branch-name to send your commits to GitHub. After the first push, you can use git push by itself if you set up the upstream branch.

You can push as many times as you want while you're working. Each push updates the branch on GitHub with your latest commits. This is how you back up your work and let teammates see what you're doing.

If you created the branch on GitHub's website and are working on your computer, you need to pull it down first. Run git fetch origin to read the branch list, then git checkout branch-name to switch to it. Git automatically knows where to find it on GitHub.

Creating a pull request to merge your branch

When your work is done and committed, you create a pull request (often called a PR) to ask for your changes to be merged into main. On GitHub, go to your repository and you'll usually see a banner suggesting you create a pull request for your recently pushed branch. Click that, or go to the "Pull requests" tab and click "New pull request".

Select your branch as the source and main as the destination. Write a title and description explaining what you changed and why. If your change fixes a bug or closes an issue, mention it in the description — GitHub can automatically close issues when the PR is merged. Click "Create pull request".

Your teammates (or you, if you're working alone) can now review the changes, leave comments, and request modifications. You can keep pushing commits to the same branch while the PR is open — they automatically appear in the PR. Once everyone approves, click "Merge pull request" to combine your branch into main.

Deleting a branch after merging

After your branch is merged, delete it to keep your repository clean. GitHub usually offers a "Delete branch" button right after you merge a PR — click it. If you don't see it, go to the "Branches" tab in your repository, find your branch in the list, and click the trash icon next to it.

If you created the branch on your computer, delete it locally with git branch -d branch-name. If you want to delete it even if it hasn't been merged, use git branch -D branch-name (capital D). Then run git push origin --delete branch-name to remove it from GitHub.

Deleting a branch doesn't delete your work — the commits are still in the main branch after the merge. Deleting just removes the branch pointer, which keeps your branch list from getting cluttered with old work.

Frequently Asked Questions

What happens if I delete a branch by accident?

If you deleted it locally but haven't pushed the deletion to GitHub, you can recover it with git reflog and git checkout -b branch-name [commit-hash]. If you already pushed the deletion, the branch is gone from GitHub but the commits still exist — you can create a new branch from the same commit if you know the commit hash. GitHub keeps deleted branches in the reflog for a few weeks.

Can I create a branch from a branch that isn't main?

Yes. When you create a new branch, it's based on whatever branch you're currently viewing. If you're on a branch called "feature-x" and create a new branch from it, the new branch will have all of feature-x's commits. This is useful when you're building on someone else's work, but usually you want to branch from main to avoid stacking incomplete work.

What if two people push changes to the same branch?

Git combines the changes automatically if they're in different files or different parts of the same file. If you both edited the same lines, Git creates a merge conflict and asks you to decide which version to keep. You fix the conflict, commit the resolution, and push again. This is why branches are useful — each person works on their own branch to avoid conflicts.

Do I have to use branches, or can I just push to main?

Technically you can push directly to main, but it's a bad habit. Branches let you test your work, get feedback through a pull request, and catch problems before they reach the main version. Most teams require branches for everything, even one-person projects, because it keeps your history clean and makes it easier to undo mistakes.

How do I see what changed between my branch and main?

On GitHub, go to your pull request and click the "Files changed" tab to see every file you modified and exactly what lines changed. On the command line, run git diff main branch-name to see all the differences. This is useful for reviewing your own work before you ask others to merge it.