git|July 13, 2020|3 min read

A Practical Guide in understanding Git Branch and Conflict resolution during merge

TL;DR

Learn how to create and manage Git branches, merge them, and resolve conflicts that arise when the same files are modified on different branches.

A Practical Guide in understanding Git Branch and Conflict resolution during merge

Introduction

In this guide, We will learn about branching, handling conflict resolution during merge.

I hope you have seen previous git training posts:

We should never work directly in master branch. For any feature work, we should create a new branch based on master (or whatever is your stable or active dev branch). And, should merge back to master or active branch when the feature is complete.

List Local and Remote Git Branches

$ git branch -a

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

It lists down all local and remote branches, and put an asterisk * with the active branch.

Create a new Git Branch

To create a new branch, use git branch <branch name>

$ git branch feature_branch

# lists branches

$ git branch -a
  feature_branch
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

We have created a new branch, but not yet switched to that. To switch to another branch use git checkout <branch name>

Swith to another Git Branch

$ git checkout feature_branch
Switched to branch 'feature_branch'

$ git branch -a
* feature_branch
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Rename a Git Branch

To rename an existing branch:

$ git branch -m feature_branch fb_branch

$ git branch -a
  fb_branch
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Delete a Git Branch

To delete branch, first checkout to another branch. Then,

$ git branch -d ff
Deleted branch ff (was fc7c680).

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

It will delete the branch without any warning.

Create a Branch and Switch into it Single command

$ git checkout -b feature
Switched to a new branch 'feature'

$ git branch -a
* feature
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Merge our Branch into Master

First switch to your main branch by git checkout. Have a look at the differences by git diff.

To see the diff between branches:

git diff master feature_branch

If everything looks good. Lets merge it.

$ git merge feature_branch

Updating fc7c680..d8fa86a
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

Console shows that it was a Fast Forwarded merge. When you do a fast-forward merge, All the commits in your source branch becomes part of your target or active branch. And there will be no trace of the branch from where you merged the code.

To disable Fast-Forward commiit

git merge feature_branch --no-ff

This will open up an editor to put a message. Now, we will have the trace of branch in log history, Even after deleting the branch.

$ git log --oneline --graph --decorate --all

*   a0a3150 (HEAD -> master) Merge branch 'bugfix'
|\  
| * fcf0a1f bug desc
|/  
* e4b09ba adding another file
* d8fa86a Adding another feature
* fc7c680 (origin/master, origin/HEAD) another commit
* 1170bc1 Update test2.txt
* e7dd2a0 t2
* 007675f t1
* 1a2af9c test
* f997108 test
* 9a5a776 test
* 85587c6 test1
* 574c251 test 1

Delete a Branch

Now when we have merged the developed code into our main branch. We can safely delete the branch.

git branch -d feature

Handling Conflicts

Example, you created a branch and did some changes. Now, you want to merge your branch in master branch. But, someone else has done some changes in Master branch as well.

In this case, to perform an automatic merge:

git merge <branch_name> -m "message"

In the git log command, you will see both commits.

In scenarios, when changes are complex. And, merge can not happen automatically. There can be conflicts in some files.

When you try to do the merge, it shows error stating name of each file that is causing conflicts. Now, you can open the file in an editor and manually resolve the conflicts.

Or, if you have configured a visually diff tool like p4 merge tool, you can do this visually as well. Once you resolve your conflicts, just do a git commit -m "message"

Related Posts

A Practical Guide on Understanding Git Best Practices

A Practical Guide on Understanding Git Best Practices

Introduction In this post, we will learn about some of Best practices while…

A Practical Guide on how to work with Git Basic Commands and workflows

A Practical Guide on how to work with Git Basic Commands and workflows

Introduction In this guide, we will see git basic commands, and fundamentals of…

A Practical Guide on how to work with Git log command and history

A Practical Guide on how to work with Git log command and history

Introduction In this post, we will see ways to look at git history logs. For…

A Practical Guide on how to to create your own git command alias

A Practical Guide on how to to create your own git command alias

Introduction In this guide, We will learn on how to create some handy command…

A Practical Guide for better understanding Git Diff

A Practical Guide for better understanding Git Diff

Introduction In this guide, We will get basic understanding of various options…

Git - How to create a Pull Request with no history of commits

Git - How to create a Pull Request with no history of commits

Introduction If you working on a github project in a team. Consider you have…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…