Mastering Git and GitHub: Advanced Strategies for DevOps Engineers Part 1
Day 10: Advanced Git & GitHub for DevOps Engineers
Introduction
Git is an effective version control system that is essential to the DevOps process. DevOps engineers need to grasp advanced Git concepts like branching, reverting, resetting, rebasing, and merging to manage code repositories efficiently. We will explore these subjects in-depth along with thorough solutions to typical problems in this blog post.
Git Branching: Diverging Paths
The branching system of Git is one of its most potent features. Multiple concurrent streams of development can coexist thanks to branching. Teams can work on different features or fixes concurrently without affecting each other's work because each branch represents a separate version of the code.
Creating a Branch
To create a new branch, simply use the command git branch branch_name
. This creates a new pointer to the same commit as the branch you're currently on.
Switching Between Branches
You can switch between branches using git checkout branch_name
. This updates your working directory to reflect the selected branch's state.
Git Revert and Reset: Correcting Mistakes
Git provides tools like revert
and reset
for undoing changes. They serve different purposes and should be used based on the specific requirements of your workflow.
Git Revert
git revert
creates a new commit that undoes the changes made in a previous commit. This is a safe way to correct mistakes without altering the commit history.
Git Reset
git reset
allows you to reset your branch to a specific commit, effectively erasing any commits made after it. This can be useful in situations where you want to rework or discard a series of commits.
Git Rebase and Merge: Integrating Changes
Integrating changes from one branch into another is a crucial part of collaborative development. Two common methods for this are rebase
and merge
.
Git Rebase
git rebase
allows you to incorporate the changes from one branch onto another by moving, combining, or omitting commits. This creates a linear history, making it easier to follow the progression of the codebase.
Git Merge
git merge
combines the changes from one branch into another, creating a new commit that represents the integration. This maintains a more branching history, showing the distinct lines of development.
Hands-On Practice
Task 1: Adding a Text File and Making Commits
Step 1: Create a New Branch and Add version01.txt
Open your terminal and navigate to the Devops/Git directory. Then, execute the following commands:
cd Devops/Git/
git checkout -b dev
echo "This is first feature of our application" > version01.txt
Step 2: Committing Changes in dev Branch
git add version01.txt
git commit -m "Added new feature"
Step 3: Pushing Changes to Remote Repository
git push origin dev
Step 4: Adding Additional Commits in dev Branch
echo "This is the bug fix in development branch" >> version01.txt
git add version01.txt
git commit -m "Added feature2 in development branch"
echo "This is gadbad code" >> version01.txt
git add version01.txt
git commit -m "Added feature3 in development branch"
echo "This feature will gadbad everything from now." >> version01.txt
git add version01.txt
git commit -m "Added feature4 in development branch"
Step 5: Restoring version01.txt to a Previous Version
You can use either git revert
or git reset
to achieve this. Let's use git reset
in this example.
git log
# Note down the commit hash of the version you want to restore to
git reset --hard <commit-hash>
git push -f origin dev
Task 2: Branching, Merging, and Rebase
Step 1: Creating Additional Branches
git checkout -b feature1
# Make changes and commit
git checkout -b feature2
# Make changes and commit
Step 2: Merging dev Branch into master
git checkout master
git merge dev
git push origin master
Step 3: Using Git Rebase
git checkout feature1
git rebase dev
# Resolve any conflicts, if there are any
git checkout feature2
git rebase dev
# Resolve any conflicts, if there are any
Conclusion
Developers may effectively manage their codebase by knowing these Git commands. Branching allows for simultaneous development, error correction through revert and reset, and integration support through rebase and merge. You can make sure that the development process runs smoothly and cooperatively by understanding when and how to use these tools.
Happy Learning!