Day 10: 90DaysOfChallenge

Day 10: 90DaysOfChallenge

Advance Git & GitHub for DevOps Engineers

Git Branching

Git Branching is a strategy where branches are created for specific feature, bug or experimental ideas by taking a clone from the main repository. This means that a copy of the main repository is created in branch where developers can do their experimental changes on their own branch without affecting the main branch. And once their development is completed they can either merge their branch with another branch or push their changes to the main branch.

Git Revert and Reset

Git Revert is used to revert the specified commit. It is a kind of undo command. It will create a new commit while reverting the commit and also maintains the version history before the commit. We can revert the commit by simply giving git revert command.

Git Reset is used to reset the changes but without maintaining the version history. There are three types of git reset - hard, soft and mixed.

  • Git reset --hard: The --hard option changes the Commit History, and ref pointers are updated to the specified commit but any previously pending commits to the Staging area will be lost.

  • Git reset --mixed: The --mixed option updates the ref pointers to the specified commit without losing any data from the Staging area.

  • Git reset --soft: The --soft option is simply used to move the HEAD to the particular commit by giving the commit id along with reset command.

Git Rebase and Merge

Git rebase is a linear process of merging. It combines a sequence of commits from distinct branches into a final commit. It merges the different commits one by one and the logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs. Let's assume we have made three commits in the master branch and three in the test branch. If we rebase it, then it will be merged in a linear manner.

Consider the below image:

Git Merge is always a forward changing record. The git merge command takes the data created by git branches and integrate them into a single branch while the logs of commits on branches remain intact. It combines a series of commits into one unified history. Let's assume we have made three commits in the master branch and three in the test. If we merge this, then it will merge all commits in a time.

Consider the below image:

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]

Steps to do the above task-

i) Initialize an empty repository inside Devops folder by using git init and check the status using git status to find the Branch you are currently in.

ii) Create a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. Now, stage the file using git add version01.txt and then commit the file along with the message using git commit -m "Added new feature".

iv) To push the local repository in GitHub, ccreate a new repsoitory in GitHub and copy its url and then add the copied remote url to the local using git remote add origin <url>. Check the remote url using git remote -v.

v) A Personal Access Token is required to connect to the remote repository. Concatenate the PAT with the remote url. To learn more about it, do read https://hashnode.com/post/clks82lqc000z0ajrd7z89tf8.

vi) Push the code using git push origin master. This will push the code to the newly created remote repository in GitHub.

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 the 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]

Steps to do the above task-

i) Firstly, we will be creating a new branch named dev and switch to the dev branch using git checkout -b dev

ii) Now, add the above message in version01.txt and commit one by one with the message provided above.

iii) Check the git log to view all the commits done till now.

iv) To restore the file to a previous version where the content should be “This is the bug fix in development branch”, use - git revert --hard <commitid>. All the commits in between the given commit are removed and the head points to the given commit id.

Task 2:

  • Demonstrate the concept of branches with 2 or more branches with a screenshot.

    We can see in the above examples, that the first commit we created was on master branch. After that a new branch was created named dev where all the multiple changes and revert were done.

  • add some changes to dev branch and merge that branch in master

    As we have already made the changes in dev branch for version01.txt file, let's merge the dev branch to master.

    To merge the dev branch first switch to master branch using git checkout master. Check on which branch are we currently in using git status. Now, use the git merge dev to merge the dev branch to master. Push the master branch to remote repository using git push origin master.

    We have successfully done the above task and learned about the Branching strategy, git revert, git reset, git merge and git rebase.

Thank you!

~Shilpi