π Advanced Git & GitHub for DevOps Engineers
Branching, Merging, and Rebasing: Demystifying Git for All.
Git can feel like magic, but itβs really just a set of tools that help you collaborate, experiment, and recover from mistakes with ease. Letβs break it all down in a fun and simple way, and weβll explain the commands step-by-step too!
πΏ Git Branching: Your Playground for Experiments
Think of branches as separate workspaces for your ideas. They let you work on new features, fix bugs, or test experiments without touching the main project. When youβre done, you can merge your branch into the main one.
Hereβs why branches are awesome:
π‘οΈ They keep your main branch safe from unfinished work.
π€ Multiple people can work on different branches simultaneously.
π You can merge branches when features are ready.
π Task 1: Feature Development with Branches
1. Create a New Branch and Add a Feature
π Command:
git checkout -b dev
git checkout -b dev
: Creates a new branch calleddev
and switches to it.
Now, letβs add a file and commit our changes:
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"
echo
: Adds text to the fileversion01.txt
.git add
: Stages the file, meaning Git is now tracking this change.git commit -m
: Saves the change with a message (-m
) describing what you did.
2. Push Your Branch to GitHub
π Command:
git push origin dev
git push origin dev
: Uploads yourdev
branch to the remote GitHub repository.
3. Add More Features with Separate Commits
Letβs simulate more updates to the file:
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
git commit -am "Added bug fix in development branch"
>>
: Appends text to the file without overwriting it.git commit -am
: Stages (-a
) and commits the change in one step.
Repeat this for more lines:
echo "This is gadbad code" >> Devops/Git/version01.txt
git commit -am "Added gadbad code in development branch"
echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt
git commit -am "Added problematic feature"
4. Revert to a Clean Version
Oops! We messed up. Letβs roll back the last two changes:
π Command:
git revert HEAD~2
git revert HEAD~2
: Creates a new commit that undoes the last two commits (HEAD
refers to the latest commit).
π Git Reset vs Git Revert
Git Revert: Safely undoes changes by creating a new commit. Great for collaborative projects.
π Command:git revert HEAD
Git Reset: Completely erases changes, even from history. Be careful!
π Command:git reset --hard HEAD~1
--hard
: Removes changes from both the history and the working directory.
π Task 2: Playing with Branches
1. Check Branches
List all branches in your repository.
π Command:
git branch
- The branch with
*
is your current branch.
Create two new branches:
π Command:
git branch feature1
git branch feature2
2. Merge Branches
Merge changes from dev
into master
.
π Command:
git checkout master
git merge dev
git checkout master
: Switches to themaster
branch.git merge dev
: Merges thedev
branch intomaster
.
3. Rebase Your Branch
Rebase dev
onto master
to clean up the commit history.
π Command:
git rebase master
git rebase master
: Movesdev
's commits to the end ofmaster
's history, creating a linear timeline.
π Rebase vs Merge
Merge: Combines branches while preserving the full history (shows all commits).
π Command:git merge dev
Rebase: Integrates changes while rewriting the commit history (creates a cleaner timeline).
π Command:git rebase master
π― Why Is This Important for DevOps?
Branches let you experiment without breaking the main project.
Revert/Reset helps you fix mistakes easily.
Rebase/Merge organizes your project history.
Git makes teamwork seamless and reduces risks during development. π