Site icon Student Projects Live

Working with Git Branches

Understanding and Resolving Merge Conflicts in GIT

Git Tutorial – Part 5: Working with Git Branches

Objective

In this tutorial, you will learn how to:


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:


Understanding Branches

Consider the following example:

main
 │
 ├───────────────●──────────────●
                 │
                 │
           feature/login
                 │
                 ●──────●──────●

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:


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

CommandDescription
git branchList 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 -aList all local and remote branches.

Best Practices


Summary

In this chapter, you learned how to:

Using branches allows teams to develop features independently while keeping the main branch stable.


Next Tutorial

The next chapter will cover:

Click Here to view the Next tutorial.


Tutorial Index:

Exit mobile version