Advance Git & GitHub for DevOps Engineers #Day-13
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 branch
command to create a new branch. For instance, to create a branch namedfeature
, you would run:git branch feature
Switching to a Branch: Use
git checkout
to switch between branches:git checkout feature
Creating and Switching: Combine these actions with the
-b
flag:git checkout -b feature
Merging 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 master
Then, 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
HEAD
pointer 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 master
This command applies the changes from the
feature
branch on top of themaster
branch, 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 feature
While 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:
Add a text file called
version01.txt
inside theDevops/Git/
directory with “This is the first feature of our application” written inside.Create a new branch from
master
.Commit your changes with a message reflecting the added feature
git checkout -b dev
Push Changes to GitHub:
Push your local commits to the repository on GitHub.
git push origin dev
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.
Connect and Follow Me On Socials :
Like ❤ | Share 📲 | Comment 💭
Subscribe to my newsletter
Read articles from Nikunj Vaishnav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nikunj Vaishnav
Nikunj Vaishnav
👋 Hi there! I'm Nikunj Vaishnav, a passionate QA engineer Cloud, and DevOps. I thrive on exploring new technologies and sharing my journey through code. From designing cloud infrastructures to ensuring software quality, I'm deeply involved in CI/CD pipelines, automated testing, and containerization with Docker. I'm always eager to grow in the ever-evolving fields of Software Testing, Cloud and DevOps. My goal is to simplify complex concepts, offer practical tips on automation and testing, and inspire others in the tech community. Let's connect, learn, and build high-quality software together! 📝 Check out my blog for tutorials and insights on cloud infrastructure, QA best practices, and DevOps. Feel free to reach out – I’m always open to discussions, collaborations, and feedback!