Day 13 Task : Advanced Git & GitHub for DevOps Engineers

Faizan ShaikhFaizan Shaikh
6 min read

Mastering Git Branching, Reverting, and Rebasing for DevOps Engineers

Git is an essential tool for developers and DevOps engineers alike. It helps you manage your codebase efficiently, collaborate with your team, and keep track of all changes made to a project. 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.


Understanding Git Branching

Branches in Git allow you to create separate environments for development without affecting the main codebase (often called the master branch). This way, you can add new features, experiment, or fix bugs without disturbing the main version of your project.

Think of branches as separate workspaces:

  • Master branch: The official version of your project, typically used for production.

  • Development branch: A place where you develop new features or fix bugs.

Creating a new branch is simple:

git checkout -b dev

This command creates a new branch called dev and switches to it. Now, you can make changes and commit them without affecting the master branch.

Git Revert and Git Reset

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

  1. Git Reset: Moves your project back in time, erasing commits and changes. Use this when you want to remove recent work permanently from your project. Be cautious, especially when working with a team, as it affects the commit history.

  2. Git Revert: Creates a new commit that undoes the changes from a previous commit, keeping your project history intact. This is the safer option when you need to undo a mistake but want to maintain a clear record of what happened.

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.

Git Rebase vs. Git Merge

Both rebase and merge allow you to combine changes from one branch into another, but they handle the history differently.

Git Rebase

Rebase re-applies your changes from one branch on top of another, creating a clean, linear history. This makes the commit history look as if you developed everything on one branch from the start.

Use rebase if you want to keep your history tidy:

git rebase master

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

Git Merge

Merge combines changes from two branches but keeps the entire commit history intact, including where the branches diverged. This is great if you want to see a full record of development, but it can make the commit history look messier.

Use merge when you want to combine features while keeping track of exactly when they were developed:

git checkout master
git merge dev

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


Practical Example: Working with Branches and Features

Let’s walk through a simple scenario where you’ll create a new feature, commit changes, and then undo a mistake.

  1. Create a Branch and Add a Feature:

    • First, create a new branch for your feature:
    git checkout -b dev
  • Then, add a new file called version01.txt with some initial content:
    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"
  1. Push Your Changes to GitHub:

    • Push the branch to GitHub so it’s available to your team:
    git push origin dev
  1. Add More Features with Separate Commits:

    • Keep adding new features and committing them:
    echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
    git commit -am "Added feature2 in development branch"

    echo "This is gadbad code" >> Devops/Git/version01.txt
    git commit -am "Added feature3 in development branch"

You’ve now made a mistake (the "gadbad" code), so let’s revert it.

  1. Revert to Fix the Mistake:

    • Use the revert command to undo the last two commits:
    git revert HEAD~2

This safely undoes the last two changes while keeping a record of them in your project’s history.


Merging and Rebasing Your Work

Now, after making all your changes on the dev branch, it’s time to merge them back into master:

  1. Merging:

    • First, switch back to the master branch:
    git checkout master
  • Then, merge the changes from dev into master:
    git merge dev
  1. Rebasing:

    • If you’d rather keep a clean, linear history, you can rebase your branch instead:
    git rebase master
  • This applies the changes from dev onto master as if they were developed sequentially.

Tasks:

Task 1: Feature Development with Branches

  1. Create a Branch and Add a Feature:

    • Create and switch to a new branch dev.
    git checkout -b dev
  • Add the file version01.txt under the Devops/Git/ directory and write the text This is the first feature of our application 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"
  1. Push Changes to GitHub:

    • Push the new dev branch to GitHub.
    git push origin dev
  1. Add More Features with Separate Commits:

    • Modify the file version01.txt step-by-step and commit each change:

1st Change:

    echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
    git commit -am "Added feature2 in development branch"

2nd Change (Buggy Code):

    echo "This is gadbad code" >> Devops/Git/version01.txt
    git commit -am "Added feature3 in development branch"

3rd Change (More Bugs):

    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 the last two buggy commits, effectively returning the file to the second feature’s content:
    git revert HEAD~2

Task 2: Working with Branches

  1. Demonstrate Branches:

    • Create two additional branches (e.g., feature1 and feature2), make a few commits in each, and take screenshots to show the branch structure.
    git checkout -b feature1
    # Make changes and commit
    git checkout -b feature2
    # Make changes and commit
  1. Merge Changes into Master:

    • Merge the dev branch into master:
    git checkout master
    git merge dev
    git push origin master
  1. Practice Rebase:

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

Key Takeaways

  • Branches are your friend when developing new features or fixing bugs without disturbing your main project.

  • Use git revert for safely undoing changes, especially in shared repositories. git reset can be powerful, but use it carefully as it alters the history.

  • Git merge is ideal for maintaining a full record of all development activity, while git rebase keeps your project history clean and linear.

Mastering these tools will help you become more efficient and confident in your development workflows. Plus, following Git best practices ensures that your project remains organized and easy to collaborate on.

Keep practicing with Git by trying out branching, merging, and rebasing. The more you work with Git, the more natural it will become!

#HappyLearning😇

0
Subscribe to my newsletter

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

Written by

Faizan Shaikh
Faizan Shaikh