Git and GitHub Made Simple: A Complete Guide - Part 7
Table of contents
- What is a branch?
- Why and When Do We Create a Branch?
- Listing All Branches
- Creating a New Branch
- Listing All Remote Branches
- Switching to a New Branch
- Creating and Switching to a New Branch in One Go
- First Push of a Newly Created Branch to Remote Repo
- Merging Files from Source -> Target Branch
- Understanding HEAD
Welcome to Phase 7 of our DevOps journey. Today, we're diving into the world of branching in Git.
What is a branch?
A branch is like a parallel universe for your code. It allows you to work on new features or bug fixes without affecting the main codebase[Master or Main Branch]. Think of it as a sandbox for your ideas.
Why and When Do We Create a Branch?
Branching is a fundamental concept in Git, allowing multiple developers to work together efficiently. It's your way of keeping your code organized, clean, and ready for whatever the future holds.
Branches are handy when you want to:
Develop a new feature
Fix a bug
Experiment with new ideas
Keep your main codebase clean
Listing All Branches
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git branch
This command lists all the local branches in your repository.
Creating a New Branch
The branch can be created by the following command- git branch new_branch_name
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git branch release-1.1
Create a new branch with the given name.
Listing All Remote Branches
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git branch -r
List all the branches on your remote repository.
Switching to a New Branch
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git checkout release-1.1
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git branch
Switch to the branch you want to work on.
Creating and Switching to a New Branch in One Go
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git checkout -b release-1.2
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git branch
This one-liner creates and switches to a new branch. Is Handy, right?
First Push of a Newly Created Branch to Remote Repo
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git remote add git-hash https://github.com/theshubhamgour/git-hash.git
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git push origin release-1.2
These commands add a remote repository and push your new branch to it.
Merging Files from Source -> Target Branch
theshubhamgour@ubuntuM1:~/Desktop/git-hash$ git merge release-1.2
Merge changes from the source branch to your target branch.
Understanding HEAD
HEAD is like your GPS in Git. It always points to the latest commit in your repository. It helps you navigate through your project's history.
HEAD is a pointer in git which
- Always points to the latest commit in the repository
- Always points to tip of the current reposiotory
- Always points to parent of the next commit
So go ahead, create branches, experiment, and keep your codebase in tip-top shape. Your DevOps journey continues, and you're doing great!
And as i always say - This is not the end more to come Join me in this series and let's connect on LinkedIn
#DevOps #Git #Branching #VersionControl #HappyCoding