DEV Community

Qudus Olaniyi YUSUFF
Qudus Olaniyi YUSUFF

Posted on

A Beginner’s Guide to Git Branching and Merging (Without the Panic)

Imagine you are working on a new feature for your web app. You write some code, but everything breaks, and suddenly your whole project is ruined. This is why Git branches exist. They allow you to test new ideas in a safe sandbox without touching your main, working code. By using the terminal, you gain complete control over your project history and collaborate like a professional developer.

Prerequisites: Setting the Stage

Before you can practice branching, you need a live Git repository with at least one save point (commit). Git cannot track or display branches in an empty repository.

Open your terminal, create a project folder, initialize Git, and create your foundational commit by running these commands:

mkdir git-blog-post && cd git-blog-post
git init
git branch -m main
echo "Base project structure" > README.md
git add .
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Now that your repository has a history, you are ready to manage your branches.

Step 1: Check Your Current Branch

Before changing anything, you must find out exactly where you are standing in your repository. Run the following command:

git branch
Enter fullscreen mode Exit fullscreen mode

This command lists all local branches. The branch with an asterisk (*) next to it is your active branch.

Terminal output showing the active local branch marked with an asterisk

Step 2: Create and Switch to a New Branch

To experiment safely, create an isolated workspace. Run this command to create a new branch named feature-test and switch to it immediately:

git checkout -b feature-test
Enter fullscreen mode Exit fullscreen mode

Using the -b flag is a handy shortcut. Without it, you would have to run two separate commands: git branch feature-test to create it, and then git checkout feature-test to switch over. This flag combines them into a single step, saving you precious terminal keystrokes.

Terminal output confirming a successful switch to the new feature-test branch

Step 3: Make and Commit Your Changes

Now, create a simple file to simulate working on a new feature. Run the following command to create a new file named sample.txt with some placeholder text:

echo "This is a new feature test." > sample.txt
Enter fullscreen mode Exit fullscreen mode

Next, stage the file to prepare it for saving:

git add sample.txt
Enter fullscreen mode Exit fullscreen mode

Finally, permanently save this snapshot to your branch history with a descriptive commit message:

git commit -m "Testing a new feature"
Enter fullscreen mode Exit fullscreen mode

Terminal output showing the file being created, staged, and successfully committed

Step 4: Merge the Changes Back to Main

Your feature works perfectly, so it is time to bring it into your main project. First, navigate back to your primary branch:

git checkout main
Enter fullscreen mode Exit fullscreen mode

Now, pull the work you did on feature-test into your main branch:

git merge feature-test
Enter fullscreen mode Exit fullscreen mode

This performs a "fast-forward" merge, cleanly moving your main branch pointer up to match the latest commit you made in your feature sandbox.

Terminal output showing a successful fast-forward merge into the main branch

Conclusion

Mastering branching is the first real step to working like a professional engineer. Now you can experiment with confidence, knowing your main code is always safe.

Top comments (0)