Git, the version control system, empowers developers worldwide to collaborate seamlessly and manage their projects efficiently. Among its myriad features, mastering Git branching, reverting, resetting, rebase, and merge is crucial for maintaining a clean and organized codebase. Let's delve into these essential aspects and unlock the full potential of Git.
Git Branching:
Branches: Think of branches as parallel universes where you can work on features or fixes without affecting the main codebase.
Creating Branches: Use
git branch <branch-name>
to create a new branch andgit checkout <branch-name>
to switch to it.Merging Branches: After completing your changes, merge the branch back into the main codebase using
git merge <branch-name>
.Branch Management: Keep your repository clean by regularly deleting merged branches with
git branch -d <branch-name>
.
Git Revert and Reset:
Revert: Undo specific commits while preserving the commit history using
git revert <commit-id>
.Reset: Roll back changes entirely with
git reset <commit-id>
. Use--soft
,--mixed
, or--hard
flags for different reset modes.Caution: Exercise caution with reset as it can alter history and potentially lose commits. Only use it for local changes.
Feature | Git Reset | Git Revert |
Action | Resets current branch to a specified commit | Reverts specified commits by creating new ones |
Commit History | Rewrites commit history | Preserves commit history |
Impact | Hard reset: discards commits permanently | Soft reset: retains commits in reflog |
Collaboration | Can cause conflicts in shared repositories | Safer for shared repositories |
Usage | Useful for local changes and branch cleanup | Recommended for reverting public changes |
Git Rebase and Merge:
Rebase: Rewrite commit history to maintain a linear project history. Use
git rebase [base] [branch]
to rebase the current branch onto the base branch.Interactive Rebase: Fine-tune commits interactively with
git rebase -i [base]
.Merge: Combine changes from different branches into one. Use
git merge <branch>
to merge the specified branch into the current one.
Feature | Git Rebase | Git Merge |
Operation | Reapplies commits on top of another | Integrates changes from different branches |
History | Linearizes commit history | Preserves branch history |
Clarity | Results in a cleaner, linear history | Maintains original context of development |
Conflicts | Resolved per commit | Resolved at merge com |
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
, [hint try git checkout -b dev
], swithch to dev
branch ( Make sure your commit message will reflect as "Added new feature"). [Hint use your knowledge of creating branches and Git commit command]
- version01.txt should reflect at local repo first followed by Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
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 development branch
Commit this with message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with message “ Added feature3 in development branch
3rd line>> This feature will gadbad everything from now.
Commit with message “ Added feature4 in development branch
Restore the file to a previous version where the content should be “This is the bug fix in development branch” [Hint use git revert or reset according to your knowledge]
Task 2:
Demonstrate the concept of branches with 2 or more branches with screenshot.
add some changes to fruits
branch and merge that branch in main
as a practice try git rebase too, see what difference you get.
Created fruits branch using command git checkout -b fruits
Created games branch using command git checkout -b games
Use command git branch to list all the git branches (* is current branch)
Use command git merge fruits to merge fruits branch in main branch
In conclusion, Git's versatility lies in its ability to adapt to various workflows seamlessly. By mastering branching, reverting, resetting, rebase, and merge, you equip yourself with the tools needed to navigate complex development scenarios with ease. Embrace Git's power, experiment fearlessly, and let it propel your projects to new heights!
Happy coding!