Why Use Branches with Git and GitHub?
Branches let you experiment without risk. They give you a safe, isolated place to make and review changes before they become permanent.
Prerequisites
- Git installed (git-scm.com/install)
- GitHub CLI installed (cli.github.com)
- GitHub account created (github.com)
- GitHub CLI connected to your account. Run this once in your terminal:
gh auth login
Set Up Your Local Repository For Git and GitHub
Create a Repository on GitHub
Create a new repository at github.com/new
Initialize Your Local Repository with Git
In the Terminal, navigate to the root directory of your code project. Then enter the following Terminal Command.
git init
Connect Your Local Repository to Your GitHub Repository
Find your repository URL by going to your repo in GitHub.com and clicking on the green button that says "Code."
Enter the following Terminal Command to link your GitHub repository, but replacing with your repo URL.
git remote add origin https://github.com/yourname/yourrepo.git
Make Your First Commit and Push it to GitHub
Then, enter the following Terminal Command to push your current local state to GitHub.
git add . git commit -m "Initial commit" git branch -M main git push -u origin main
OR
Prompt in Claude Code
Initialize this project with git. Add the following url as my remote "https://github.com/yourname/yourrepo.git" Make my first commit and push it to GitHub
The Day-to-Day Workflow
Make all code changes to a branch not directly on main.
First open up your Terminal or Claude Code in the root folder for your coding project.
Update Local Main
Start on the main branch and make sure it's updated with all of the latest changes.
Prompt in Claude Code
Update my local main branch with the latest from the origin
OR
Run a Terminal Command
git checkout main git pull
If git pull errors, you likely have uncommitted changes — commit or stash them first.
Create a New Branch
Then you'll create a new branch, ideally you'll name it based on the unit of work you are going to complete (e.g. feat/setup-payments).
One convention is to prefix your branch names with feat/, fix/, or chore/ depending on what type of thing you are working on. Another convention is to prefix branches with your GitHub username.
Branch names should have no spaces.
Prompt in Claude Code
Create a new branch called "your-branch-name-here"
OR
Run a Terminal Command
git checkout -b your-branch-name-here
Edit Your Code on the Branch
Prompt and edit your way to a new feature or functionality. When you get each piece of work done, commit and push the changes.
Commit - creates a save point with the code changes tracked. A commit is local only until you push.
Push - sends that save point up to GitHub
Prompt in Claude Code
Commit and Push the changes
OR
Run a Terminal Command
For the first commit on a new branch. This creates the branch on GitHub.
git add . git commit -m "Enter a description of your change here." git push -u origin your-branch-name-here
For the subsequent commits to a branch. It remembers what branch on GitHub matches the local branch.
git add . git commit -m "Enter a description of your new change here." git push
Create a Pull Request
Once you have made all of the changes and tested that things are working, you can create a pull request to merge the branch back into main.
The pull request becomes a reviewable artifact of all the changes related to a piece of work.
Prompt in Claude Code
Create a pull request
OR
Run a Terminal Command
This version of creating a PR simply adds the accumulated commit messages as the Title and Description of the PR.
gh pr create --fill
Review Your Pull Request
After the pull request is created, Claude Code has a built-in slash command that will review the pull request. When the change is more than a simple fix, use it.
Prompt in Claude Code
/review
It will then give you a summary of fixes and the priority for each. You can then prompt to fix certain things. When it's done, you commit and push the changes and they automatically get added to the active pull request.
Keep reviewing and iterating until the /review command comes back saying things look good to merge.
Merge The Pull Request
Once all the checks pass, the pull request will show up as "ready to merge" on GitHub (go to the pull request URL shown in Claude Code to view it there). Press merge and all of your changes will be merged into the main branch.
Update Main and Clean Up Branches
After everything is merged, you can switch back to the main branch on your local computer and pull down the changes you just merged into it.
Prompt in Claude Code
Switch back to the main branch and pull the latest changes.
OR
Run a Terminal Command
git checkout main git pull
If you don't need the former branch anymore, you can then delete it both from your local branch and on GitHub.
Prompt in Claude Code
Can you delete the branch I was just working on locally and on the remote?
OR
Run a Terminal Command
git branch -d your-branch-name-here git push origin --delete your-branch-name-here