Git Stash
Git stash is a command that allows you to temporarily save changes that you have made to a branch, without committing those changes.
This can be useful if you need to switch to a different branch to work on something else, but you don't want to lose your progress on the current branch.
Scenario based example on how Git Stash works:
Suppose you are a developer working on a project and you are making changes to a branch. Suddenly, you get an urgent request to work on a different branch to fix a critical bug. You want to switch to the other branch, but you don't want to commit your changes on the current branch yet.
Here's how Git stash can help:
Here are some common git stash
commands:
git stash save <message>
: Creates a new stash with the changes in your working directory and a custom message.git stash list
: Lists all the stashes on the stash stack.git stash apply
: Applies the most recent stash to your working directory, leaving the stash on the stack.git stash pop
: Applies the most recent stash to your working directory and removes the stash from the stack.git stash drop
: Removes the most recent stash from the stack.git stash clear
: Removes all stashes from the stack.
git stash
is a useful tool for managing changes in Git and allows you to switch between tasks or branches without committing incomplete work.
TASK:
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.
Make some changes to the main branch, such as adding or modifying code files.Once you have applied the changes, commit them using the command git commit -m 'completed work on <original branch>'
.
Resolve any conflicts that may arise, using the steps outlined in the previous answer.
Review the changes and commit them to the main branch if they are correct, using the following commands:
By using Git stash, you can temporarily save changes to a stash and switch to a different branch to work on something else without losing your progress. You can then apply the changes back to the original branch and continue working where you left off.
Git cherry-pick is a command that allows you to pick a single commit from one branch and apply it to another branch. It is a useful command for applying specific changes made in one branch to another branch.
Scenario based example on how Git Cherrry-Pick works:
Suppose you are a developer working on a project and you have two branches, feature A and feature B. You have completed work on feature A and have made several commits to that branch. However, there is one commit in feature A that you want to apply to feature B.
Here's how Git cherry-pick can help:
Identify the commit: Use the
git log --oneline
command to identify the commit that you want to cherry-pick. Note the commit hash, which is a unique identifier for that commit.Use Git cherry-pick: Run the command
git cherry-pick <commit hash>
. This will apply the changes made in the commit that you identified in step 1 onto feature B.Resolve conflicts: If there are any conflicts between the two branches, you will need to resolve them before you can continue. Use the
git mergetool
command to resolve any conflicts.Commit changes: Once you have resolved any conflicts, commit the changes using the command
git commit -m 'cherry-picked commit from feature A into feature B'
.
Suppose, I am on branch dev and I have file called feature01 and I did below tasks.
Again did the same task as below.
Here is cat of feature01.txt
Here is git log.
Now I will go to master branch which has same file and I will add the third commit to my feature01.txt file in master.
Now run cherry-pick command along with the commit id
Don't worry you just need to do
By using Git cherry-pick, you can easily apply specific changes made in one branch to another branch. In this scenario, you identified a single commit that you wanted to apply to feature B and used Git cherry-pick to apply those changes. This can save you time and effort compared to merging the entire feature A branch into feature B, which may include changes that you don't need or want.
GIT REBASE :
Git rebase is a command that lets users integrate changes from one branch to another, and the logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs.
Git Conflict Resolution
Git conflict resolution occurs when there are conflicting changes made to the same file in different branches. Git provides tools to help you resolve these conflicts and merge the changes together.
Scenario based example on how Git Conflict Resolution works:
Suppose you are a developer working on a project and you have two branches, feature A and feature B. You have made changes to a file in feature A, and your colleague has made changes to the same file in feature B. When you try to merge feature A into feature B, Git detects a conflict and stops the merge process.
Here's how you can resolve the conflict:
Identify the conflict: Run the command
git status
to identify which file has a conflict. The conflicting file will have a message sayingboth modified
.Use Git mergetool: Run the command
git mergetool
. This will open a visual tool, such as vimdiff or meld, that allows you to see the changes made to the file in both branches side-by-side.Resolve the conflict: In the visual tool, you will need to manually choose which changes to keep and which to discard. You can use the arrow keys to move between the conflicting sections and make changes as needed. Once you have resolved the conflict, save and close the visual tool.
Use Git add: Run the command
git add <filename>
. This will stage the changes that you made to the conflicting file.Commit the changes: Run the command
git commit -m 'resolved conflict in <filename>'
. This will create a new commit that resolves the conflict and allows you to continue merging the branches together.
By using Git conflict resolution tools, you can easily identify and resolve conflicts that occur when merging changes from different branches. In this scenario, you used Git mergetool to visualize the conflicting changes and manually resolve the conflict before committing the changes.