Day 10: Advance Git & GitHub for DevOps Engineers.
Introduction
This blog helps you understand complex topics like Git branching for better code organization. Learn how to fix mistakes with Git's revert and reset features. Dive into merging and updating code efficiently with Git's rebase and merge tools. This guide gives DevOps pros clear insights and methods to improve their work and collaboration.
Git Branching
Use a branch to isolate development work without affecting other branches in the 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 allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.
Git Revert and Reset
git revert
andgit reset
are two Git commands that can be used to undo changes made to a repository’s commit history. The main difference between the two is that git reset moves the branch pointer back in the chain to “undo” changes, while git revert adds a new commit at the end of the chain to “cancel” changes.git reset
: This command allows you to reset your current head to a specified state. You can reset the state of specific files as well as an entire branch. This is useful if you haven’t pushed your commit up to GitHub or another remote repository yet. You can use git reset to unstage a file, reset a file or set of files, or reset a branch to a prior commit.git revert
: This command is used for undoing changes to a repository’s commit history. Other “undo” commands like git checkout and git reset move the HEAD and branch ref pointers to a specified commit. git revert also takes a specified commit, however, it does not move ref pointers to this commit. Instead, it adds a new commit at the end of the chain to cancel changes.
Git Rebase and Merge
Both git rebase and git merge are used to integrate changes from one branch into another. However, they differ in how they accomplish this task.
Git Merge:
Git merge
is a command used to integrate changes from one branch into another. When executing a merge, Git combines the changes from the source branch into the target branch, creating a new commit that encapsulates these changes. This method preserves the commit history of both branches, allowing developers to track changes and contributions from various branches within the repository.
Git Rebase:
On the other hand, Git rebase
offers a different approach to integrating changes between branches. When you initiate a rebase, Git transplants the commits from the feature branch onto the tip of another branch. This action effectively rewrites the commit history, resulting in a more linear sequence of commits. By doing so, developers can maintain a cleaner and more straightforward commit history, which can be particularly useful when preparing feature branches for integration into a main or production branch.
Hands-On Practice Using Git Commands:
Task 1:
Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master
switch to dev
branch.
Clone your repository to the local.
Check the branch where you located. Using
git branch
command and switch to the dev branch and create a file name version01.txt & add content to it “This is the first feature of our application”.Add more commit below mention in your dev branch :
1st line>> This is the bug fix in development branch
Commit this with message “ Added feature2 in development branch”
2nd line>> This is not proper code
Commit this with message “ Added feature3 in development branch”
3rd line>> This feature will not proper code from now.
Commit with message “ Added feature4 in development branch
In these steps, you can only repeat the process, like echo
, git add,
and git commit
. After completing the above commits, check if your commit is successfully done or not using the command git log.
Now we Restore the file to a previous version where the content should be “This is the bug fix in development branch” using
git reset --hard <commit id>
Task 2:
Demonstrate the concept of branches with 2 or more branches with screenshot.
Add some changes to
dev
branch and merge that branch inmaster
As a practice try git rebase too, see what difference you get.
For Task 1, Here, I am creating one new branch named Branch1 and also adding one file named Version02.txt.
Now I make some changes in the development branch and commit them.
After making changes to the dev branch, I can merge this branch to the master using the git merge
command.
Use Git Rebase command for Restoring version01.txt to a Previous Version:
Creating Additional Branches
git checkout -b branch1 # Make changes and commit git checkout -b branch2 # Make changes and commit
Merging dev Branch into master:
git checkout master git merge dev git push origin master
Use Git Rebase:
git checkout branch1 git rebase dev # Resolve any conflicts, if there are any
Conclusion
In the world of DevOps, mastering Git is like having a superpower for collaboration and keeping code in check. Git Branching lets teams work on different parts of a project without stepping on each other's toes. Revert and Reset act like magic undo buttons, fixing mistakes and keeping the code history clean. Meanwhile, Rebase and Merge bring everything together smoothly, helping everyone's work fit into a coherent story. So, for DevOps Engineers, these Git tools are not just tech jargon; they're the keys to building, fixing, and weaving code with finesse.
Subscribe to my newsletter
Read articles from Aesha Shah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by