Git Stash:
Git stash is a command that allows you to temporarily save changes you have made in your working directory, without committing them. This is useful when you need to switch to a different branch to work on something else, but you don't want to commit the changes you've made in your current branch yet.
To use Git stash, you first create a new branch and make some changes to it. Then you can use the command git stash to save those changes. This will remove the changes from your working directory and record them in a new stash. You can apply these changes later. git stash list command shows the list of stashed changes.
You can also use git stash drop to delete a stash and git stash clear to delete all the stashes.
Cherry-pick:
Git cherry-pick is a command that allows you to select specific commits from one branch and apply them to another. This can be useful when you want to selectively apply changes that were made in one branch to another.
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.
Resolving Conflicts:
Conflicts can occur when you merge or rebase branches that have diverged, and you need to manually resolve the conflicts before git can proceed with the merge/rebase. git status command shows the files that have conflicts, git diff command shows the difference between the conflicting versions and git add command is used to add the resolved files.
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-
Here are the steps involved:
- Create a new branch called
new_branch
:
git checkout -b new_branch
- Make some changes to the files in the
new_branch
:
echo "This is a new line" >> file.txt
- Save the changes without committing them using the
git stash
command:
git stash
- Switch to the
master
branch:
git checkout master
- Make some changes to the files in the
master
branch and commit them:
echo "This is a new line in master" >> file.txt
git commit -m "Added a new line to file.txt"
- Bring the changes back from the stash and apply them on top of the new commits in the
master
branch using thegit stash pop
command:
git stash pop
This will bring back the changes that you made to the new_branch
and apply them to the master
branch. You should now see the new line that you added to file.txt
in the master
branch.
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-
Here are the steps involved:
- Make sure you are on the development branch.
git checkout development
- Add the new lines to
version01.txt
after the bug fix line.
echo "After bug fixing, this is the new feature with minor alteration" >> version01.txt
- Commit the changes with the message "Added feature2.1 in development branch":
git commit -m "Added feature2.1 in development branch"
- Add another line to
version01.txt
after the previous line.
echo "This is the advancement of previous feature" >> version01.txt
- Commit the changes with the message "Added feature2.2 in development branch":
git commit -m "Added feature2.2 in development branch"
- Add the last line to
version01.txt
after the previous line.
echo "Feature 2 is completed and ready for release" >> version01.txt
- Commit the changes with the message "Feature2 completed":
git commit -m "Feature2 completed"
- Checkout the production branch:
git checkout production
- Run the following command to rebase the development branch onto the production branch:
git rebase development
This will replay the commits from the development branch onto the production branch, in chronological order. The new feature commits will be added to the production branch.
- Push the changes to the remote repository:
git push
Now, all the commits from the development branch, including the new feature commits, will be reflected in the production branch.
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-
Here are the steps involved:
- Make sure you are on the production branch.
git checkout production
- Cherry-pick the commit "Added feature2.2 in development branch":
git cherry-pick <commit-hash>
Replace <commit-hash>
with the hash of the commit you want to cherry-pick. You can find the hash by running the following command:
git log
- Add the new lines to
version01.txt
after the line "This is the advancement of previous feature".
echo "Added few more changes to make it more optimized." >> version01.txt
- Commit the changes with the message "Optimized the feature":
git commit -m "Optimized the feature"
- Push the changes to the remote repository:
git push
Now, the commit "Added feature2.2 in development branch" will be cherry-picked into the production branch, and the new lines will be added to version01.txt
.
I hope this helps! Let me know if you have any other questions.