Day 11 Task: Advance Git & GitHub for DevOps Engineers: Part-2
Git Stash 📦
Git stash is a powerful and handy feature in Git that allows you to temporarily save changes that you don't want to commit immediately. It's like putting your changes in a "stash" or a temporary storage area. This can be useful in various situations, such as when you need to switch branches but don't want to commit your changes, or when you want to isolate your changes to focus on a different task.
Key Features:
📁 Temporary Storage: Stash allows you to save your current changes without committing them, creating a clean working directory.
🔄 Switching Branches: Handy when you need to switch branches but don't want to commit half-finished work.
🧹 Clean Working Directory: Helps maintain a clean working directory by allowing you to stash changes and start with a fresh slate.
🚧 Interrupted Workflow: Useful in situations where you need to interrupt your work to address an urgent task or fix a critical issue.
Basic Usage:
git stash save "message": Stash your changes with an optional message.git stash list: View the list of stashes.git stash apply stash@{n}: Apply a specific stash to your working directory.git stash pop: Apply the latest stash and remove it from the stash list.git stash drop stash@{n}: Discard a specific stash.git stash clear: Remove all stashes.
Example:
# Stash changes
git stash save "Work in progress on feature X"
# List stashes
git stash list
# Apply changes from stash
git stash apply stash@{0}
# Discard a stash
git stash drop stash@{0}
Using Git stash effectively can enhance your workflow by providing a flexible way to manage your changes in progress.
Git Cherry-Pick 🍒
Git cherry-pick is a command that allows you to apply a specific commit from one branch to another. It's like handpicking a commit from one branch and applying it onto another branch. This can be particularly useful when you want to bring in specific changes without merging the entire branch.
Key Features:
🍒 Selective Commits: Cherry-pick allows you to choose specific commits to incorporate into another branch.
🔄 Branch Independence: You can cherry-pick commits from one branch into another without merging the entire branch.
🚀 Targeted Changes: Useful when you only need certain changes from a branch and want to avoid merging unrelated code.
🔍 Granular Control: Provides fine-grained control over which changes are brought into another branch.
Basic Usage:
git cherry-pick <commit-hash>: Applies the changes introduced by the specified commit to the current branch.git cherry-pick -n <commit-hash>: Performs the cherry-pick but stages the changes, allowing you to make modifications before committing.git cherry-pick -x <commit-hash>: Adds a line to the commit message indicating that it was cherry-picked.git cherry-pick -e <commit-hash>: Opens the commit message in the default text editor, allowing you to modify it before committing.
Example:
# Switch to the target branch
git checkout target-branch
# Cherry-pick a specific commit
git cherry-pick abcdef123
# Cherry-pick with commit message editing
git cherry-pick -e abcdef123
# Cherry-pick multiple commits
git cherry-pick abcdef123 123456789
Cherry-picking is a powerful feature for incorporating specific changes across branches, giving you flexibility in managing your codebase without merging entire branches.
**
Resolving Conflicts 🤔🛠️**
Resolving conflicts in Git is a crucial step in collaborative development when changes made by different contributors overlap and cannot be automatically merged. Conflict resolution ensures that the final result combines the intended changes seamlessly.
Key Points:
🧩 Collaborative Challenges: Conflicts arise when multiple contributors modify the same part of a file concurrently.
⚖️ Manual Intervention: Resolving conflicts often requires manual intervention to decide which changes to keep, discard, or merge.
🔄 Three-Way Merge: Git uses a three-way merge approach, comparing the common ancestor, the current branch, and the incoming changes.
🚦 Conflict Markers: Git marks conflict areas in the affected files, allowing you to identify and resolve conflicts manually.
Basic Steps:
🔄 Pull Changes: Fetch the latest changes from the remote repository using
git pull.🚧 Conflict Indication: Git marks conflicted files with conflict markers (
<<<<<<<,=======,>>>>>>>).📝 Manual Edit: Open the conflicted file, locate the conflict markers, and manually edit the content to resolve conflicts.
✅ Add Changes: After resolving conflicts, add the changes using
git add.🔄 Complete Merge: Complete the merge process with
git mergeorgit rebase, depending on the workflow.💾 Commit Changes: Finally, commit the resolved changes to complete the merge.
Example:
# Fetch the latest changes
git pull origin main
# Resolve conflicts manually
# Open conflicted files, edit, and remove conflict markers
# Add resolved changes
git add conflicted-file.txt
# Complete the merge
git merge main
# Commit the merged changes
git commit -m "Merge branch 'main' into feature-branch"
Resolving conflicts is a collaborative effort that ensures the integrity of the codebase. It requires clear communication among team members to understand the intent of conflicting changes and reach a consensus on how to integrate them seamlessly.
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.
Solution:
Certainly! Let's walk through the steps to achieve this scenario:
# Create a new branch and make some changes
git checkout -b feature-branch
echo "Changes in feature-branch" >> myfile.txt
git add myfile.txt
git commit -m "Add changes in feature-branch"
# Use git stash to save the changes without committing them
git stash save "Stashing changes in feature-branch"
# Switch to a different branch (e.g., main), make some changes, and commit them
git checkout main
echo "Changes in main branch" >> anotherfile.txt
git add anotherfile.txt
git commit -m "Add changes in main branch"
# Use git stash pop to bring the changes back and apply them on top of the new commits
git checkout feature-branch
git stash pop
# Now myfile.txt has the changes from feature-branch on top of the changes in main branch
In this example, we created a new branch (feature-branch), made changes, stashed those changes without committing, switched to a different branch (main), made changes, and committed them. Finally, we switched back to feature-branch and used git stash pop to apply the stashed changes on top of the new commits in main.
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).
Solution:
Assuming you are currently on the
developmentbranch:# Add lines to version01.txt in development branch echo "After bug fixing, this is the new feature with minor alteration" >> version01.txt git add version01.txt # Commit feature 2.1 git commit -m "Added feature2.1 in development branch" # Add lines for feature 2.2 echo "This is the advancement of the previous feature" >> version01.txt git add version01.txt # Commit feature 2.2 git commit -m "Added feature2.2 in development branch" # Add lines for completed feature 2 echo "Feature 2 is completed and ready for release" >> version01.txt git add version01.txt # Commit completed feature 2 git commit -m "Feature2 completed" # Switch to the master branch git checkout master # Create a new production branch from master git checkout -b production # Rebase production branch onto development to bring in the commit messages git rebase development # Now the production branch has the changes from the development branch with the desired commit messages.In this example, we added lines to
version01.txtin thedevelopmentbranch, made three separate commits with the specified commit messages. Then, we switched to themasterbranch and created a new branch calledproduction. Finally, we performed a rebase of theproductionbranch onto thedevelopmentbranch to bring in the commit messages from thedevelopmentbranch into theproductionbranch.Task-03
In Production branch Cherry pick Commit “Added feature2.2 in development branch” and added below lines in it:
Line to be added after Line3>> This is the advancement of previous feature
Line4>>Added few more changes to make it more optimized.
Commit: Optimized the feature
Solution:
Certainly! Here's how you can cherry-pick the commit, add the requested lines, and make a new commit in the
productionbranch:Assuming you are currently on the
productionbranch:# Cherry-pick the commit "Added feature2.2 in development branch" git cherry-pick <commit-hash-of-feature2.2> # Add lines after "This is the advancement of the previous feature" echo "Added few more changes to make it more optimized." >> version01.txt git add version01.txt # Commit the changes git commit -m "Optimized the feature"Replace
<commit-hash-of-feature2.2>with the actual commit hash of the "Added feature2.2 in development branch" commit.In this example, we cherry-picked the specific commit into the
productionbranch, added additional lines to the file, and committed the changes as "Optimized the feature" in theproductionbranch.
HAPPY LEARNING !!!