Day 10 : Git and GitHub Advance
Another step towards 'Git' and 'GitHub' (Part 1)
โญIntroduction
In the previous blog, we learned a lot about Git and Github๐๐ผ. We went through the topics like Git Types of Branches ๐ฟ and the Importance of Git ๐ญ. This blog will go over advanced Git topics like Git Rebase, Merge, Revert and Reset ๐.
Git Branching๐ฟ
Think of branches as parallel universes ๐ณ๐ within your project. Each branch represents a separate timeline ๐๐ง, allowing you to work on different ideas without affecting the main project.
This is how you create a branch:
git checkout -b <BRANCH_NAME>
Git Revert ๐
Think of Git Revert as an "undo" button ๐๐ for your project. It helps you reverse the changes made in a specific commit, like stepping back in time โช๐ง, while keeping a clear history.
When to Use Revert?
Use Git Revert๐ when you want to undo a specific commit without altering the history. It's like selectively removing a change without affecting the rest of the project.
Hands-On Git Revert ๐โช๐ง:
Let's say you have a commit with the ID ABC123
that introduced a bug.
Identify and Revert: Use
git revert
to undo the changes from that commit:git revert ABC123
Resolve Conflicts: If there are conflicts, resolve them just like in a regular merge.
Commit the Reversion: Once resolved, Git creates a new commit that undoes the problematic changes.
Git Reset ๐
Git Reset is like a time machine ๐งโช that lets you go back to a previous state of your project. It erases commits and takes you to an earlier version, but be cautious as it can discard some history.
When to Use Reset?
Use Git Reset๐ when you need to "rewind" your project to a specific point in time, erasing commits and altering the history. However, be cautious as it can be a powerful tool.
Difference Between Reset๐ and Revert๐.
Git Revert and Reset give you the ability to control your project's history โช๐๐. Revert selectively undoes changes, while Reset takes you back to a specific state.
Git Merge ๐๐ค
Imagine Git Merge as a master builder ๐๐ทโโ๏ธ that brings different branches together. It takes changes from one branch and integrates them into another, creating a unified version with all the improvements.
๐ค Git Merge is your bridge to collaboration. When you're ready to combine your work with others' changes, Git Merge ensures that all progress is effectively integrated, making the project complete and cohesive.
You can merge your code using:
git checkout master
git merge <BRANCH_NAME>
Git Rebase ๐
Git Rebase is a powerful tool that enables you to reshape your commit history in a branch. It's like rearranging puzzle pieces to create a more organized and coherent picture.
Checkout the branch you want to rebase. For example, if you're working on a feature branch, switch to that branch.
git checkout feature
Use the command git rebase <base-branch>
where <base-branch>
is the branch you want to rebase onto. This could be the main branch or another relevant branch.
git rebase master
Tasks๐:
1.) Branch, Commit & Reset
First, create an Empty repo and Add 'devops/git/version01.txt'. Then add the following
This is the bug fix in development branch
Then, create a branch named dev
and Push the changes into dev
with the commit message "Added feature 2 in development branch".
git checkout -b dev
#After making changes
git add .
git commit -m "Added feature 2 in development branch"
git push origin dev
After reviewing the changes, add the remaining features.
2nd Feature: "This is gadbad code"
3rd Feature: "This feature will gadbad everything from now."
The next task is to revert the file to its previous state with the content โThis is the bug fix in the development branch.โ
First, use the log command to get the commit id then use the respective id to reset it back.
git log --oneline
git reset <COMMIT_ID>
2.) Merge and Rebase
In this task, we will merge the dev
and the main
branch using the git merge
command.
Make sure you are on the main branch then execute this command.
git merge <BRANCH_NAME>
Resolve conflicts if any, then check logs.
git log --oneline
Now, let's try again with git rebase
.
Switch to main
branch.
git checkout main
Then, execute this command.
git rebase dev
During the rebase, the commits from the โdevโ branch are moved on top of the existing commits in the โmainโ branch, resulting in an updated, linear commit history on the โmainโ branch. This process demonstrates how branches interact, and how Gitโs merge and rebase commands can be used to manage code development seamlessly.
โEndcard:
๐ Thank you for joining me on this insightful journey into the world of DevOps!
โค If you found this blog helpful and informative, don't forget to give it a like!
๐ Share this valuable knowledge with your friends and colleagues, so they can also benefit from understanding the power of DevOps!
๐ Stay updated with my latest posts and never miss out on exciting content! Click that Follow button to join and stay in the loop!
Follow me on Linkedin --> abdallah-qamar ๐
Stay tuned for Day 11...๐