Day 13: Advanced Git & GitHub for DevOps Engineers

Greetings! 👋 I'm Priyadarshi Ranjan, a dedicated DevOps Engineer embarking on an enriching journey. Join me as I delve into the dynamic realms of cloud computing and DevOps through insightful blogs and updates. 🛠️ My focus? Harnessing AWS services, optimizing CI/CD pipelines, and mastering infrastructure as code. Whether you're peers, interns, or curious learners, let's thrive together in the vibrant DevOps ecosystem. 🌐 Connect with me for engaging discussions, shared insights, and mutual growth opportunities. Let's embrace the learning curve and excel in the dynamic realm of AWS and DevOps technology!
Git Branching


Branches are a core concept in Git, enabling isolated development work without affecting the main codebase. Each repository has a default branch (often main or master), but you can create multiple branches for different tasks. This way, you can work on new features, bug fixes, or experimental changes without disrupting the main project.
Creating a Branch: Use the
git branchcommand to create a new branch. For instance, to create a branch namedfeature, you would run:git branch featureSwitching to a Branch: Use
git checkoutto switch between branches:git checkout featureCreating and Switching: Combine these actions with the
-bflag:git checkout -b featureMerging a Branch: When you are done with your changes, you can merge your branch back into the main branch. First, switch to the main branch:
git checkout masterThen, merge the feature branch:
git merge feature
Branches are useful for organizing work on different aspects of your project, making it easier to track changes and collaborate with others.
Git Revert and Reset

Both git revert and git reset are commands used to undo changes, but they function differently:
Git reset: This command moves the
HEADpointer to a specified commit, effectively changing the commit history. It has three modes:--soft: Keeps changes in the working directory and staging area.--mixed: Keeps changes in the working directory but clears the staging area.--hard: Discards all changes, both in the working directory and the staging area.
Example of a hard reset:
git reset --hard <commit_hash>
Git revert: This command creates a new commit that undoes the changes of a specified commit, preserving the commit history. It’s safer for collaborative environments since it doesn’t rewrite history.
Example:
git revert <commit_hash>
Git Rebase and Merge

Git Rebase and Git Merge are used to integrate changes from one branch to another:
Git Rebase: Rebase rewrites the commit history by moving the entire branch to a new base commit. It creates a linear history, which is cleaner and easier to understand.
git checkout feature git rebase masterThis command applies the changes from the
featurebranch on top of themasterbranch, making the history linear.Git Merge: Merge combines the changes from one branch into another by creating a new merge commit. It preserves the commit history of both branches.
git checkout master git merge featureWhile rebase results in a cleaner history, merge is better for preserving the context of feature development.
Tasks
Task 1: Feature Development with Branches
Create a Branch and Add a Feature:
Create a text file called
version01.txtinside theDevops/Git/directory with the content “This is the first feature of our application”.Create a new branch from
master:git checkout -b devAdd and commit the changes:
git add Devops/Git/version01.txt git commit -m "Added new feature"
Push Changes to GitHub:
Push the local commits to the repository on GitHub:
git push origin dev
Add More Features with Separate Commits:
Update
version01.txtwith new lines and commit after each change: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" 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:
Use git revert to undo the last two commits:
git revert HEAD~2
Task 2: Working with Branches
Demonstrate Branches:
Create multiple branches:
git branch feature1 git branch feature2Take screenshots of the branch structure:
git branch -a
Merge Changes into Master:
Make changes to the
devbranch and merge intomaster:git checkout dev echo "More changes" >> Devops/Git/version01.txt git commit -am "Added more changes in dev branch" git checkout master git merge dev
Practice Rebase:
Try rebasing
devontomasterand observe the changes:git checkout dev git rebase master
Conclusion
Mastering advanced Git and GitHub techniques is essential for effective DevOps practices. By leveraging branches, reverts, resets, rebases, and merges, you can manage code changes efficiently, collaborate seamlessly, and maintain a clean and organized project history. These skills are vital for any DevOps engineer aiming to streamline development workflows and ensure code quality.
Reference
For a visual guide, you can watch this YouTube video by Shubham Londhe Sir.
Connect and Follow Me on Socials




