Advance Git & GitHub for DevOps Engineers
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:
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.
- Instead of deleting or moving commits like
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 Reset
git reset
is a powerful command used to move theHEAD
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:
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 intomaster
, keeping the logs from both branches.
git checkout master
git merge dev
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
Create a Branch and Add a Feature:
Create a new branch from
master
and swich todev
.git checkout -b dev
Add a text file called
version01.txt
inside theDevops/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"
Push Changes to GitHub:
- Push the new
dev
branch to GitHub.
- Push the new
git push origin 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
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"
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:
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
Practice Rebase:
- Rebase the
dev1
branch on top ofmaster
:
- Rebase the
git checkout dev1
git rebase master
# Resolve any conflicts, if necessary
git push origin dev1 --force
Keep practicing with Git.
Happy Learning:)
Subscribe to my newsletter
Read articles from Imran Shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by