🚀Day 10 Task: Advance Git & GitHub for DevOps Engineers.
🔗Git Branching
Git branching is a fundamental concept in version control systems, particularly in Git, a distributed version control system widely used for source code management.
Git branching allows developers to work on different aspects of a project simultaneously without interfering with each other's work.
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
Both git revert
and git reset
are Git commands used to undo changes in a repository, but they operate differently and serve different purposes.
Git Revert:
The git revert
a command is used to create a new commit that effectively undoes the changes introduced by a previous commit. This is a safe way to revert changes without altering the project's history. The original commit remains in history, and a new commit is created to reverse its effects.
Usage:
git revert <commit-hash>
Key points:
It's a way to undo individual commits while preserving the commit history.
It doesn't remove any commits; instead, it adds new commits that negate the changes.
Useful when you want to undo changes that have already been shared with others.
Git Reset: The git reset
a command is used to manipulate the commit history by moving branches or resetting the HEAD to a specific commit. It can be used in multiple modes, each with different effects on the repository.
Usage examples:
git reset --soft <commit-hash> # Moves HEAD to a specific commit, keeping changes in staging
git reset --mixed <commit-hash> # Moves HEAD to a specific commit, unstaging changes
git reset --hard <commit-hash> # Moves HEAD and discards all changes after the commit
Key points:
It allows you to reset the branch to a specific commit, potentially discarding commits and changes after that commit.
Depending on the mode used (
--soft
,--mixed
, or--hard
), it can impact the staging area and the working directory differently.
🔗Git Rebase and Merge
git rebase
and git merge
are two different ways of integrating changes from one branch into another in Git. They have distinct effects on the commit history and can be used in different scenarios.
\=================================
Git Merge:
Merging is a straightforward way to integrate changes from one branch into another.
When you merge a branch into another, Git creates a new commit that has two parent commits: one from the current branch and another from the branch you're merging in.
Merged changes form a linear history, where you can clearly see when each branch was merged and the order of merges.
Merges preserve the original branch's commit history.
Merging is useful for incorporating feature branches or bug fixes into a main development branch (e.g., master/main).
Usage of Git Merge:
# Assuming you're on the target branch (e.g., master/main)
git merge <source-branch>
\=========================================
Git Rebase:
Rebasing involves moving or combining a series of commits from one branch onto another.
When you rebase a branch onto another, Git "replays" the commits from the source branch on top of the destination branch, creating a new linear history.
Rebasing can help maintain a cleaner and more linear commit history as if the changes on the source branch were developed directly on the destination branch.
Rebasing can make the commit history easier to understand by removing unnecessary merge commits.
Rebasing is suitable for feature branches that are still in progress and need to incorporate the latest changes from the main branch before they are merged.
Usage of Git Rebase:
# Assuming you're on the feature branch
git rebase <base-branch>
Key Differences:
Commit History: Merging results in a branching commit history, while rebasing creates a linear commit history.
Visibility of Changes: Merged changes are clearly visible in the merge commit, while rebased changes become part of a continuous history.
Commit IDs: Merging preserves original commit IDs while rebasing creates new commit IDs.
Collaboration and Shared Repositories: Merging is generally safer in shared repositories since it doesn't rewrite history. Rebasing can cause issues if used improperly in shared environments.
Scenario: Use merging for incorporating finished features and bug fixes, and use rebasing for integrating work-in-progress changes onto a main branch.
📚Tasks
👉🏼 Task 1:
Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside.
This should be in a branch coming from master
,
switch to dev
branch ( Make sure your commit message will reflect as "Added new feature").
- version01.txt should reflect at local repo first followed by Remote repo for review.
Add new commit in dev
branch after adding below mentioned content in Devops/Git/version01.txt:
While writing the file make sure you write these lines
1st line>> This is the bug fix in the development branch
Commit this with a message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with a message “ Added feature3 in development branch
3rd line>> This feature will gadbad everything from now.
Commit with a message “ Added feature4 in development branch
👉🏼 Task 2:
- Demonstrate the concept of branches with 2 or more branches with screenshots.
git branch
- add some changes to
dev
branch and merge that branch inmaster
git checkout dev # switch to dev branch
git checkout master
git merge dev
- as a practice try git rebase too, and see what difference you get.
git rebase master
🌟 Conclusion:
Congratulations! 🎉
Git is an essential tool ⚙️ for developers 👨🏻💻to manage their code, collaborate effectively, and maintain a detailed record of their project's development history. It's widely used across the software industry and has become a standard tool for version control and collaborative coding.
GitHub 😸 is a web-based platform where developers can collaborate, contribute, and showcase their coding skills.
We hope this blog has given you a Question/Answer 🚀💪
🔍 Did you find this blog helpful? Let us know in the comments below! 👇 And if you have any questions or need further assistance, we're here to help! 🤗
Happy Learning! 💻✨