Git, the version control system, has revolutionized the way developers collaborate on projects. With its powerful features and flexibility, Git empowers teams to work seamlessly, manage code efficiently, and resolve conflicts effectively. In this blog, we'll delve into some advanced Git concepts - Stash, Cherry-Pick, conflicts, and their resolution strategies - to equip you with the skills needed to navigate through complex development scenarios like a pro!
1. Git Stash
Git Stash is a lifesaver when you need to temporarily shelve changes without committing them. It allows you to stash away unfinished work, switch branches, and come back to your changes later. Here's how it works:
Stash Changes:
git stash
command saves your modified tracked files and staged changes.List Stashes: Use
git stash list
to see a list of stashed changes.Apply Stash: Retrieve stashed changes with
git stash apply
orgit stash pop
.Clear Stash: Remove stashed changes using
git stash drop
.
2. Git Cherry-Pick
Cherry-picking enables you to pick specific commits from one branch and apply them onto another. It's handy for incorporating individual changes without merging entire branches. Here's how to cherry-pick like a pro:
Select Commit: Identify the commit you want to cherry-pick.
Cherry-Pick: Execute
git cherry-pick <commit>
to apply the chosen commit.Resolve Conflicts: Handle conflicts if any arise during cherry-pick process.
Commit Changes: After resolving conflicts, commit the changes.
3. Conflicts and Its Types
Conflicts occur when Git can't automatically merge changes from different branches. Understanding the types of conflicts helps you tackle them effectively:
Content Conflict: Changes in the same part of a file result in content conflicts.
Rename/Move Conflict: When a file is renamed or moved differently in branches.
Delete/Modify Conflict: One branch deletes a file, while the other modifies it.
Binary Conflict: Occurs with binary files that Git can't merge automatically.
4. Resolving Conflicts
Resolving conflicts is an essential skill for every Git user. Here's a systematic approach to resolve conflicts like a pro:
Identify Conflicts: Use
git status
to see conflicted files.Open Files: Open conflicted files in your text editor.
Resolve Conflicts: Manually edit the files to resolve conflicts.
Mark as Resolved: After resolving conflicts, stage the changes with
git add
.Commit Changes: Finally, commit the resolved changes with
git commit
.
Task-01
Create a new branch and make some changes to it.
Use git stash to save the changes without committing them.
Switch to a different branch, make some changes and commit them.
Use git stash pop to bring the changes back and apply them on top of the new commits.
Task-02
In version01.txt of development branch add below lines after “This is the bug fix in development branch” that you added in Day10 and reverted to this commit.
Line2>> After bug fixing, this is the new feature with minor alteration”
Commit this with message “ Added feature2.1 in development branch”
Line3>> This is the advancement of previous feature
Commit this with message “ Added feature2.2 in development branch”
Line4>> Feature 2 is completed and ready for release
Commit this with message “ Feature2 completed”
All these commits messages should be reflected in Production branch too which will come out from Master branch (Hint: try rebase).
With these powerful Git techniques in your arsenal, you're equipped to handle complex development scenarios with ease. Whether it's stashing away unfinished work, cherry-picking specific changes, or gracefully resolving conflicts, Git empowers you to collaborate seamlessly and deliver exceptional code. Happy coding!