Day 13 Advanced Git & GitHub for DevOps Engineers
Git is a powerful version control system that helps developers manage their code effectively. This guide will walk you through key concepts like branching, reverting, resetting, merging, and rebasing in Git. Each concept will be explained, followed by practical tasks to help you apply what you've learned.
1. What is Git Branching?
Branches in Git are used to develop features, fix bugs, or experiment with new ideas without affecting the main codebase.
Main Branch: The default branch where the stable code resides, often named
main
ormaster
.Feature Branch: A new branch created for a specific feature or fix.
How to Create a Branch and Add a Feature
Create a New Branch: Start by switching to the main branch.
git checkout main
Create a New Branch: Make a new branch for your feature.
git checkout -b dev
- This creates and switches you to the
dev
branch.
- This creates and switches you to the
Add a Feature: Create a new text file with some content.
echo "This is the first feature of our application" > Devops/Git/version01.txt
Stage Your Changes: Add the file to the staging area.
git add Devops/Git/version01.txt
Commit Your Changes: Save your changes with a descriptive message.
git commit -m "Added new feature"
Push Your Changes to GitHub: Send your changes to the remote repository.
git push origin dev
2. Adding More Features with Separate Commits
You can continue to add features or make changes by following these steps:
Update Your File: Add a line about a bug fix.
echo "This is the bug fix in the development branch" >> Devops/Git/version01.txt git commit -am "Added feature2 in development branch"
Add More Lines:
Add another line (gadbad code):
echo "This is gadbad code" >> Devops/Git/version01.txt git commit -am "Added feature3 in development branch"
Add one more line (this feature will gadbad everything):
echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt git commit -am "Added feature4 in development branch"
3. Restoring the File to a Previous Version
If you want to revert changes made in the last few commits:
Revert Commits: This will remove the last two commits from the history.
git revert HEAD~2
- The file will now contain only the first feature and the bug fix.
4. Working with Branches
To manage multiple features or fixes:
Create Additional Branches:
- Create another branch for a different feature:
git checkout -b feature2
Visualize Your Branch Structure:
- To see your branches and their structure, use:
git branch
5. Merging Changes into Master
To merge your changes back into the main branch:
Switch to the Main Branch:
git checkout main
Merge the Development Branch:
git merge dev
6. Understanding Git Revert vs. Git Reset
Git Revert: Creates a new commit that undoes changes from previous commits without altering commit history.
Git Reset: Moves the HEAD pointer to a specified commit and can affect the working directory.
7. Understanding Git Merge vs. Git Rebase
Git Merge: Combines changes from one branch into another, preserving the history of both branches.
Git Rebase: Moves or combines commits from one branch onto another branch, resulting in a linear commit history.
8. Practicing Rebase
To practice rebasing:
Rebase Your Development Branch onto Master:
git checkout dev git rebase main
- This replays the commits from the
dev
branch on top of themain
branch.
- This replays the commits from the
Conclusion
Git's branching, reverting, resetting, merging, and rebasing features provide powerful tools for managing your code. By following this guide and practicing the tasks, you can enhance your Git skills and collaborate more effectively with others.
Subscribe to my newsletter
Read articles from Fauzeya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Fauzeya
Fauzeya
Hi there! I'm Fauzeya 👩💻, a passionate DevOps Engineer with a background in Computer Science Engineering🎓. I’m committed to enhancing security🔒, efficiency⚙️, and effectiveness in software development and deployment processes. With extensive knowledge in cloud computing☁️, containerization📦, and automation🤖, I aim to stay updated with the latest tools and methodologies in the DevOps field. Currently, I’m on a journey to deepen my understanding of DevOps I enjoy sharing my learning experiences and insights through my blog, 📝where I cover topics related to DevOps practices, tutorials, and challenges. I believe in continuous growth and learning and am excited to connect with fellow tech enthusiasts and professionals🤝. Let’s embark on this journey together!🚀