> BOOTING CV-SYSTEM v2.0
back_to_writing
Devops 26 Jun 2026

Git Branching, Merging & Repository Management Explained for Beginners (2026)

Learn how Git branching works, the difference between fast forward and three way merges, how to resolve merge conflicts, and how to manage your repository with .gitignore, tags, and remotes — with real examples.

Git Branching, Merging & Repository Management Explained for Beginners (2026)

Git Branching, Merging & Repository Management Explained for Beginners (2026)


The Problem That Branching Solves

Imagine you are working on a project with three other developers. You are all pushing code to the same place. One developer is building a new login feature. Another is fixing a critical bug. You are working on a payment module. A fourth person is experimenting with a completely new UI that may or may not make it into the product.

If everyone commits directly to the same codebase at the same time, things break constantly. Half-finished features interfere with each other. A bug fix gets tangled up with new feature code. The experimental UI breaks the entire application for everyone.

Branching solves this completely. Instead of everyone working on the same line of code, each developer works on their own isolated copy — a branch. Changes on one branch have zero effect on any other branch until you deliberately decide to combine them.


What is a Branch?

A branch in Git is a lightweight, movable pointer to a specific commit. When you create a branch, you are not copying the entire codebase — you are just creating a new pointer that starts at the current commit and moves forward independently as you make new commits.

This is why branching in Git is so fast. Creating a branch takes milliseconds regardless of how large the project is.

Every Git repository starts with one default branch. Historically this was called master — most platforms and teams now use main instead.

When you create a new branch and start committing on it, those commits are completely isolated from the main branch. The main branch stays exactly as it was until you decide to merge your branch back into it.


Why Teams Use Branches — The Feature Branch Workflow

The most common branching strategy used by professional teams is the feature branch workflow. The idea is simple:

  • The main branch always contains stable, production-ready code
  • Every new feature, bug fix, or experiment gets its own branch
  • When the work is complete and reviewed, the branch is merged back into main
  • The branch is then deleted

This means main is always in a deployable state. No half-finished features, no experimental code, no accidental breakage from someone else's work.


Core Branching Commands

Creating a Branch

# Create a new branch
git branch feature-login

# Create a new branch AND switch to it immediately
git checkout -b feature-login

# Modern way to create and switch (Git 2.23+)
git switch -c feature-login

Viewing Branches

# List all local branches
git branch

# List all branches including remote branches
git branch -a

The branch you are currently on will have an asterisk next to it.

Switching Between Branches

# Switch to an existing branch
git checkout main

# Modern way to switch
git switch main

When you switch branches, Git updates your working directory to match the state of the branch you switched to. Your files literally change to reflect the different branch.

Deleting a Branch

# Delete a branch that has been merged
git branch -d feature-login

# Force delete a branch (even if not merged)
git branch -D feature-login

What is Merging?

Merging is the process of taking the changes from one branch and integrating them into another. When your feature is complete, you merge your feature branch into main so the rest of the team gets your changes.

# Switch to the branch you want to merge INTO
git checkout main

# Merge the feature branch into main
git merge feature-login

Git handles two types of merges depending on the situation:


Fast Forward Merge

A fast forward merge happens when the branch you are merging in is directly ahead of the branch you are merging into — meaning main has not had any new commits since you created your feature branch.

In this case, Git simply moves the main pointer forward to point at the latest commit on the feature branch. No new commit is created. The history stays perfectly linear.

Before merge:
main:    A --- B
                \
feature:         C --- D

After fast forward merge:
main:    A --- B --- C --- D

Three Way Merge (Merge Commit)

A three way merge happens when both branches have had new commits since they diverged — meaning main has moved forward while you were working on your feature branch.

In this case, Git finds the common ancestor commit, compares both branches against it, and creates a new merge commit that combines both sets of changes.

Before merge:
main:    A --- B --- E
                \
feature:         C --- D

After three way merge:
main:    A --- B --- E --- M (merge commit)
                \         /
feature:         C --- D

Merge Conflicts — What They Are and How to Fix Them

A merge conflict happens when two branches have made different changes to the same part of the same file. Git does not know which version to keep, so it stops the merge and asks you to decide.

This sounds scary but it is a normal, manageable part of working with Git.

When a conflict happens, Git marks the file like this:

<<<<<<< HEAD
This is the version from the main branch
=======
This is the version from the feature branch
>>>>>>> feature-login

To resolve it:

  1. Open the conflicted file
  2. Decide which version to keep — or write a new version that combines both
  3. Delete the conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Save the file
  5. Stage the resolved file with git add
  6. Complete the merge with git commit

The best way to avoid frequent conflicts is to keep branches short-lived — merge them back quickly rather than letting them diverge for weeks.


Repository Management — Keeping Things Clean

As projects grow, repositories need to be maintained. Here are the key repository management practices every developer should know.


The .gitignore File

Not every file in your project should be tracked by Git. Log files, compiled binaries, dependency folders, and files containing sensitive information like API keys should never be committed.

The .gitignore file tells Git which files and folders to ignore completely.

A typical .gitignore for a Node.js project:

node_modules/
.env
*.log
dist/
.DS_Store

Once a file is listed in .gitignore, Git will never track it — it will not show up in git status and cannot be accidentally committed.

Important: Add your .gitignore file at the very beginning of a project, before your first commit. If you add files to .gitignore after they have already been committed, Git will continue tracking them.


Git Tags — Marking Important Points in History

Tags are used to mark specific commits as significant — typically used to mark release versions.

# Create a tag for version 1.0.0
git tag v1.0.0

# Create an annotated tag with a message
git tag -a v1.0.0 -m "First stable release"

# Push tags to the remote repository
git push origin --tags

# List all tags
git tag

Unlike branches, tags do not move as new commits are made. A tag always points to the exact commit it was created on — making it a permanent marker in your history.


Managing Remote Repositories

# View all configured remote repositories
git remote -v

# Add a new remote repository
git remote add origin https://github.com/username/repo.git

# Change the URL of an existing remote
git remote set-url origin https://github.com/username/new-repo.git

# Remove a remote
git remote remove origin

Useful Maintenance Commands

# Fetch updates from remote without merging
git fetch origin

# See all commits in a visual branch graph
git log --oneline --graph --all

# Undo the last commit but keep the changes in working directory
git reset --soft HEAD~1

# Temporarily save uncommitted changes and clean the working directory
git stash

# Bring back stashed changes
git stash pop

A Complete Branching Workflow — From Start to Finish

Here is exactly what a professional Git workflow looks like for adding a new feature:

# 1. Make sure main is up to date
git checkout main
git pull origin main

# 2. Create a new branch for the feature
git checkout -b feature-user-profile

# 3. Do your work — edit files, make commits
git add .
git commit -m "Add user profile page layout"
git add .
git commit -m "Connect profile page to user API"

# 4. Push the feature branch to remote
git push origin feature-user-profile

# 5. Open a Pull Request on GitHub for code review
# (done through the GitHub website)

# 6. After approval, merge into main
git checkout main
git merge feature-user-profile

# 7. Push the updated main to remote
git push origin main

# 8. Delete the feature branch — work is done
git branch -d feature-user-profile
git push origin --delete feature-user-profile

This workflow keeps main clean, makes code review possible, and gives every feature its own isolated history.


Branching Commands Reference Table

CommandWhat it does
git branchList all local branches
git branch -aList all branches including remote
git branch <name>Create a new branch
git checkout -b <name>Create and switch to a new branch
git switch <name>Switch to an existing branch
git merge <branch>Merge a branch into the current branch
git branch -d <name>Delete a merged branch
git branch -D <name>Force delete a branch
git tag <name>Create a tag at the current commit
git stashTemporarily save uncommitted changes
git stash popRestore stashed changes
git remote -vView configured remote repositories
git fetch originDownload remote changes without merging

Interview Q&A

Q1. What is a branch in Git and why is it used?

A branch in Git is a lightweight pointer to a specific commit that moves forward independently as new commits are made. Branches allow developers to work on features, bug fixes, or experiments in complete isolation from the main codebase. Changes on one branch have no effect on any other branch until they are deliberately merged. This makes it safe for multiple developers to work simultaneously without interfering with each other, and keeps the main branch always in a stable, deployable state.


Q2. What is the difference between a fast forward merge and a three way merge?

A fast forward merge happens when the branch being merged is directly ahead of the target branch — meaning the target branch has had no new commits since the feature branch was created. Git simply moves the target branch pointer forward, keeping history linear with no new commit created. A three way merge happens when both branches have diverged — both have new commits since they split. Git finds the common ancestor, compares both sets of changes, and creates a new merge commit that combines them. A three way merge is needed when parallel work has happened on both branches simultaneously.


Q3. What is a merge conflict and how do you resolve it?

A merge conflict occurs when two branches have made different changes to the same part of the same file and Git cannot automatically determine which version to keep. Git marks the conflicted sections in the file with conflict markers showing both versions. To resolve it, you open the file, manually decide which version to keep or write a combined version, remove the conflict markers, save the file, stage it with git add, and complete the merge with git commit. The best way to minimize conflicts is to keep branches short-lived and pull from the main branch frequently.


Q4. What is a .gitignore file?

A .gitignore file is a configuration file in a Git repository that tells Git which files and directories to ignore and never track. Common examples include dependency folders like node_modules, environment files containing secrets like .env, compiled output directories like dist, and operating system files like .DS_Store. Files listed in .gitignore will never appear in git status and cannot be accidentally committed. It should be created at the very beginning of a project before the first commit.


Q5. What is the difference between git fetch and git pull?

git fetch downloads the latest commits and branches from the remote repository but does not modify your local working directory or current branch — it just updates your local knowledge of what the remote contains. git pull does everything git fetch does plus automatically merges the downloaded changes into your current branch. git fetch is useful when you want to see what has changed on the remote before deciding to integrate those changes. git pull is the standard command when you simply want to update your local branch with the latest remote changes.


Q6. What are Git tags and how are they different from branches?

Git tags are permanent markers pointing to specific commits, typically used to mark release versions like v1.0.0. Unlike branches, tags do not move as new commits are made — a tag always points to the exact commit it was created on, making it a fixed reference point in history. Branches are dynamic and move forward with each new commit; tags are static. Tags are pushed to remote repositories separately using git push origin --tags and are commonly used in CI/CD pipelines to trigger release deployments when a new version tag is created.


Zero to DevOps Series — Blog 8

#Git Branching#Git Merge#Merge Conflict#Git Repository Management#gitignore#Git Tags#Git Workflow#DevOps 2026