Advance Git & GitHub for DevOps Engineers

Advance Git & GitHub for DevOps Engineers

Day 10 Task of #90DaysOfDevOps

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 the local repo first followed by the Remote repo for review.

  • 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 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

          Restore the file to a previous version where the content should be “This is the bug fix in the development branch”

Solution

For making a repository DevOps and setup the repo on the local as well as remote on GitHub refer to my previous blog here↗.

Let's begin with the first task.

git checkout -b dev

echo "This is the first feature of our application" > version01.txt

git add .

git commit -m "Added new feature"

vim version01.txt
# added line > This is the bug fix in development branch
git add version01.txt
git commit -m "Added feature2 in development branch"

vim version01.txt
# added line > This is gadbad code
git add .
git commit -m "Added feature3 in development branch"

vim version01.txt
# added line > This feature will gadbad everything from now.
git add .
git commit -m "Added feature4 in development branch"

git log --oneline
#Check log below:

Let's revert to 6db60b5:

git revert 6db60b5

There is a conflict here, we have to resolve it.

git status

vim version01.txt
# Removed Changes
git add .
git commit -m "This is the bug fix in development branch"

git log --oneline

git diff d221ec3 56e19ac

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.

Solution

Branches:

Branches in Git are used for a good and clean workflow.

For example, If a company is developing a website for their company then it can start with the master branch where all the production codes are stored. 2nd branch would be the Dev branch where developers are working on the development, also Dev branch may have other branches like Feature-1 and Feature-2 where multiple features are developing. And by this, the company's codes are distributed and easily managed.

Here, I have two branches one is master and another is dev.

To get the list of available branches run the command:

git branch

' * ' indicates your current branch.

To change the branch there are two commands which can be used:

  • git switch <branch name>

  • git checkout <branch name>

switch command is used for switching the branch whereas checkout has more features other than switching the branch.

i.e If you modify a file but haven't staged the change, then git checkout <filename> will reverse the modifications. it is a quick and easy way to cancel changes to a file. And you will remain in the same branch.

You can perform many more actions with its parameters as well. You can find it here↗ in the official documentation.

For the git switch you can refer to the official documentation here↗.

Now run the command:

git switch master

You will be switched to branch master.

Now as I have made changes in the dev branch I will merge into the master branch.

git merge -m "merging the dev with the master branch" dev

git log --oneline

As you can see in the above screenshot all the dev branch commits are now in the master branch.

Now let's git rebase the master branch by adding some data in the dev branch.

Adding New Branches to the dev branch:

Adding feature-1 and committing the change.

git branch

git checkout -b feature-1

echo "This is app description file for feature 1." > info.txt

git add .
git commit -m "added info file"

Switch back to the dev branch:

Adding feature-2 branch and committing changes.

git switch dev
git checkout -b feature-2

echo "This is app copyright file for feature 2." > copyright.txt

git add .
git commit -m "added copyright file"

Merging both into the dev branch:

git switch dev

git merge feature-1

git merge feature-2

git log --oneline

Rebasing the dev branch into the master branch:

git checkout master

git rebase dev

git log --oneline

Let's push the changes to the remote repository:

git fetch --all

git rebase remotes/origin/master

git push origin master

Well, now let's check the log and see the magic of the git rebase.

From the logs, now there are no feature-1 and feature-2 branches. It has been rebased.

For the difference, check the below image of the git merge log which contains both of the branches as a history.

Done.

If you like the post do like, have any queries? do comment and share the blog 😊

You can also subscribe to newsletter for updates on my blogs, below 👇.