Advance Git & GitHub for DevOps Engineers
Git Branching
Branches are a core concept in Git that allow you to isolate development work without affecting other parts of your repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.
Branches let you develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.
Git Revert and Reset
Git reset and git revert are two commonly used commands that allow you to remove or edit changes you’ve made in the code in previous commits. Both commands can be very useful in different scenarios.
Git Rebase and Merge
What Is Git Rebase?
Git rebase is a command that lets users integrate changes from one branch to another, and the commit history is modified once the action is complete. Git rebase helps keep a clean project history.
What Is Git Merge?
Git merge is a command that allows developers to merge Git branches while keeping the logs of commits on branches intact. Even though merging and rebasing do similar things, they handle commit logs differently.
For a better understanding of Git Rebase and Merge, check out this article.
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
:git checkout -b dev
Commit your changes with a descriptive message:
git add Devops/Git/version01.txt git commit -m "Added new feature"
Push Changes to GitHub
Push your local commits to the GitHub repository:
git push origin dev
Add More Features with Separate Commits
Update
version01.txt
with additional lines, committing after each change:1st line:
This is the bug fix in development branch
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt git commit -am "Added feature2 in development branch"
2nd line:
This is gadbad code
echo "This is gadbad code" >> Devops/Git/version01.txt git commit -am "Added feature3 in development branch"
3rd line:
This feature will gadbad everything from now
echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt git commit -am "Added feature4 in development branch"
Verify the content of
version01.txt
after the updates.
Restore the File to a Previous Version
Revert the file to where the content should be “This is the bug fix in development branch”:
git revert HEAD~2
Task 2: Working with Branches
Demonstrate Branches
Create 2 or more branches and take screenshots to show the branch structure.
Image Placeholder: Screenshot showing the branches.
Merge Changes into Master
Make changes to the
dev
branch and merge it intomaster
:git checkout master git merge dev
Practice Rebase
Rebase changes and observe the differences:
git rebase master
Make changes in the
master
branch and commit them.Switch to the
dev
branch and check file content before rebasing.Perform the rebase and verify the updated file content in
dev
.
Best Practices
Following best practices for branching is crucial. Check out these best practices commonly followed in the industry.
References
Connect with me on LinkedIn to stay updated on my DevOps journey!