Day 13 Task: Advance Git & GitHub for DevOps Engineers

Pooja BhavaniPooja Bhavani
5 min read

Git Branching

Branches are a core concept in Git that allow you to isolate development work without affecting other parts of your repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.

Branches let you develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.

12 Branching (git branch) | Git & GitHub Tutorial for Scientists: It's Not  Only for Programmers

Git Revert and Reset

Git revert is used to undo the changes introduced by a specific commit by creating a new commit with the opposite changes.

linux$ git revert <commit-hash>

Git reset is used to undo changes by moving the current branch pointer to a different commit.

There are types of reset,

  • Soft Reset (--soft): Moves the branch pointer to a different commit but leaves your working directory and the index (staging area) unchanged.
linux$ git reset --soft <commit-hash>
  • Hard Reset (--hard): Moves the branch pointer to a different commit and resets both the index and working directory to match that commit.
linux$ git reset --hard <commit-hash>

Git Rebase and Merge

What Is Git Rebase?

Git rebase is a command that lets users integrate changes from one branch to another, and the commit history is modified once the action is complete. Git rebase helps keep a clean project history.

linux$ git checkout feature-branch
linux$ git rebase master

What Is Git Merge?

Git merge is a command that allows developers to merge Git branches while keeping the logs of commits on branches intact. It combines changes from one branch into another. Even though merging and rebasing do similar things, they handle commit logs differently.

linux$ git checkout master # Switched to master branch
linux$ git merge feature-branch # Trying to merge branch feature-branch to main

Tasks

Task 1: Feature Development with Branches

  1. Create a Branch and Add a Feature:

    • Add a text file called version01.txt inside the Devops/Git/ directory with “This is the first feature of our application” written inside.
linux/Devops/Git/Adv_git$ ls
version01.txt
linux/Devops/Git/Adv_git$ cat version01.txt
This is the first feature of our application.
  • Create a new branch from "master".
linux/Devops/Git/Adv_git$ git branch dev
linux/Devops/Git/Adv_git$ git branch
  dev
* master
  • Commit your changes with a message reflecting the added feature.
linux$ git checkout dev
D       README.md
Switched to branch 'dev'

linux$ git add version01.txt
linux$ git commit -m "Added new feature"

Push Changes to GitHub:

  • Push your local commits to the repository on GitHub.
/linux/Devops/Git/Adv_git$ git status
On branch dev
nothing to commit, working tree clean

linux/Devops/Git/Adv_git$ git push origin dev
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 300 bytes | 150.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/ojasjawale/Adv_git/pull/new/dev
remote:
To github.com:ojasjawale/Adv_git.git
 * [new branch]      dev -> dev

Add More Features with Separate Commits:

  • Update version01.txt with the following lines, committing after each change:

    • 1st line: This is the bug fix in development branch
linux/Devops/Git/Adv_git$ git commit -am "Added feature2 in development branch"
[dev 4cf98e2] Added feature2 in development branch
 1 file changed, 2 insertions(+)

linux/Devops/Git/Adv_git$ git log --oneline
4cf98e2 (HEAD -> dev) Added feature2 in development branch
63c03df (origin/dev) Added new feature
9f59996 (origin/master, origin/HEAD, master) Initial commit
  • 2nd line: This is Gadbad code.
linux/Devops/Git/Adv_git$ echo "This is gadbad code" >> version01.txt

linux/Devops/Git/Adv_git$ git commit -am "Added feature3 in development branch"
[dev 04600e2] Added feature3 in development branch
 1 file changed, 2 insertions(+)

linux/Devops/Git/Adv_git$ git log --oneline
04600e2 (HEAD -> dev) Added feature3 in development branch
4cf98e2 Added feature2 in development branch
63c03df (origin/dev) Added new feature
9f59996 (origin/master, origin/HEAD, master) Initial commit
  • 3rd line: This feature will Gadbad everything from now.
linux/Devops/Git/Adv_git$ echo "This feature will gadbad everything from now" >> version01.txt       

linux/Devops/Git/Adv_git$ git commit -am "Added feature4 in development branch"
[dev 5dc8e94] Added feature4 in development branch
 1 file changed, 1 insertion(+)

linux/Devops/Git/Adv_git$ git log --oneline
5dc8e94 (HEAD -> dev) Added feature4 in development branch
04600e2 Added feature3 in development branch
4cf98e2 Added feature2 in development branch
63c03df (origin/dev) Added new feature
9f59996 (origin/master, origin/HEAD, master) Initial commit

Restore the File to a Previous Version:

  • Revert or reset the file to where the content should be “This is the bug fix in development branch”.
linux/Devops/Git/Adv_git$ cat version01.txt
This is the first feature of our application.

This is the bug fix in development branch
This is gadbad code
This feature will gadbad everything from now
linux$ git revert HEAD~2

linux/Devops/Git/Adv_git$ cat version01.txt
This is the bug fix in development branch
  1. Task 2 : Working with Branches

    Demonstrate Branches

  • Create 2 or more branches and take screenshots to show the branch structure.
linux$ git branch stg
linux$ git branch prd
linux$ git branch
* dev
  master
  prd
  stg

Merge Changes into Master:

  • Make some changes to the dev branch and merge it into master
linux/Devops/Git/Adv_git$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

linux/Devops/Git/Adv_git$ git merge dev
Updating 9f59996..6f3f6fe
Fast-forward
 README.md     | 1 -
 version01.txt | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
 delete mode 100644 README.md
 create mode 100644 version01.txt
0
Subscribe to my newsletter

Read articles from Pooja Bhavani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pooja Bhavani
Pooja Bhavani