Understand Git branches on GitHub with our step-by-step guide on creating, switching, merging, and deleting branches for efficient development.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1. Understanding the concept of a branch
A branch in Git is a lightweight movable pointer to one of these commits. Think of branches as separate lines of development:
Step 2. Viewing branches in a cloned repository
First, clone a repository (if you haven’t already) and navigate into it. Then list all local branches:
git clone https://github.com/USERNAME/REPO.git
cd REPO
git branch
You’ll see an asterisk (\*
) next to the current branch, usually main
or master
.
Step 3. Inspecting remote branches
To see branches that exist on the remote (GitHub) as well as local ones:
git fetch
git branch -a
remotes/origin/main
is the default remote branch.remotes/origin/feature-xyz
branches represent work in progress on GitHub.
Step 4. Creating a new branch
Create a branch to start working on a new feature without affecting main
:
git checkout -b feature/login-form
feature/login-form
in one command.
Step 5. Pushing your branch to GitHub
After committing your changes locally, push the branch to the remote so others can see it:
git push -u origin feature/login-form
-u origin feature/login-form
sets up tracking so future pushes can use git push
directly.
Step 6. Switching between branches
To switch from one branch to another:
git checkout main
git pull
git checkout feature/login-form
main
before switching back.git checkout
updates your working files to match the branch you switch to.
Step 7. Merging a branch back into main
Once your feature is complete and tested, merge it into main
:
git checkout main
git pull
git merge feature/login-form
Step 8. Deleting a branch
Clean up branches you no longer need:
git branch -d feature/login-form
git push origin --delete feature/login-form
-d
removes a local branch if it’s fully merged.--delete
removes the remote branch from GitHub.
Step 9. Visualizing branches on GitHub
On the GitHub website:
Step 10. Best practices for branches
feature/
, bugfix/
).When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.