Table of contents
Git Stash
git stash
is a command that is used to temporarily save changes that you have made in your working directory but are not yet ready to commit. This can be useful if you need to switch to a different branch or work on a different task but don't want to commit your current changes just yet.
When you run git stash
, Git will save your changes and revert your working directory to the state it was in when you last committed. You can then switch to a different branch or work on a different task. Later, when you're ready to return to the changes you stashed, you can use the git stash apply
command to apply the stash and restore your changes.
You can also create a stash with a message using the git stash save
command. For example, git stash save "WIP: feature XYZ"
would create a stash with the message "WIP: feature XYZ". This can be useful for keeping track of multiple stashes.
Git Cherry-Pick
git cherry-pick
is a command that is used to apply the changes of a specific commit to your current branch. This can be useful if you want to bring changes from another branch into your current branch without merging the entire branch.
To use git cherry-pick
, you'll need to specify the commit that you want to apply. For example, git cherry-pick abc123
would apply the changes from the commit with the hash abc123
to your current branch.
It's important to note that when you use git cherry-pick
, Git will create a new commit on your current branch with the changes from the cherry-picked commit. This means that the commit hash will be different than the original commit, even if the changes are the same.
Resolving Conflicts
Sometimes, when you try to merge changes from one branch into another, Git will encounter a conflict. This happens when there are conflicting changes in the same file or lines of code.
To resolve a conflict, you'll need to manually edit the file to remove the conflicting changes and choose which version of the code to keep. Once you've resolved the conflict, you can save the changes and continue with the merge.
To resolve conflicts in Git, you can use a merge tool like git mergetool
, which will open a graphical interface to help you resolve the conflict. Alternatively, you can edit the files manually using a text editor.
To mark a conflict as resolved, you'll need to add the changes to the index using git add
. Once all conflicts have been resolved and added to the index, you can complete the merge using git commit
.
It's important to carefully review all changes and resolve conflicts as soon as possible to avoid introducing errors into your codebase. It's also a good practice to communicate with your team about any conflicts you encounter to ensure that everyone is aware of the changes being made.
Task-01
- Create a new branch and make some changes to it.
git checkout -b my-new-branch
# Make some changes to files in the working directory
- Use git stash to save the changes without committing them.
git stash
- Switch to a different branch, make some changes and commit them.
git checkout master
# Make some changes to files in the working directory
git add .
git commit -m "Added some changes to the master branch"
- Use git stash pop to bring the changes back and apply them on top of the new commits.
git checkout my-new-branch
git stash pop
# Resolve any conflicts if necessary
git add .
git commit -m "Applied changes from my-new-branch"
Task-02
Assuming we are currently on the development branch:
- Add the new lines to version01.txt and commit with message "Added feature2.1 in development branch"
git add version01.txt
git commit -m "Added feature2.1 in development branch"
- Add the next set of lines to version01.txt and commit with message "Added feature2.2 in development branch"
git add version01.txt
git commit -m "Added feature2.2 in development branch"
- Add the final set of lines to version01.txt and commit with message "Feature2 completed"
git add version01.txt
git commit -m "Feature2 completed"
- Switch to the production branch and rebase it onto the development branch to include the commit messages.
git checkout production
git rebase -i development
# In the interactive rebase editor, change the "pick" command for each commit to "edit", save and exit the editor
# For each commit that is being edited, add the commit message from the development branch with "git commit --amend -m <message>" and save the changes
# Once all commits have been edited and amended, run "git rebase --continue" to complete the rebase
Task-03
Assuming we are currently on the production branch:
- Cherry-pick the commit "Added feature2.2 in development branch"
git cherry-pick <commit-hash>
# Resolve any conflicts if necessary
git add .
git commit -m "Cherry-picked Added feature2.2 in development branch"
- Add the new line to version01.txt and commit with message "Optimized the feature can you write a blog on this?"
git add version01.txt
git commit -m "Optimized the feature can you write a blog on this?"
Happy Learning ๐
Bhaktiben Kadiya