Table of contents
Introduction
Welcome DevOps enthusiasts. Now we're headed to dive deep into the world of Git and GitHub and learn some advanced concepts. In this blog we will gain knowledge on some advanced features like Reverting, Reset, Rebase and merge. Also we'll understand the significance of Branches.
That's not it, we'll be carry forwarding our tradition of interactive blogs and by the end we'll be solving some hands on tasks for better understanding of the concepts. Stuck somewhere? No worries, I got you covered with the Solution section section at the end. So without any further delays let's begin learning.
Git Branching
What is Git Branch?
A branch in Git is the line of development. In the normal scenario, we have a default branch ("master" or "main" ) and then we have other sub branches called the feature branch.
The Feature branch are used to push code that contains changes from the local repository to the remote repository without disturbing the line of development i.e. the default branch.
These sub branches can later on get merged to the default branch if it meets the requirements of the project.
Naming Convention of Branches:
A branch can be named on the purpose they solve. Here's a table explaining the ideal naming convention that should be followed:
Branch Name | Purpose It solves |
Feature Branch | For changes like adding features or updating a feature. |
Bugfix branch | For commits containing Bug Fixes |
Release branch | For Newer Version |
Hotfix branch | For critical issues or bugs in a production environment |
Commands:
To Check the current branch
git branch
To Create a branch
git branch <name_of_branch>
To Checkout and Create a branch at the same time
git checkout -b <name_of_branch>
To list all branches
git branch -a
To Delete a branch
git checkout -d <name_of_branch>
Git Revert and Reset
What is Git Revert?
Git Revert is the command that is used for undoing changes to a repository's commit history.
Git Reverts creates a new commit in which the changes made in the previous commit are reverted or Undone. By doing this, it preserves the commit history and users can access to the past commit if needed.
NOTE: It does not delete the commit.
Commands:
To Revert a change
git revert <Hash_code_of_the_commit>
To Revert the previous commit
git revert HEAD
What is Git Reset?
This is a similar command as compared to git revert but the catch here is that it creates a commit undoing the changes and DELETES the commit which needs to be undone.
There are two types of Resetting:
--soft : It resets the commit but still we can have the access to the commit in the git history.
--hard : It resets the commit and deletes it. This way we don't have any access to the history and the commit cannot be accessed.
Commands:
git reset --soft HEAD~n
git reser --hard HEAD~n
Where n is the number of commits respectively from the head we want to reset.
Git Rebase and Merge
What is git Rebase?
Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow. The general process can be visualized as the following:
From the above we can conclude, rebasing is changing the base of your branch from one commit to another making it appear as if you'd created your branch from a different commit.
Git accomplishes this by creating new commits and applying them to the specified base.
NOTE: It's very important to understand that even though the branch looks the same, it's composed of entirely new commits.
Read more about Rebasing, Here.
What is Git Merge?
Git merge is a command used to merge two or more branches into the Main or Default branch.
Say we have a new branch feature that is based off the main
branch. We now want to merge this feature branch into main
.
Calling the git merge
command will merge the specified branch feature into the main branch.
Read more, Here.
Commands:
git merge
Hands On Practice
Task 1:
Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master
, [hint try git checkout -b dev
], switch to dev
branch ( Make sure your commit message will reflect as "Added new feature").
[Hint use your knowledge of creating branches and Git commit command]
- version01.txt should reflect at local repo first followed by Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
Add new commit in dev
branch after adding below mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines
1st line>> This is the bug fix in development branch
Commit this with message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with message “ Added feature3 in development branch
3rd line>> This feature will gadbad everything from now.
Commit with message “ Added feature4 in development branch
Restore the file to a previous version where the content should be “This is the bug fix in development branch” [Hint use git revert or reset according to your knowledge]
Task 2:
Demonstrate the concept of branches with 2 or more branches with screenshot.
add some changes to
dev
branch and merge that branch inmaster
as a practice try git rebase too, see what difference you get.
Solution
Task 1 :
Created version01.txt file, made changes and reflecting it into the remote repo.
Making Bug Fixes chances, committing and Pushing it the remote repository
Restoring the file to a previous version where the content should be “This is the bug fix in development branch”. We'll be using git reset command followed by --hard flag.
Now Merging The Commits
And Here we can see the branches are merged.
Task 2 :
Concept of Branch vis screenshot
Changes to dev branch and merged to main branch
Conclusion
So here comes the end of this blog. In summary we covered the major concepts of branch, naming convention of branches, commands and also understood the differences between Git Revert, Git Reset and Git Rebase and Git Merge. Along with that the hands on practice section acted as a cherry on the cake making our learning even more fun and robust.
Ending this with a quote
In theory, theory and practice are the same. In practice, they’re not.
Happy Learning.