Day 10: Advance Git & GitHub for DevOps Engineers.

Git Branching:

What Is Git & Why Should You Use It? | Noble Desktop Blog

Git branching is a powerful feature of the Git version control system that allows you to work on multiple versions of your code simultaneously. A branch is a copy of your code that you can modify independently of other branches. This allows you to experiment with new features, fix bugs, or work on different aspects of your project without affecting the main branch.

Git Revert and Reset:

How to Revert a Commit in Git - UpSkillMe

Git revert creates a new commit that undoes the changes made in a previous commit. This is useful when you want to keep a record of the original changes, but also want to undo them. The revert commit contains information about the commit being reverted and the changes made to undo it.

Git reset, on the other hand, moves the HEAD pointer and resets the staging area to a specific commit. This is useful when you want to completely remove changes made after a certain point in time. Git reset has three modes: soft, mixed, and hard, each of which resets the repository to a different state.

Git Rebase and Merge:

Git Merge vs Git Rebase. TLDR | by Needone App | Medium

Git merge creates a new commit that combines the changes from two or more branches. This is useful when you want to merge changes from a feature branch into a main branch. The merge commit contains information about the commits being merged and the changes made to combine them.

Git rebase, on the other hand, moves the entire feature branch to begin on the tip of the main branch, effectively replaying all of the commits from the feature branch on top of the main branch. This is useful when you want to maintain a linear history or when you want to incorporate changes from a main branch into a feature branch before merging.

Task 1:

Add a text file called version01.txt inside the sample/ with “This is first feature of our application” written inside. This should be in a branch coming from master

Step-1

Clone the Repository to your local system:

Step-2

Make sure you are in master branch, Create a branch name dev

git branch <branch name>
git checkout <branch name>

Step-3

Create a file name version01.txt & add content to it “This is the first feature of our application”.

Step-4

Now add it and commit it with the message "Added new feature"

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 gadbad code

  • Commit this with the message “ Added feature3 in the development branch

  • 3rd line>> This feature will gadbad everything from now.

  • Commit with the message “ Added feature4 in the development branch

In this Process just edit the file, add it and commit every time with a new comment and can verify with Git log:

Learn GIT Commit command to save New, Modified, or All changed files ...

git log

Now we have to get to the version where the feature is "This is the bug fix in development branch" which we did in the feature2 commit

We can move to that commit by taking the commit id and reverting to that commit id

git revert < commit id >

But now as we are making changes to the same file while reverting, there will be merge conflicts which we need to resolve by personally editing the file and then follow the below process:

Or else we can directly jump to the feature2 commit by git reset command, it will delete all the commits till that commit id which we give.

git reset --hard < commit id >

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 in master

As a practice try git rebase too and see what difference you get.

Here at First, I create two new branches named Dev-1 & Dev-2 through master and see the branches through git branch command.

Also for the next task see the content inside the master branch.

git checkout < master branch >

Then add some content in a file in dev branch and try to merge it with the master branch:

First adding some content in the dev branch and committing that changes

As you can see, firstly there is no file named version01.txt but after using git merge command we can see version01.txt file along with its content coming into the master branch as it will get all the commits added from dev to master after using git merge command.

Git Rebase:

Git Visual Reference

git rebase <branch name>

For the task related to rebase:

As we can see, in dev1 - we have "feature4" commit as the last commit, while in dev branch we have "last change" as the last commit

When we do git rebase dev in the dev1 branch, all the commits above the dev1 branch will get added on the top of the stack in the commit history of dev1.

It is demonstrated below:

Before rebase:

After rebase:

Hope you found it helpful :)

Thank you

Shubham Jangale