Advance Git & GitHub for DevOps Engineers

Imran ShaikhImran Shaikh
5 min read

Mastering Git Branching, Reverting, Reseting, Merging and Rebasing for DevOps Engineers

In today’s fast-paced DevOps environment, mastering version control is crucial for seamless collaboration, automation, and continuous integration. At the heart of version control lies Git, an open-source system that revolutionized how teams manage and track code. Combined with GitHub, a platform built around Git, developers and operations engineers can streamline workflows, manage repositories, and foster team collaboration more efficiently.

Today, we’ll dive into some of the more advanced features of Git, including branching, reverting, resetting, rebasing, and merging. These concepts are key to working effectively in any development team.

Git Branching:

Branching is one of Git’s most powerful features. It allows you to create separate lines of development for working on different features, bug fixes, or experiments without affecting the main codebase. Once a branch's development is complete, you can merge it back into the main branch.

Why Branching is Important:

  • Parallel Development: Different features or bug fixes can be developed simultaneously in separate branches.

  • Isolation: Work on new features or fixes in isolation without affecting the main project code.

  • Experimentation: You can experiment in a branch and abandon it if it doesn't work, without affecting the stable code.

Create a New Branch

git branch <branch-name>

This creates a new branch

Git Revert and Git Reset:

These two commands let you undo changes in your project, but they work differently:

  1. Git Revert

    git revert is a safer way to undo changes by creating a new commit that undoes the effects of a specific commit. It doesn’t change the commit history, making it useful for undoing changes in a public or shared repository without rewriting history.

    How git revert Works:

    • Instead of deleting or moving commits like reset, git revert creates a new commit that reverses the changes introduced by a specified commit.

For example, if you made some bad changes in the last two commits and want to undo them, you could use git revert like this:

git revert HEAD~2

This command will undo the last two commits but add new ones to show that you reverted those changes.

  1. Git Reset

    git reset is a powerful command used to move the HEAD and current branch pointer to a specific commit, thereby undoing changes either locally or in the commit history. It essentially “resets” the state of your project to an earlier point in time.

    Use this when you want to remove recent work permanently from your project.

Git Rebase and Merge:

  1. Git Merge

    git merge combines the changes from two branches into one. It creates a new merge commit that brings together the histories of both branches. This is the traditional and most common way to integrate changes between branches.

    How Git Merge Works:

    When you merge two branches, Git creates a new commit (the merge commit) that ties the histories of the two branches together.

    This merges the dev branch into master, keeping the logs from both branches.

git checkout master
git merge dev
  1. Git Rebase

    git rebase moves or re-applies your branch’s changes onto another branch, creating a linear commit history. Instead of creating a merge commit, rebase rewrites the commit history as if the changes were made directly on top of the target branch.

    How Git Rebase Works:

    When you rebase a branch, Git takes all the commits from your branch and applies them on top of another branch.

git rebase master

This command applies your dev branch’s commits on top of the master branch.

Task:

Feature Development with Branches

  1. Create a Branch and Add a Feature:

    • Create a new branch from master and swich to dev.

        git checkout -b dev
      
    • Add a text file called version01.txt inside the Devops/Git/ directory with “This is the first feature of our application” written inside.

        echo "This is the first feature of our application" > Devops/Git/version01.txt
        git add Devops/Git/version01.txt
        git commit -m "Added new feature"
      
    • Commit your changes with a message reflecting the added feature.

        git add Devops/Git/version01.txt
        git commit -m "Added new feature"
      
  2. Push Changes to GitHub:

    • Push the new dev branch to GitHub.
        git push origin dev
  1. 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
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
git commit -am "Added feature2 in development branch"
  • 2nd line: This is gadbad code
echo "This is gadbad code" >> Devops/Git/version01.txt
git commit -am "Added feature3 in development branch"
  • 3rd line: This feature will gadbad everything from now
echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt
git commit -am "Added feature4 in development branch"
  1. 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”.
git revert HEAD~2

Working with Branches:

  1. Demonstrate Branches:

    • Create 2 or more branches and take screenshots to show the branch structure.
git checkout -b dev1
# Make changes and commit
git checkout -b dev2
# Make changes and commit

2. Merge Changes into Master:

Make some changes to the dev1 branch and merge it into master

git checkout master
git merge dev1
  1. Practice Rebase:

    • Rebase the dev1 branch on top of master:
git checkout dev1
git rebase master
# Resolve any conflicts, if necessary
git push origin dev1 --force

Keep practicing with Git.

Happy Learning:)

0
Subscribe to my newsletter

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

Written by

Imran Shaikh
Imran Shaikh