
Git Stash:
Purpose: Temporarily saves changes in your working directory without committing them.
Use Case: When switching to a different branch but not ready to commit current changes.
Commands:
git stash: Saves changes and removes them from the working directory.git stash list: Shows a list of stashed changes.git stash drop: Deletes a specific stash.git stash clear: Deletes all stashes.
Cherry-pick:
Purpose: Selectively applies specific commits from one branch to another.
Use Case: When you want to bring specific changes from one branch into another.
Command:
git cherry-pick <commit_hash>: Applies the specified commit to the current branch.
Resolving Conflicts:
Cause: Occurs during merges or rebases when conflicting changes exist between branches.
Manual Resolution:
git status: Shows files with conflicts.git diff: Displays differences between conflicting versions.git add: Marks resolved files as ready for commit.
Let's practice these essential Git concepts.
Task 1: Stashing and Reverting
Objective: Stash changes, switch branches, and then apply the stashed changes back.
Create a New Branch:
Create a branch named “feature-branch” from the existing “master” branch.
Use the following command:
git checkout -b feature-branch
Add a File and Stash Changes:
Inside the newly created branch, add a text file named
feature.txt.Write some content in the file (e.g., “This is a new feature!”).
Commit your changes with an appropriate message (e.g., “Added feature to feature-branch”).
Realize there’s a bug in your feature. The content should have been “This is a fantastic feature!” instead.
Stash the Changes:
Use
git stashto temporarily save your changes without committing them.Switch to a different branch (e.g., “dev” or “bug-fix”) to work on something else.
Apply the Stashed Changes:
Switch back to the “feature-branch.”
Use
git stash applyorgit stash popto bring back your stashed changes.Verify that the content in
feature.txtis now “This is a fantastic feature!”
Push Your Branch:
- Push your local branch to the remote repository (e.g., GitHub).
Task 2: Cherry-pick and Conflict Resolution
Objective: Cherry-pick commits from one branch to another and resolve any conflicts.
Create Two Feature Branches:
Create two branches: “feature1” and “feature2.”
In each branch, add a file (e.g.,
feature1.txtandfeature2.txt) with some content.Commit your changes in both branches.
Cherry-pick Feature1 into Master:
Switch back to the “master” branch.
Cherry-pick the changes from “feature1” into “master”:
git cherry-pick <commit_hash>Resolve any conflicts if they occur.
Rebase Feature2 onto Master:
Still in the “master” branch, rebase “feature2” onto it:
git checkout feature2 git rebase masterAgain, resolve any conflicts.
Observe the Differences:
Compare the commit history and logs after cherry-picking and rebasing.
Note how cherry-picking creates individual commits, while rebasing keeps a linear history.
Thank you for reading😉.



