Content :-
Git Stash.
Cherry-pick.
Resolving Conflicts.
Git Stash :-
Git stash is a command in the Git version control system that allows you to temporarily store changes that are not yet ready to be committed to your repository. This can be useful in various scenarios such as when you need to switch to a different branch or pull in changes from a remote repository but don't want to commit your current changes.
Here's how it works:
Stash changes: Use the command
git stash
to stash your changes. This command will save your modifications and revert your working directory to the last commit.Work on something else: After stashing your changes, you can work on something else, switch branches, or perform any other operations without worrying about your uncommitted changes.
Apply stashed changes: When you're ready to reapply your stashed changes, you can use
git stash apply
orgit stash pop
. Theapply
command reapplies the changes from the stash but keeps the stash intact, whilepop
applies the changes and removes the stash from the stack.Multiple stashes: You can stash multiple sets of changes. Git maintains a stack of stashes, so you can stash changes multiple times, and then apply or pop them in the reverse order in which they were stashed.
Inspect stashes: You can view the list of stashes using
git stash list
. This will show you all the stashed changes along with a unique identifier for each stash.Clear stashes: To remove stashed changes, you can use
git stash drop
followed by the identifier of the stash you want to remove. Alternatively, you can usegit stash clear
to remove all stashed changes.
Here's a simple example workflow:
# Make some changes to your files
git add .
git commit -m "Work in progress"
# Realize you need to switch branches
git stash
# Switch to another branch
git checkout <branch>
# Do some work on the new branch
# Switch back to your original branch
git checkout <original_branch>
# Apply the stashed changes
git stash apply
# Continue working on your changes
That's the basic idea of using git stash
to temporarily store changes in Git. It's a handy tool for managing your work in progress.
Cherry-pick :-
git cherry-pick
is a command in Git that allows you to apply the changes introduced by one or more existing commits onto the current branch. This can be useful when you want to pick specific commits from one branch and apply them to another branch, without merging the entire branch.
Here's how you can use git cherry-pick
:
Identify the commit(s): First, you need to identify the commit or commits that you want to apply to your current branch. You can find the commit hashes using
git log
or any Git history visualization tool.Checkout the target branch: Before cherry-picking, make sure you are on the branch where you want to apply the changes. You can switch branches using
git checkout <branch_name>
.Cherry-pick the commit(s): Use the
git cherry-pick
command followed by the commit hash(es) of the commit(s) you want to apply. For example:git cherry-pick <commit_hash>
If you want to cherry-pick multiple commits, you can specify their hashes separated by spaces:
git cherry-pick <commit_hash1> <commit_hash2> ...
Resolve conflicts (if any): If the changes you're cherry-picking conflict with the current state of your branch, Git will pause the cherry-pick process and ask you to resolve the conflicts manually. You'll need to edit the conflicted files to resolve the conflicts and then complete the cherry-pick by running
git cherry-pick --continue
.Complete the cherry-pick: After resolving any conflicts, Git will apply the changes from the cherry-picked commit(s) to your current branch. If there are no conflicts, the cherry-pick will be completed automatically.
Repeat if necessary: You can cherry-pick multiple commits one after the other if needed.
Here's a simple example workflow:
# Switch to the branch where you want to apply the changes
git checkout <target_branch>
# Cherry-pick a single commit
git cherry-pick <commit_hash>
# Cherry-pick multiple commits
git cherry-pick <commit_hash1> <commit_hash2> ...
# Resolve conflicts if any
# Edit the conflicted files
git add .
git cherry-pick --continue
git cherry-pick
is a powerful tool for selectively applying changes from one branch to another. However, it's essential to use it carefully, especially when cherry-picking multiple commits, as it can lead to conflicts and potentially alter the commit history of your repository.
Resolving Conflicts :-
Resolving conflicts in Git occurs when there are conflicting changes between the branch you're merging into and the branch you're merging from, or when you're cherry-picking commits that conflict with the current state of your branch. When conflicts occur, Git will pause the merging or cherry-picking process and mark the conflicted files. You'll then need to manually resolve these conflicts before proceeding. Here's how to do it:
Identify conflicted files: After attempting to merge or cherry-pick commits, if Git encounters conflicts, it will stop and mark the conflicted files. You can identify these files by running
git status
, which will show them as "both modified".Open conflicted files: Open the conflicted files in your preferred text editor. Inside these files, you'll see conflict markers indicating the conflicting changes. The conflict markers look like this:
<<<<<<< HEAD // Your current changes ======= // Changes from the commit being merged or cherry-picked >>>>>>> <commit_hash>
Between
<<<<<<< HEAD
and=======
are your changes (the changes in your current branch), and between=======
and>>>>>>> <commit_hash>
are the changes from the commit being merged or cherry-picked.Resolve conflicts: Edit the conflicted files to resolve the conflicts. Decide which changes you want to keep and delete the conflict markers (
<<<<<<<
,=======
,>>>>>>>
). You may need to manually modify the code to merge the changes correctly. Once you've resolved the conflicts, save the files.Add the resolved files: After resolving the conflicts, stage the resolved files using
git add <resolved_file>
.Complete the merge or cherry-pick: If you're merging branches, use
git merge --continue
to complete the merge. If you're cherry-picking commits, usegit cherry-pick --continue
.Commit the changes: After resolving conflicts and completing the merge or cherry-pick, commit the changes using
git commit
. Git will automatically generate a commit message indicating that conflicts were resolved.
Here's a simplified example of resolving conflicts during a merge:
# Attempt to merge branch 'feature' into 'master'
git checkout master
git merge feature
# Resolve conflicts (edit conflicted files)
# Add resolved files
git add <resolved_file>
# Complete the merge
git merge --continue
# Commit the changes
git commit
And here's a simplified example of resolving conflicts during a cherry-pick:
# Cherry-pick a commit
git cherry-pick <commit_hash>
# Resolve conflicts (edit conflicted files)
# Add resolved files
git add <resolved_file>
# Complete the cherry-pick
git cherry-pick --continue
# Commit the changes
git commit
Resolving conflicts is an integral part of collaborating with Git, especially when working on projects with multiple contributors or when integrating changes from different branches.