what is git branching?
Git branching is a feature of the Git version control system that allows you to create separate lines of development within a project. Each line of development is called a branch, and it allows you to work on different features or fixes without affecting the main or "master" branch.
Let me give you an analogy.Imagine you are a chef who has a recipe book for making pizzas. You want to experiment with some new toppings for the pizza, but you don't want to mess up the original recipe.
So you make a copy of the original recipe, and on this new copy, you start experimenting with new toppings. You can add and remove toppings, adjust the cooking time, and do whatever you want without worrying about ruining the original recipe.
This new copy of the recipe is like a branch in Git. It allows you to make changes and experiment without affecting the original code or recipe. Once you're happy with your changes, you can merge them back into the original code or recipe, just like you would add your successful new pizza recipe to your original recipe book.
what is git revert and reset?
git revert
undoes a commit by creating a new commit that reverses the changes made in the original commit. It does not remove the original commit from the repository's history. This means that you can usegit revert
to undo changes without losing any of the commit histories.git reset
is a more powerful command that can be used to undo changes more drastically. It resets the repository's HEAD to a previous commit, discarding all changes made since that commit. This means that any commits made after the reset point are effectively removed from the repository's history.
what are git rebase and git merge?
git merge
combines changes from two branches into a new branch, whilegit rebase
applies changes from one branch to another, building on top of the original branch.
Let's take the same analogy again : )
Imagine you have a recipe book with different pizza recipes. You want to create a new pizza recipe that combines the toppings from two existing recipes.You have two options:
git merge
is like mixing two pizza recipes together. You take the toppings from each recipe and put them on the pizza in a new order. This creates a new pizza recipe that incorporates the toppings from both original recipes. The downside is that the recipe has a longer list of toppings and can become more complicated to read and understand.git rebase
is like creating a new recipe that builds on top of an existing one. You take one pizza recipe and add toppings to it one by one, and then you take the toppings from the second recipe and add them on top. The result is a new recipe that has a cleaner, simpler list of toppings and builds on top of the original recipe.
Tasks:
Creating and modifying a feature branch in Git
step 1: create a new branch dev by using the commandgit checkout -b dev
step 2: Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside.
step 3: version01.txt should reflect at Local repo first followed by Remote repo for review.
In Local👇In Remote👇
select the dev branch in the remoteNow we can see our git repo here
Now we can see the content in version01.txt**[see the path]**
step 4: Add a 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 the message “Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with the message “Added feature3 in development branch"
3rd line>> This feature will gadbad everything from now.
Commit with the message “ Added feature4 in development branch"
we can check our commits using the command
git log
Now we can see the file contents after commit
step 5: Restore the file to a previous version where the content should be “This is the bug fix in development branch”
☝️Here, we can see our last commit is "This is the bug fix in development branch". Hence git revert is successfully done
Demonstrate the concept of branches with 2 or more branches with screenshot.
step 1: check your current branch by using the commandgit branch
step 2: if you want a new branch, then use the command
git checkout -b branch_name
This creates a new branch.step 3: To display all the branches created then use the command
git branch
step 4: if you want to switch to your main branch, use the command
git checkout another_branch_name
in my case I switched to main👇Add some changes to dev branch and merge that branch in your default branch
step 1: To do changes in dev,
git checkout dev
, create a file and add some content into the file, thengit add
followed bycommit
command, as shown below.step 2: To merge the dev branch changes in your default branch, first we need to checkout to our default branch.
Now we can merge it by using the command
git merge your_another_branch_name
as shown below.step 3: Now to check, whether the contents are added or not, just type
ls
Hence, we can see our dev branch content version01.txt in our main branch.
Before MergeAfter Merge
As a practice try git rebase too, see what difference you get.
step 1: Switch to the dev branch or any desired branch you wish, create a file and add some content to it. Latergit add .
andgit commit
it.
step 2: Now typels
in the dev branch, we can see rebase.txt is created in the dev branch.step 3: Now to rebase it to our main branch, we need to switch to the main branch, Here you type
ls
, and we can see rebase.txt is not visible in the main branch yet, hence now we need to rebase it.step 4: To rebase it, we use the command
git rebase your_branch_name
And finally, we can see the rebase.txt file in our main branch
Thank you so much for reading.
Follow me on LinkedIn to see interesting posts like this : )