✨Table of Content
📌 Git Stash
🍒 Cherry-pick
⚔️ Resolving Conflicts
📌Git Stash:
What is it?
Git stash is a powerful command that lets you save your current changes in the working directory without committing them. This is useful when you need to switch contexts but don't want to lose your current work or create a commit that may be incomplete or irrelevant to the main project history.
Why use it?
Imagine you're working on a new feature and you suddenly need to fix a critical bug in another branch. You don’t want to commit your half-done feature work just yet because it might not be in a state that's ready to share or merge. Git stash allows you to temporarily set aside your changes and return to them later.
git stash # Stash changes in the working directory
git stash list # List all stashed changes
git stash apply # Apply the most recent stash to the working directory
git stash apply stash@{n} # Apply a specific stash
git stash drop # Delete the most recent stash
git stash drop stash@{n} # Delete a specific stash
🍒Cherry-pick:
What is it?
Git cherry-pick allows you to apply the changes introduced by an existing commit onto your current branch. This command is useful when you want to include specific changes from another branch without merging the entire branch.
Why use it?
Imagine you've made a bug fix in the bugfix
branch and you need this fix in the release
branch as well, but without bringing in any other changes from bugfix
. Git cherry-pick enables you to copy that specific fix.
To use git cherry-pick, you first create two new branches and make some commits to them. Then you use git cherry-pick <commit_hash> command to select the specific commits from one branch and apply them to the other.
git cherry-pick <commit-hash>
You can also cherry-pick multiple commits by specifying their commit hashes separated by spaces:
git cherry-pick <commit-hash-1> <commit-hash-2> ... <commit-hash-n>
⚔️Resolving Conflicts:
What is it?
Conflicts occur when Git cannot automatically reconcile differences between branches that are being merged or rebased. These conflicts need to be resolved manually.Indicators:
Conflict Notification: When Git encounters conflicts during a merge or rebase, it will notify you in the command line.
Status Check:
git status
This command shows the current status of the working directory and highlights files with conflicts.Difference Check:
git diff
This command shows the differences between conflicting versions of files, highlighting the conflict markers.
How to Resolve:
Compare Changes: Open the conflicting files. Git will mark conflicts within the files using conflict markers:
<<<<<<< HEAD Your changes ======= Changes from the branch being merged >>>>>>> branch_name
Make Adjustments: Manually edit the file to reconcile the differences. Decide which changes to keep, modify, or combine.
Stage Resolved Files: After resolving the conflicts, stage the resolved files using
git add <file>
.Complete the Process: Complete the merge or rebase operation:
For a merge:
git commit
(if necessary, as Git may auto-commit).For a rebase:
git rebase --continue
.
Stay tuned for more updates on my DevOps journey! 📅
Feel free to contact me if you have any questions or need any more help. Let’s connect, learn, and succeed together!
Happy Learning! 😊