Git Stash: A Crafty DevOps Tool
Understanding Git Stash
In the intricate dance of version control, Git stash emerges as a hidden gem. It allows you to gracefully save your current changes without committing them, perfect for those moments when you need to switch branches without committing unfinished work.
Theoretical Insights:
Git Stash Basics:
Explore the purpose of Git stash in temporarily saving changes.
Learn how to stash changes and retrieve them when needed.
Discover commands like
git stash list
,git stash drop
, andgit stash clear
.
Practical Task:
Stashing Changes:
# Create a new branch and make changes git checkout -b feature-branch # Make changes... git stash save "Work in Progress" # Switch to a different branch, make changes, and commit git checkout main # Make changes... git commit -m "New changes in main branch" # Bring back the stashed changes git stash pop
Cherry-Pick: Selective Commit Magic
Unveiling Git Cherry-Pick
Git cherry-pick adds a touch of elegance to version control, allowing you to select specific commits from one branch and apply them selectively to another.
Theoretical Insights:
Cherry-Pick Magic:
Understand how
git cherry-pick
works.Grasp the scenarios where cherry-pick becomes a valuable asset.
Practical Task:
Cherry-Picking Commits:
# Create two new branches and make commits git checkout -b branch-1 # Make commits... git checkout -b branch-2 # Make commits... # Cherry-pick specific commits to another branch git checkout target-branch git cherry-pick <commit_hash>
Resolving Conflicts: Navigating Git Harmony
Mastering Conflict Resolution
Conflicts are an inevitable part of branching, merging, and rebasing. Git equips you with tools to gracefully resolve conflicts and proceed with your version control journey.
Theoretical Insights:
Conflicts in Git:
Explore why conflicts occur during merges and rebases.
Utilize commands like
git status
,git diff
, andgit add
to navigate and resolve conflicts.
Practical Task:
Resolving Conflicts:
Create conflicts intentionally.
Resolve conflicts using
git status
,git diff
, andgit add
.
Practical Application: Tying It All Together
Task-01: Stash, Switch, and Pop
Create a New Branch and Make Changes:
git checkout -b feature-branch echo "Work in progress" > file.txt git add . git commit -m "Add work in progress"
Stash Changes Without Committing:
git stash save "Work in Progress"
Switch to a Different Branch, Make Changes, and Commit:
git checkout main echo "New changes in main branch" > file.txt git add . git commit -m "Commit in main branch"
Bring Back Stashed Changes:
git stash pop
Task-02: Feature Enhancement and Rebase
Add New Lines to version01.txt in Development Branch:
Open
version01.txt
in your code editor.Add the specified lines after "This is the bug fix in development branch."
Commit Changes and Rebase onto Master:
git add . git commit -m "Added feature2.1 in development branch" git rebase main
Task-03: Cherry-Pick and Optimize
Cherry-Pick a Specific Commit to the Production Branch:
git checkout production git cherry-pick <commit_hash_of_feature2.2>
Add New Lines for Optimization:
Open
version01.txt
in your code editor.Add the specified lines after "This is the advancement of the previous feature."
Save the file.
Commit Changes for Optimization:
git add . git commit -m "Optimized the feature"
After completing these steps, your local Git repository should reflect the changes made in the specified tasks. Ensure to adapt the commands and paths based on your specific project structure.
Continue your DevOps journey on Hashnode.
#90DaysOfDevOpsChallenge #Git #GitHub #DevOpsJourney #HashnodeBlog #CollaborativeCoding #trainwithshubham #shubhamlondhe