Advance Git & GitHub for DevOps Engineers: Part-1

Photo by Michal Ico on Unsplash

Advance Git & GitHub for DevOps Engineers: Part-1

Git Branching

Branching in Git is like creating separate paths of development within a project. It lets users work on different things at the same time, like adding new features or fixing bugs, without affecting the main part of the project. Think of it like working on different versions of a document, where you can make changes to each version separately. It helps with developing features, fixing bugs, or experimenting with new features in your repository without affecting the Main or Master Branch.

Git Branches

Git Revert and Reset

Git Revert: It is a command used for version control to undo a previously committed change without erasing the previous commit. It is important to note that you don't want to erase any commits from the history as it could lead to problems if you are working in a collaborative environment.

Example of Git Revert

In the code example below, I created a dev branch from the Master branch. The dev branch has 3 feature files in it. In the file name "feature3.txt" some errors were added and committed. I then used the "git revert" command to go back to the previous "acceptable" commit. When the "git revert" command is entered, the Nano editor opens up and the user needs to confirm if okay to proceed with the revert changes.

git revert

ubuntu@ip-172-31-92-141:~/devops-batch-3$ git log --oneline
c583676 (HEAD -> dev) added bad changes
73e1175 added changes to feature 3 by developer 2
8f85e77 added new feature by developer
ac1eecb (master) added new feature
c28b1b3 added first feature
ubuntu@ip-172-31-92-141:~/devops-batch-3$ git revert c583676
[dev 59f7630] Revert "added bad changes"
 1 file changed, 2 deletions(-)
ubuntu@ip-172-31-92-141:~/devops-batch-3$ cat feature3.txt
this feature is from developer 1
developer 2 added his changes here
ubuntu@ip-172-31-92-141:~/devops-batch-3$

Git Reset: It is used to reset the current branch to a previous state undoing any changes made after the selected commit. It is useful if you made a mistake in your last commit or if you want to discard some changes. However, it is important to note that you could lose useful data when using the "git reset" command, therefore it's better to create a backup.

Example of Git Reset

In the code example below, as you can see by running the "git log --oneline" command it shows the commits. Currently, the latest commit ID is 59f7630 and I would like to reset this commit and go back to commit ID 8f85e77 making that the head. To do this, I ran the "git reset" command. This would then give the message stating that my file "feature3.txt" has been modified and it's "unstaged". This gives you the opportunity now to go into the file using the "vim" editor and fix your code. Next, I staged the file using the "git add" command and then used the "git commit" command to commit this file. Now if I run the "git log --oneline" command you can see that all changes after commit ID 8f85e77 have disappeared.

ubuntu@ip-172-31-92-141:~/devops-batch-3$ git log --oneline
59f7630 (HEAD -> dev) Revert "added bad changes"
c583676 added bad changes
73e1175 added changes to feature 3 by developer 2
8f85e77 added new feature by developer
ac1eecb (master) added new feature
c28b1b3 added first feature
ubuntu@ip-172-31-92-141:~/devops-batch-3$ git reset 8f85e77
Unstaged changes after reset:
M    feature3.txt
ubuntu@ip-172-31-92-141:~/devops-batch-3$ vim feature3.txt
ubuntu@ip-172-31-92-141:~/devops-batch-3$ git add feature3.txt
ubuntu@ip-172-31-92-141:~/devops-batch-3$ git commit -m "feature 3 file now has been fixed"
[dev 14ea635] feature 3 file now has been fixed
 1 file changed, 6 insertions(+), 1 deletion(-)
ubuntu@ip-172-31-92-141:~/devops-batch-3$ git log --oneline
14ea635 (HEAD -> dev) feature 3 file now has been fixed
8f85e77 added new feature by developer
ac1eecb (master) added new feature
c28b1b3 added first feature
ubuntu@ip-172-31-92-141:~/devops-batch-3$

Git Rebase and Merge

Git Rebase: It is taking the commits from one branch and moves them over to another specified branch. As an example, if you have 2 commits in the master branch and 2 commits in the dev branch, it's taking the 2 commits from the feature branch and moving them on top of the master branch - see the below images. This can be executed by using the "git rebase" command.

Git Merge: It is used to bring changes made in one branch of code into another branch. When you start working on a new feature or a bug fix in Git, you can create a new branch to work on it separately. When you're done with your changes and they are acceptable, you can use the "git merge" command to bring those changes into the master branch of the codebase.

Example of Git Merge

In the code example below, I created a dev branch and added a file there "simple_file.txt". I then staged & committed the file, switched over to the master branch and merged the dev branch with the master branch by using the "git merge" command.

sam@Sams-MacBook-Pro simple-git-tutorial % git checkout -b dev
Switched to a new branch 'dev'
sam@Sams-MacBook-Pro simple-git-tutorial % git branch
* dev
  master
sam@Sams-MacBook-Pro simple-git-tutorial % vim new_file.txt
sam@Sams-MacBook-Pro simple-git-tutorial % ls
new_file.txt    simple_file.txt
sam@Sams-MacBook-Pro simple-git-tutorial % git add new_file.txt 
sam@Sams-MacBook-Pro simple-git-tutorial % git commit -m "added new file"
[dev 59f4f26] added new file
 1 file changed, 1 insertion(+)
 create mode 100644 new_file.txt
sam@Sams-MacBook-Pro simple-git-tutorial % git status
On branch dev
nothing to commit, working tree clean
sam@Sams-MacBook-Pro simple-git-tutorial % git checkout master
Switched to branch 'master'
sam@Sams-MacBook-Pro simple-git-tutorial % git merge dev
Updating 7b05181..59f4f26
Fast-forward
 new_file.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 new_file.txt
sam@Sams-MacBook-Pro simple-git-tutorial % ls
new_file.txt    simple_file.txt
sam@Sams-MacBook-Pro simple-git-tutorial % git status
On branch master
nothing to commit, working tree clean
sam@Sams-MacBook-Pro simple-git-tutorial %

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

ubuntu@ip-172-31-92-141:~/devops/git$ vim version01.txt
ubuntu@ip-172-31-92-141:~/devops/git$ git status
fatal: not a git repository (or any of the parent directories): .git
ubuntu@ip-172-31-92-141:~/devops/git$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:     git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:     git branch -m <name>
Initialized empty Git repository in /home/ubuntu/devops/git/.git/
ubuntu@ip-172-31-92-141:~/devops/git$ ls
version01.txt
ubuntu@ip-172-31-92-141:~/devops/git$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    version01.txt

nothing added to commit but untracked files present (use "git add" to track)
ubuntu@ip-172-31-92-141:~/devops/git$ git add version01.txt
ubuntu@ip-172-31-92-141:~/devops/git$ git commit -m "This commit is for our 1st feature of our app"
[master (root-commit) f81d9f9] This commit is for our 1st feature of our app
 1 file changed, 3 insertions(+)
 create mode 100644 version01.txt
ubuntu@ip-172-31-92-141:~/devops/git$ git status
On branch master
nothing to commit, working tree clean
ubuntu@ip-172-31-92-141:~/devops/git$ git checkout -b dev
Switched to a new branch 'dev'
ubuntu@ip-172-31-92-141:~/devops/git$ ls
version01.txt
ubuntu@ip-172-31-92-141:~/devops/git$ git status
On branch dev
nothing to commit, working tree clean
ubuntu@ip-172-31-92-141:~/devops/git$ git log --oneline
f81d9f9 (HEAD -> dev, master) This commit is for our 1st feature of our app
ubuntu@ip-172-31-92-141:~/devops/git$

The file "version01.txt" should be reflected in a local report first and then followed by a remote repo for review. See the below in bash confirming that local has been pushed to the remote repo.

ubuntu@ip-172-31-92-141:~/devops/git$ git remote add origin https://github.com/samsamarullah/devops-git.git
ubuntu@ip-172-31-92-141:~/devops/git$ ls
version01.txt
ubuntu@ip-172-31-92-141:~/devops/git$ git push origin dev
Username for 'https://github.com': ssamarullah
Password for 'https://ssamarullah@github.com': 
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/samsamarullah/devops-git.git/'
ubuntu@ip-172-31-92-141:~/devops/git$ git push origin dev
Username for 'https://github.com': ssamarullah
Password for 'https://ssamarullah@github.com': 
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/samsamarullah/devops-git/pull/new/dev
remote: 
To https://github.com/samsamarullah/devops-git.git
 * [new branch]      dev -> dev
ubuntu@ip-172-31-92-141:~/devops/git$

Add new commits in the dev branch following the sub-tasks of Task 1 below in the "version01.txt" file:

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

    • Commit this with the message "Added feature 2 in the dev branch"
  • 2nd line -> This is an error code

    • Commit this with the message "Added feature 3 in the dev branch"
  • 3rd line -> This feature will now cause errors from now on

    • Commit this with the message "Added feature 4 in the dev branch"
    ubuntu@ip-172-31-92-141:~/devops/git$ ls
    version01.txt
    ubuntu@ip-172-31-92-141:~/devops/git$ git branch
    * dev
      master
    ubuntu@ip-172-31-92-141:~/devops/git$ vim version01.txt
    ubuntu@ip-172-31-92-141:~/devops/git$ git add version01.txt
    ubuntu@ip-172-31-92-141:~/devops/git$ git commit -m "Added feature 2 in the dev branch"
    [dev 22854af] Added feature 2 in the dev branch
     1 file changed, 2 insertions(+)
    ubuntu@ip-172-31-92-141:~/devops/git$ vim version01.txt
    ubuntu@ip-172-31-92-141:~/devops/git$ git add version01.txt
    ubuntu@ip-172-31-92-141:~/devops/git$ git commit -m "Added feature 3 in the dev branch"
    [dev a6dcbe5] Added feature 3 in the dev branch
     1 file changed, 2 insertions(+)
    ubuntu@ip-172-31-92-141:~/devops/git$ vim version01.txt
    ubuntu@ip-172-31-92-141:~/devops/git$ git add version01.txt
    ubuntu@ip-172-31-92-141:~/devops/git$ git commit -m "Added feature 4 in the dev branch"
    [dev 47301ff] Added feature 4 in the dev branch
     1 file changed, 2 insertions(+)
    ubuntu@ip-172-31-92-141:~/devops/git$ cat version01.txt
    This is the 1st feature of our application.

    This is the bug fix in the dev branch

    This is an error code

    This feature will now cause errors from now on

    ubuntu@ip-172-31-92-141:~/devops/git$

Now restore the file to a previous version where the content should be "This is the bug fix in the dev branch" (Feature 2). I used the "git revert" command for commit IDs 47301ff & a6dcbe5 See the below in bash.

ubuntu@ip-172-31-92-141:~/devops/git$ git log --oneline
47301ff (HEAD -> dev) Added feature 4 in the dev branch
a6dcbe5 Added feature 3 in the dev branch
22854af Added feature 2 in the dev branch
f81d9f9 (origin/dev, master) This commit is for our 1st feature of our app
ubuntu@ip-172-31-92-141:~/devops/git$ git revert 47301ff
[dev f634261] Revert "Added feature 4 in the dev branch"
 1 file changed, 2 deletions(-)
ubuntu@ip-172-31-92-141:~/devops/git$ git revert a6dcbe5
[dev eee105a] Revert "Added feature 3 in the dev branch"
 1 file changed, 2 deletions(-)
ubuntu@ip-172-31-92-141:~/devops/git$ cat version01.txt
This is the 1st feature of our application.

This is the bug fix in the dev branch

ubuntu@ip-172-31-92-141:~/devops/git$ git log --oneline
eee105a (HEAD -> dev) Revert "Added feature 3 in the dev branch"
f634261 Revert "Added feature 4 in the dev branch"
47301ff Added feature 4 in the dev branch
a6dcbe5 Added feature 3 in the dev branch
22854af Added feature 2 in the dev branch
f81d9f9 (origin/dev, master) This commit is for our 1st feature of our app
ubuntu@ip-172-31-92-141:~/devops/git$

Task 2: Demonstrate the concept of branches with 2 or more branches with a screenshot and add some changes to the dev branch (add another file - test.txt) and then merge the dev branch with the master branch.

ubuntu@ip-172-31-92-141:~/devops/git$ git branch
  dev
* master
ubuntu@ip-172-31-92-141:~/devops/git$ git checkout -b test_qa
Switched to a new branch 'test_qa'
ubuntu@ip-172-31-92-141:~/devops/git$ git checkout master
Switched to branch 'master'
ubuntu@ip-172-31-92-141:~/devops/git$ git checkout -b hotfix
Switched to a new branch 'hotfix'
ubuntu@ip-172-31-92-141:~/devops/git$ git checkout master
Switched to branch 'master'
ubuntu@ip-172-31-92-141:~/devops/git$ git branch
  dev
  hotfix
* master
  test_qa
ubuntu@ip-172-31-92-141:~/devops/git$ git checkout dev
Switched to branch 'dev'
ubuntu@ip-172-31-92-141:~/devops/git$ ls
version01.txt
ubuntu@ip-172-31-92-141:~/devops/git$ cat test.txt
cat: test.txt: No such file or directory
ubuntu@ip-172-31-92-141:~/devops/git$ git checkout master
Switched to branch 'master'
ubuntu@ip-172-31-92-141:~/devops/git$ git merge dev
Updating f81d9f9..eee105a
Fast-forward
 version01.txt | 2 ++
 1 file changed, 2 insertions(+)
ubuntu@ip-172-31-92-141:~/devops/git$ git log
commit eee105a8cee2e42ba23f4d8b0a65196bbd38c231 (HEAD -> master, dev)
Author: ssamarullah <ssamarullahdevops@gmail.com>
Date:   Sat Apr 29 05:17:38 2023 +0000

    Revert "Added feature 3 in the dev branch"

    This reverts commit a6dcbe5ed0974a96eaf7717c53621fc1808fb08d.

commit f634261aebd2e46a0a196f5978bee552a2c30786
Author: ssamarullah <ssamarullahdevops@gmail.com>
Date:   Sat Apr 29 05:17:20 2023 +0000

    Revert "Added feature 4 in the dev branch"

    This reverts commit 47301ff90be219f6a86a5b1165e21e1da618ab17.

commit 47301ff90be219f6a86a5b1165e21e1da618ab17
Author: ssamarullah <ssamarullahdevops@gmail.com>
Date:   Sat Apr 29 05:13:27 2023 +0000

    Added feature 4 in the dev branch

commit a6dcbe5ed0974a96eaf7717c53621fc1808fb08d
Author: ssamarullah <ssamarullahdevops@gmail.com>
Date:   Sat Apr 29 05:12:36 2023 +0000

    Added feature 3 in the dev branch

commit 22854af74f5c8c68c8e3761621c496e9ba3217a5
Author: ssamarullah <ssamarullahdevops@gmail.com>
Date:   Sat Apr 29 05:11:38 2023 +0000

    Added feature 2 in the dev branch

commit f81d9f925a4b0d8118c0c6d316052aa94aaaf43a (origin/dev, test_qa, hotfix)
Author: ssamarullah <ssamarullahdevops@gmail.com>
Date:   Sat Apr 29 04:50:27 2023 +0000

    This commit is for our 1st feature of our app
ubuntu@ip-172-31-92-141:~/devops/git$

I appreciate your busy time reading this short blog. As I continue with my journey to learn and acquire the skill set of a DevOps Engineer, I will share what I learn. Thank you.

Happy Learning!


Sam Samarullah

LinkedIn

Previous Blog

#devops #devopstools #devopsworld #git #github