Day 10: Advance Git & GitHub for DevOps Engineers
Table of contents
Git Branching
Git branching allows for a structured and organized approach to software development. By working on isolated branches, developers can avoid conflicts, experiment with new features, and maintain a stable main codebase. It's a powerful technique that facilitates collaboration and efficient code management.
Git Revert and Reset
Sometimes, you may want to undo some of the changes you made in your repository.
Git Revert:
git revert
creates a new commit that undoes the changes introduced by a specific commit. It's a safe way to undo changes without altering the commit history. Reverting a commit is ideal for cases where you want to keep a record of the original change and explain why it was undone.
Syntax:
git revert <commit_hash>
Example:
git revert abc123 # Revert the commit with hash "abc123"
Git Reset:
git reset
is a more powerful command that can be used to undo commits and manipulate the commit history. It moves the branch pointer to a different commit, effectively erasing commits from the branch's history. It's important to use with caution, especially when working in shared repositories, as it can disrupt collaboration.
There are three main modes of git reset
:
Soft Reset (
--soft
):Moves the branch pointer to a specific commit but leaves the changes staged (i.e., ready to be committed again).
Useful when you want to recommit the changes with some modifications.
Mixed Reset (Default,
--mixed
):Moves the branch pointer to a specific commit and unstages the changes, but retains the changes in your working directory.
Useful when you want to review the changes before committing them again.
Hard Reset (
--hard
):Moves the branch pointer to a specific commit and discards all changes in the working directory and staging area.
Be cautious with this mode, as it permanently removes changes.
Syntax:
git reset [--soft | --mixed | --hard] <commit_hash>
Example:
git reset --soft abc123 # Soft reset to commit "abc123"
git reset --mixed xyz456 # Mixed reset to commit "xyz456"
git reset --hard def789 # Hard reset to commit "def789"
Git Rebase and Merge
Git Rebase:
Rebasing involves moving the entire history of a branch to a new starting point. It effectively replays the commits of one branch on top of another. Rebasing can create a linear, cleaner commit history, but it can also rewrite commit hashes, potentially causing conflicts.
Syntax:
git rebase <base_branch>
Example:
# While on the feature branch
git rebase main # Rebase the feature branch onto the main branch
Git Merge:
Merging combines changes from one branch into another, creating a new commit that has multiple parent commits. Merge commits explicitly indicate when a branch was merged into another.
Syntax:
git merge <branch_to_merge>
Example:
# While on the main branch
git merge feature # Merge the feature branch into the main branch
Choosing Between Rebase and Merge:
Use
git rebase
when you want to maintain a cleaner, more linear commit history and are willing to address potential conflicts.Use
git merge
when you want to preserve a clear record of branch integration and have a more straightforward approach to handling changes from multiple branches.
Task:
Switch to dev branch by this command
git checkout -b dev
Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside.
version01.txt should reflect at the local repo first followed by the Remote repo for review.
Your commit message should be “Added new feature”.
git commit -m "Added new feature”
We will use
git push
in order to reflect the local changes to the remote repository.
Add a new commit in
dev
branch after adding the below-mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines1st line>> This is the bug fix in the development branch
Commit this with message “Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with the message “Added feature3 in the development branch”
3rd line>> This feature will gadbad everything from now.
Commit with the message “Added feature4 in the development branch”
Restore the file to a previous version where the content should be “This is the bug fix in the development branch”. We can use
git revert
orgit reset
Conclusion
In this blog post, you have learned some of the advanced skills and techniques for using Git and GitHub as a DevOps engineer.
You learned how to:
Create and manage branches for different features and bug fixes
Revert and reset your changes to a previous state
Rebase and merge your branches to keep your history clean and avoid conflicts
Thank you for reading!
Subscribe to my newsletter
Read articles from Moiz Asif directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by