What is Git Branching?
Git branching is a fundamental concept in the Git version control system that allows you to work on different versions of your project simultaneously.
In git the default branch is master and the master branch generally represents the production so if you want to fix any bugs or want to develop a new feature you can not directly make changes to master. To make it more secure we can use git branching where you can make branches that have copies of master files then you can make changes and try out different features in a new branch and if those changes are approved then you can merge those changes to the master branch.
To create a branch the command is git branch <branchname>
and you can switch to it using the git checkout <branchname>
command.
You can use the below single command to create and switch to the new branch,
git checkout -b <branchname>
Git Revert and Reset
In Git you are committing your changes but if something went wrong and you want to undo the changes then Git Revert and Reset come into the picture.
Git revert creates a new commit that undoes the changes made in a previous commit. This is useful when you want to undo a change but still keep a record of the original change in your Git history. In simple words, the commits will not be removed just your head will point to the new commit.
The command used to revert is, git revert <hash>
Git reset allows you to undo changes by moving your branch pointer to a different commit, In simple words, it will delete all the commits history after the reset point from your git history. This is a powerful but potentially dangerous command, so use it with caution.
The command used to reset is, git reset <hash>
Git Rebase and Merge
Git merge takes the changes from one branch and merges them into another branch. For example, you have a "dev" branch so you can merge it to the master branch using the git merge dev
command. This creates a new commit that combines the changes from both branches. The result is a new commit that has two or more parent commits.
Git rebase applies the changes from one branch onto another branch, effectively moving the entire branch to a new base. This results in a linear history with a single branch, making it easier to understand and follow the development of the project. you can use this command
git rebase <branchname>
Git cherry-pick
git cherry-pick
is a Git command that allows you to apply a specific commit from one branch onto another branch.
For Example, you have a branch called dev
that contains multiple commits, but you only want to apply one of those commits to your master
branch. You can use git cherry-pick <hash>
to apply the specific commit onto your master
branch. When you use cherry-pick it will create a new commit to your master branch.
Task 1:
Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside. This should be in a branch coming from master
, switch to dev
branch ( Make sure your commit message will reflect as "Added new feature").
version01.txt should reflect at local repo first followed by the Remote repo for review.
Here you can see we have pushed the local changes to remote(GitHub)
Add a new commit in dev
branch after adding the 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 the development branch
Commit this with the message “ Added feature2 in development branch”
2nd line>> This is the Wrong code
Commit this with the message “ Added feature3 in the development branch
3rd line>> This feature will go wrong everything from now.
Commit with the message “ Added feature4 in the development branch
Here we use git add and git commit commands and we can view this by using the git log command,
All commits are now pushed to remote from the dev branch by using the below command,
git push origin dev
Restore the file to a previous version where the content should be “This is the bug fix in the development branch”.
So for this, we have to use git revert and commit hash which contains the message "Added feature 2 in development branch".
git revert <50b8d3d>
But in my case, it gives me conflict to resolve the conflict you can follow the below steps,
When you run
git revert
with a specific commit hash, Git tries to undo the changes made by that commit. However, in my case, Git is unable to do so due to a conflict in the fileGit/version01.txt
.To resolve this conflict, you need to manually edit the file to remove the conflicting changes. Once you have resolved the conflict, you should add the modified file to the staging area using
git add <path_to_file>
.After you have added the file, you can continue with the revert operation by running
git revert --continue
.
Task 2:
Demonstrate the concept of branches with 2 or more branches with a screenshot.
Add some changes to
dev
branch and merge that branch inmaster
I have made changes to the dev branch and now merging it into the master,
Switch back to the
master
branch by running the commandgit checkout master
.Merge the
dev
branch into themaster
branch by running the commandgit merge dev
.If there are no conflicts, the changes made in the
dev
branch will be merged into themaster
branch. You can confirm this by checking theproject.txt
file in themaster
branch.
-
As a practice try git rebase too and see what difference you get.
As a practice, you can try using
git rebase
instead ofgit merge
to integrate the changes from thedev
branch into themaster
branch. Thegit rebase
command is used to apply changes from one branch to another by replaying each commit on top of the other branch.To do this, first, switch to the
dev
branch by running the commandgit checkout dev
, add the changes to project.txt and add and commit the changes.Finally, switch to the
master
branch by running the commandgit checkout master
and run commandgit rebase dev
check theproject.txt
file to confirm that the changes have been applied.
Thank you for reading the blog.
Suggestions are always welcome. Thank you !!