Git Tutorial – Part 5: Working with Git Branches
Objective
In this tutorial, you will learn how to:
- Understand Git branches.
- Create, switch, rename, and delete branches.
- Merge branches into the
mainbranch. - View branch history.
- Follow common branching best practices.
What is a Branch?
A branch is an independent line of development.
Instead of making changes directly to the main branch, developers create a new branch to work on a feature, bug fix, or enhancement.
This allows multiple developers to work simultaneously without affecting the stable code.
Why Use Branches?
Branches help you:
- Develop new features safely.
- Fix bugs without impacting production code.
- Experiment with new ideas.
- Work independently from other developers.
- Collaborate efficiently within a team.
Understanding Branches
Consider the following example:
main
│
├───────────────●──────────────●
│
│
feature/login
│
●──────●──────●
- The
mainbranch contains the stable version of the application. - The
feature/loginbranch is used to develop a new login feature. - After testing, the feature branch is merged back into
main.
Step 1: View the Current Branch
Open the terminal and navigate to your project.
Run:
git branch
Example output:
* main
The asterisk (*) indicates the branch you are currently using.
Step 2: Create a New Branch
Create a branch named feature/login.
git branch feature/login
The branch has been created, but you are still on the main branch.
Verify:
git branch
Example:
feature/login
* main
Step 3: Switch to the New Branch
Run:
git checkout feature/login
Example output:
Switched to branch 'feature/login'
Verify:
git branch
Example:
* feature/login
main
Alternative Method
You can create and switch to a new branch in a single command:
git checkout -b feature/profile
This command:
- Creates the branch.
- Switches to the branch immediately.
Using the Modern Command
Recent versions of Git provide a dedicated command for switching branches.
git switch feature/login
Create and switch:
git switch -c feature/settings
Although git checkout is still widely used, git switch is often easier to understand.
Step 4: Make Changes in the Branch
You should do this in the feature branch, not in the main branch. In this example, make sure you have switched to feature/login before creating or editing login.html. This keeps your work isolated until it is ready to be merged back into main.
Create a file named:
login.html
Check the repository status:
git status
Stage the file:
git add login.html
Commit the changes:
git commit -m "Add login page"
The commit is stored only in the feature/login branch.
Step 5: View the Branch History
Display the commit history.
git log --oneline
You will see the commits made on the current branch.
Step 6: Switch Back to the Main Branch
Run:
git switch main
or
git checkout main
Notice that login.html is no longer visible because it exists only in the feature branch.
Step 7: Merge the Feature Branch
Merge the completed feature into the main branch.
git merge feature/login
Example output:
Updating abc1234..def5678
Fast-forward
The changes from feature/login are now part of the main branch.
Step 8: Verify the Merge
Run:
git log --oneline
The commit created in the feature branch is now part of the main branch history.
Step 9: Delete the Feature Branch
After a successful merge, delete the feature branch.
git branch -d feature/login
Example output:
Deleted branch feature/login
List the remaining branches:
git branch
Force Delete a Branch
If a branch has not been merged but you still want to remove it:
git branch -D feature/login
Use this command carefully, as unmerged commits may be lost.
Rename a Branch
Rename the current branch:
git branch -m feature/authentication
Rename another branch:
git branch -m old-name new-name
View All Branches
Local branches:
git branch
Remote branches:
git branch -r
Both local and remote branches:
git branch -a
Branch Naming Best Practices
Use descriptive names.
Examples:
feature/login
feature/user-profile
bugfix/login-error
bugfix/null-pointer
hotfix/payment
release/v1.2
Avoid names such as:
branch1
test
newbranch
abc
Typical Development Workflow
main
│
│
├── Create feature branch
│
▼
feature/new-feature
│
│ Develop
│ Commit
│ Test
▼
Merge into main
│
▼
Delete feature branch
Frequently Used Branch Commands
| Command | Description |
|---|---|
git branch | List local branches. |
git branch <name> | Create a new branch. |
git switch <name> | Switch to a branch. |
git switch -c <name> | Create and switch to a new branch. |
git checkout <name> | Switch branches (legacy command). |
git merge <branch> | Merge a branch into the current branch. |
git branch -d <branch> | Delete a merged branch. |
git branch -D <branch> | Force delete a branch. |
git branch -a | List all local and remote branches. |
Best Practices
- Never develop directly on the
mainbranch unless your team’s workflow allows it. - Create a separate branch for each feature or bug fix.
- Keep branches focused on a single task.
- Merge completed work only after testing.
- Delete feature branches after they have been merged.
- Pull the latest changes from
mainbefore creating a new feature branch.
Summary
In this chapter, you learned how to:
- Create and manage Git branches.
- Switch between branches.
- Develop features in isolation.
- Merge completed work into the
mainbranch. - Delete and rename branches.
- Follow common branching best practices.
Using branches allows teams to develop features independently while keeping the main branch stable.
Next Tutorial
The next chapter will cover:
- Merge conflicts
- Why merge conflicts occur
- Resolving conflicts manually
- Using Visual Studio Code to resolve conflicts
- Completing a merge after conflict resolution
- Best practices to avoid merge conflicts
Click Here to view the Next tutorial.
Tutorial Index:
- Part 1: Git Installation and Setup Guide
- Part 2: Understanding Git and Connecting to Your Self-Hosted GitLab
- Part 3: Git Workflow – Creating, Staging, and Committing Changes
- Part 4: Working with Remote Repositories (Push, Pull, and Fetch)
- Part 5: Working with Git Branches
- Part 6: Understanding and Resolving Merge Conflicts
