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-1️⃣
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.
➡️ # Switch to the main branch (assuming it's named 'main' or 'master')
git checkout main
# Create and switch to a new branch (replace 'new-branch' with your desired branch name)
git checkout -b new-branch
# Make changes in the new branch
# Stash changes without committing
git stash
# Switch to a different branch (replace 'other-branch' with the branch you want to switch to)
git checkout other-branch
# Make changes and commit them
git commit -m "Your commit message"
# Pop changes from the stash and apply them on top of the new comm
its
git stash pop
Task-2️⃣
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 .
➡️ # Switch to the development branch
git checkout development
# Edit version01.txt to add the required lines after the specified text
# Commit feature 2.1
git add version01.txt
git commit -m "Added feature2.1 in development branch"
# Edit version01.txt to add the next lines
# Commit feature 2.2
git add version01.txt
git commit -m "Added feature2.2 in development branch"
# Edit version01.txt to add the final lines
# Commit feature 2 completion
git add version01.txt
git commit -m "Feature2 completed"
# Switch to the production branch (assuming it's derived from the master branch)
git checkout production
# Rebase production branch to include the latest changes from development
git rebase development
Task-3️⃣
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
➡️# Switch to the production branch
git checkout production
# Cherry-pick the specific commit from the development branch
git cherry-pick <commit-hash-of-"Added feature2.2 in development branch">
# Edit version01.txt to add the required lines after "This is the advancement of previous feature"
# Commit optimized feature
git add version01.txt
git commit -m "Optimized the
feature"
✅Explanation:
Task-01 involves creating a new branch, making changes, stashing them, switching to another branch, making changes, and then popping the stashed changes.
Task-02 includes adding specific lines to a file in the development branch, committing them with appropriate messages, and then rebasing the production branch to include those changes.
Task-03 uses cherry-pick to bring a specific commit from development into the production branch and adds additional lines to the file, committing the optimized feature.