Day 10 - Advance Git & GitHub for DevOps Engineers.
Introduction
Git is an essential tool for DevOps engineers, enabling efficient source code management and collaboration. Understanding advanced Git commands and workflows can significantly improve your development process. In this blog, we'll explore Git branching, reverting, resetting, rebasing, and merging.
Branching
Branches in Git allow you to isolate development work without affecting other branches. This isolation is critical for developing new features, fixing bugs, or experimenting with new ideas.
Creating a Branch:
git checkout -b <branch-name>
This command creates a new branch and switches to it.
Committing Changes:
git add <file> git commit -m "Commit message"
This sequence stages your changes and commits them to the current branch.
Pushing a Branch to Remote:
git push -u origin <branch-name>
This command pushes your branch to the remote repository.
Reverting and Resetting
Git revert and reset are tools for undoing changes.
Reverting a Commit:
git revert <commit-hash>
This command creates a new commit that undoes the changes from a specified commit.
Resetting to a Previous Commit:
git reset --hard <commit-hash>
This command resets the current branch to a specified commit, discarding all changes after that commit.
Rebasing and Merging
Rebase and merge are two methods for integrating changes from one branch into another.
Merging:
git merge <branch-name>
This command integrates changes from the specified branch into the current branch, preserving the commit history.
Rebasing:
git rebase <branch-name>
This command applies your branch's commits on top of another branch, creating a linear commit history.
Task 1: Branching, Committing, and Reverting
Step 1: Creating a New Branch and Adding a Feature
Clone the Repository (if not already done):
git clone <repository-url> cd <repository-name>
Create and Switch to a New Branch:
git checkout -b dev
Add a New Feature:
mkdir -p Devops/Git echo "This is first feature of our application" > Devops/Git/version01.txt git add Devops/Git/version01.txt git commit -m "Added new feature"
Push the New Branch to Remote:
git push -u origin dev
Step 2: Adding More Features with Multiple Commits
Edit the
version01.txt
and add new content: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"
Step 3: Reverting to a Previous Commit
To revert the file version01.txt
to a state where it only contains the line "This is the bug fix in development branch", we can use git revert
.
Identify the Commit to Revert To:
git log
Find the commit hash for the commit with the message "Added feature2 in development branch".
Revert to the Specific Commit:
git revert <commit-hash> --no-commit git commit -m "Reverted to the bug fix in development branch"
Task 2: Demonstrating Branching, Merging, and Rebasing
Step 1: Creating Additional Branches
Create Additional Branches:
git checkout -b feature-branch-1 echo "Feature 1" > feature1.txt git add feature1.txt git commit -m "Added feature1" git checkout dev git checkout -b feature-branch-2 echo "Feature 2" > feature2.txt git add feature2.txt git commit -m "Added feature2"
Push the Branches to Remote:
git push -u origin feature-branch-1 git push -u origin feature-branch-2
Step 2: Merging Branches
Merge
feature-branch-1
intodev
:git checkout dev git merge feature-branch-1 -m "Merged feature-branch-1 into dev"
Push the Merged
dev
Branch:git push origin dev
Merge
dev
intomaster
:git checkout master git merge dev -m "Merged dev into master" git push origin master
Step 3: Rebasing Branches
Rebase
dev
ontomaster
:git checkout dev git rebase master
Resolve Any Conflicts and Continue Rebase: Follow the prompts to resolve any conflicts, then:
git rebase --continue
Conclusion
Mastering these advanced Git commands and workflows will make you a more effective DevOps engineer, enabling smoother collaboration and more efficient code management. Whether you're developing new features, fixing bugs, or experimenting with new ideas, these tools will help you maintain a clean and manageable project history.
Thank you for reading our DevOps blog post. We hope you found it informative and helpful. If you have any questions or feedback, please don't hesitate to contact us.
I hope this helps!
Happy Learningโจ
Subscribe to my newsletter
Read articles from Rahul Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rahul Gupta
Rahul Gupta
Hey there! ๐ I'm Rahul Gupta, a DevOps Engineer passionate about all things AWS DevOps Technology. Currently, on a learning adventure, I'm here to share my journey and Blogs in the world of cloud and DevOps. ๐ ๏ธ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space. ๐ Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!