Git Branching: Unlock Your Version Control Potential in 7 Key Points

Working with Git Branches

Branches:

Git branches are like parallel versions of your project. They allow you to work on different things without affecting the main version.

  • Why use branches?
    • To develop new features.
    • To fix bugs.
    • To experiment with code without disturbing the trunk.
  • In Git, you can create a branch from the main code (e.g., main) and then work on it independently. Once the changes are complete and tested, the branch can be merged back into the trunk (or another branch).

  Types of Branches:

  1. Feature Branches:
    • Used for developing a specific feature. Once the feature is complete, it can be merged back into the trunk or a staging branch.
    • Naming convention: feature/add-login-functionality.
  2. Hot-fix  or Bugfix Branches:
    • Created to fix a specific bug or issue. Once resolved, the fix is merged back into the trunk and often into other active branches if needed.
    • Naming convention: hotfix/fix-login-bug.
  3. Release Branches:
    • Used to prepare for a release. It allows final tweaks and bug fixes before pushing code to production.
    • Naming convention: release/v1.0.
  4. Long-lived Branches (like develop):
    • In some workflows (like Gitflow), there’s a long-lived develop branch that acts as an integration branch for feature branches before merging into the trunk.
    • Changes are integrated and tested in develop before finalising  in main.

3.Working with Branches in Git:

  • Create a new branch:
    git checkout -b new-branch-name
  • Switch to an existing branch:
    git checkout existing-branch-name
  • Merge a branch into another branch (e.g., merging a feature branch into main):
    git checkout main
    git merge feature-branch-name
  • Push a branch to a remote repository:
    git push origin branch-name
  • Delete a branch:
    git branch -d branch-name

4.Branching Stratagies

  • Gitflow: A structured workflow that uses feature branches, release branches, hotfixes, and a develop branch.
  • GitHub Flow: A simpler model where branches are created for features or fixes, merged back into the main branch, and deployed continuously.
  • Trunk-Based Development: Developers work directly on a short-lived branch and merge it quickly back into the main branch, with frequent integrations.

Branches in Git

Problems if we don’t use Git 

  • Manual copying: Create copies of all relevant files.
  • Dependency issues: Discover dependencies and copy more files.
  • Emergencies: Save work, switch to fix errors, then switch back.
  • Mistakes: Accidentally copy files before fixing errors.

Benefits of using Git

  • Branches: Create a new branch for the design update.
  • Emergencies: Create a new branch for the error fix.
  • Merging: Merge the error fix into the main branch.
  • Avoiding mistakes: Continue working on the design branch, merging it later.

Key benefits of Git branching:

  • Isolation: Work on different parts of the project without affecting the main version.
  • Flexibility: Easily switch between branches to focus on different tasks.
  • Efficiency: Lightweight and fast branching operations.
  • Error prevention: Avoid accidentally overwriting changes.

Creating New Git Branch

git branch branch-1

Now we have created a new branch named as branch-1
now lets check the branch formation by visiting the branch.

git branch
(you will se all the available branches )

To switch to a branch use

git checkout branch-1
(Switched to branch ‘branch-1’)

Now you are ready to work on the branch.

Switching Between Branches

Now let’s see just how quick and easy it is to work with different branches, and how well it works.
now we are on branch named branch-1 and if we want to switch  back to master branch , so to do that use command

 

git checkout master
(Switched to branch ‘master’)

Emergency Branch

Now imagine that we are working on branch-1, but we need to fix an error on master.
So to deal with this problem we create a new brach known as emergency branch
lets se how can we do that.

git checkout -b emergency-fix
(Switched to a new branch ’emergency-fix’)

And by using emergency branch you can easily fix bugs without effecting another branch.

Merge Branches

We have the emergency fix ready, and so let’s merge the master and emergency-fix branches.
First, we need to change to the master branch:

 

git checkout master
(Switched to branch ‘master’)

Now merge the current branch (master) with emergency-fix:

git merge emergency-fix
(1 file changed, 1 insertion(+), 1 deletion(-) )

As master and emergency-fix are essentially the same now, we can delete emergency-fix, as it is no longer needed

git branch -d emergency-fix
(Deleted branch emergency-fix)

Merge Conflict

Now we can move over to branch-1 and keep working. Add some code to demo.java  and change it, so it shows :

git checkout branch-1
Switched to branch ‘branch-1’)

Now, we are done with our work here and can stage and commit for this branch:

git add –all
git commit -m “added new code” 

We see that demo.java has been changed in both branches. Now we are ready to merge hello-world-images into master.
But what about the changes we made in master by merging emergency-merge ?

 

git checkout master
git merge branch-1
Auto-merging demo.java
CONFLICT (content): Merge conflict in demo.java
Automatic merge failed; fix conflicts and then commit the result.

The merge failed, as there is conflict between the versions for demo.java Let us check the status:

git status
On branch master
You have unmerged paths.
(fix conflicts and run “git commit”)

This confirms there is a conflict in demo.java
So we need to fix that conflict. Open the file in our editor:

We can see the differences between the versions and edit it like we want:
Now we can stage index.html and check the status:

git add demo.java
git status
(On branch master All conflicts fixed but you are still merging.)
(use “git commit” to conclude merge)

The conflict has been fixed, and we can use commit to conclude the merge:

git commit -m
(“merged with branch-1 after fixing conflicts”)

And delete the hello-world-images branch:

git branch -d hello-world-images
(Deleted branch hello-world-images)

 

check out full git resources –Click Here

visit our LinkedIn page –Click Here