Advanced Git & GitHub for DevOps Engineers
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:
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.
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.
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"
Push Your Changes to GitHub:
- Push the branch to GitHub so it’s available to your team:
git push origin dev
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.
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
:
Merging:
- First, switch back to the
master
branch:
- First, switch back to the
git checkout master
- Then, merge the changes from
dev
intomaster
:
git merge dev
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
ontomaster
as if they were developed sequentially.
Tasks:
Task 1: Feature Development with Branches
Create a Branch and Add a Feature:
- Create and switch to a new branch
dev
.
- Create and switch to a new branch
git checkout -b dev
- Add the file
version01.txt
under theDevops/Git/
directory and write the textThis 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"
Push Changes to GitHub:
- Push the new
dev
branch to GitHub.
- Push the new
git push origin dev
Add More Features with Separate Commits:
- Modify the file
version01.txt
step-by-step and commit each change:
- Modify the file
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"
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
Demonstrate Branches:
- Create two additional branches (e.g.,
feature1
andfeature2
), make a few commits in each, and take screenshots to show the branch structure.
- Create two additional branches (e.g.,
git checkout -b feature1
# Make changes and commit
git checkout -b feature2
# Make changes and commit
Merge Changes into Master:
- Merge the
dev
branch intomaster
:
- Merge the
git checkout master
git merge dev
git push origin master
Practice Rebase:
- Rebase the
dev
branch on top ofmaster
:
- Rebase the
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😇
Subscribe to my newsletter
Read articles from Ammar Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by