Advance Git & GitHub for DevOps Engineers - Part 1

Advance Git & GitHub for DevOps Engineers - Part 1

#90DaysofDevOps Challenge - Day 10

Table of contents

No heading

No headings in the article.

▶What is Git Branching?

Git branching allows Developers/DevOps to diverge from the production version of code to fix a bug or add a feature. They create branches to work with a copy of the code without modifying the existing version. So, you create branches to isolate your code changes, which you test before merging to the main branch (more on this later).

A Step by Step Guide to Git Branches - DEV Community

▶ What is Git Revert and Reset?

Git revert is similar to git reset**,* but the approach is slightly different. Instead of removing all the commits in its way, the revert ONLY undoes a single commit by taking you back to the staged files before the commit*.

▶What is the difference between Git Rebase and Merge?

The rebase flattens the history, removing unwanted entries**. *Git** merge, on the other hand, only changes the target branch and creates a commit, preserving the history of the source branch.*

▶Below are some tasks performed for practising:

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 main, [hint try git checkout -b dev], switch to dev branch ( Make sure your commit message will reflect as "Added new feature"). [Hint uses your knowledge of creating branches and Git commit command].

$ mkdir -p Devops/git
$ cd Devops/git
$ git init
$ vi version01.txt --> Adding "This is first feature of our app"
$ git checkout -b dev
$ git add .
$ git commit -m "This is the first feature"
$ git log

The version01.txt file should reflect at the local repo first followed by the Remote repo for review.

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 and repeat the following steps in sequence.

1st line>> This is the bug fix in dev branch

$ vi version01.txt --> "This is the bug fix in dev branch" 
$ git add .
$ git commit -m "Added feature2 in dev branch"
$ git status

2nd line>> This is gadbad code

$ vi version01.txt --> "This is gadbad code" 
$ git add .
$ git commit -m "Added feature3 in dev branch"
$ git status

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

$ vi version01.txt --> "This feature will gadbad everything from now" 
$ git add .
$ git commit -m "Added feature4 in dev branch"
$ git status
$ git log

Now, we must reset the file to a previous version where the content should be

“This is the bug fix in dev branch”

$ git reset <commit_ID>
$ git status
$ git add .
$ git commit -m "Final reset"
$ git log

Task 2

Demonstrate the concept of branches with 2 or more branches.

Let's create two new branches called Test1 and Test2 with the git branch command.

$ git branch <name>
$ git branch

Now switch to Test1 branch with git checkoutcommand.

$ git checkout Test1
$ git branch

Now we need to add some content in the dev branch and merge it into the main branch.

$ git checkout dev
$ vi testfile3.txt
$ git add testfile3.txt
$ git commit -m "testfile3 merge with main"
$ git checkout main
$ git merge dev

Now we can verify with the ls -a command the same contents are there in the main and dev branches.

This is a nutshell explanation of some topics from Git and GitHub and demonstrates some of their commands hands-on.