Skip to main content

Command Palette

Search for a command to run...

๐ŸŒŸ Advanced Git & GitHub for DevOps Engineers

Branching, Merging, and Rebasing: Demystifying Git for All.

Published
โ€ข4 min readโ€ขView as Markdown
๐ŸŒŸ Advanced Git & GitHub for DevOps Engineers
A

๐Ÿ‘จโ€๐Ÿ’ป Last-year student diving deep into DevOps, Cloud Engineering, and Infrastructure Automation. Passionate about building scalable, efficient, and secure systems. Letโ€™s connect and build something amazing! ๐Ÿš€

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 called dev 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 file version01.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 your dev 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

  1. Git Revert: Safely undoes changes by creating a new commit. Great for collaborative projects.
    ๐Ÿ‘‰ Command:

     git revert HEAD
    
  2. 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 the master branch.

  • git merge dev: Merges the dev branch into master.

3. Rebase Your Branch

Rebase dev onto master to clean up the commit history.
๐Ÿ‘‰ Command:

git rebase master
  • git rebase master: Moves dev's commits to the end of master'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?

  1. Branches let you experiment without breaking the main project.

  2. Revert/Reset helps you fix mistakes easily.

  3. Rebase/Merge organizes your project history.

Git makes teamwork seamless and reduces risks during development. ๐Ÿš€