Git Stash, Cherry-Pick, Tags, and Branch vs Tag
1. What is git stash, when would you use it, and what is the difference between stash apply and stash pop?
What is git stash?
git stash temporarily saves your uncommitted changes so that you can work with a clean working directory without creating a commit.
In simple terms:
Stash = temporarily put my unfinished changes aside and restore them later.
When Would You Use It?
Suppose you are working on:
feature/login
You have modified several files, but the work is incomplete.
Suddenly, you need to switch to main to fix an urgent issue.
Git may prevent or complicate switching because of your local changes.
Instead of making an incomplete commit:
git stash
Now your changes are temporarily stored.
You can switch branches:
git switch main
After completing the urgent work, return:
git switch feature/login
Restore your changes:
git stash pop
Simple Flow
Working on feature/login
↓
Uncommitted changes
↓
git stash
↓
Working directory becomes clean
↓
Switch to another branch
↓
Complete other work
↓
Return to feature/login
↓
git stash pop
↓
Continue your unfinished work
Useful Stash Commands
Save changes:
git stash
Save with a meaningful description:
git stash push -m "Login page changes"
View stashes:
git stash list
Example:
stash@{0}: On feature/login: Login page changes
stash@{1}: On feature/payment: Payment validation
Restore a stash:
git stash apply
or:
git stash pop
Delete a stash:
git stash drop stash@{0}
git stash apply vs git stash pop
The important difference is what happens to the stash after successful restoration.
git stash apply
git stash apply
Restores the changes but keeps the stash in the stash list.
Stash
↓
Apply changes
↓
Working directory
Stash still exists
Useful when you may want to apply the same stash again.
git stash pop
git stash pop
Restores the changes and, if successfully applied, removes that stash from the stash list.
Stash
↓
Restore changes
↓
Remove stash
Comparison
stash apply |
stash pop |
|---|---|
| Restores changes | Restores changes |
| Keeps stash | Removes successfully applied stash |
| Can reuse stash | Normally used once |
| Safer when you want to retain a backup | Convenient when stash is no longer needed |
Key Points
git stashtemporarily stores uncommitted work.- Useful when you need to quickly switch branches.
git stash listdisplays saved stashes.git stash applyrestores while retaining the stash.git stash poprestores and normally removes the stash.- By default, untracked files are not included; use
git stash -uwhen you need to include them. - Stash is intended for temporary work, not as a replacement for normal commits.
2. What is git cherry-pick, and when would you use it?
What is git cherry-pick?
git cherry-pick takes a specific commit from another branch and applies its changes to your current branch as a new commit.
In simple terms:
Cherry-pick = take only the commit I need instead of merging the entire branch.
Simple Example
Suppose:
main
A --- B
feature
A --- B --- C --- D --- E
You only need the bug fix contained in commit D.
First switch to main:
git switch main
Then:
git cherry-pick <commit-id>
For example:
git cherry-pick a1b2c3d
Conceptually:
feature
A --- B --- C --- D --- E
|
| cherry-pick D
↓
main
A --- B -------- D'
D' contains the changes from D, but it is a new commit with a new commit hash.
When Would You Use Cherry-Pick?
Suppose a developer fixes an important production bug in another branch.
That branch also contains unfinished features:
feature/payment
|
├── New payment UI
├── New reporting
└── Critical security fix
You don't want all those changes in main.
You only want:
Critical security fix
Find its commit:
git log --oneline
Then:
git switch main
git cherry-pick abc123
Only that commit's changes are applied.
Cherry-Pick Conflict
If there is a conflict:
git cherry-pick abc123
Resolve the files and stage them:
git add .
Continue:
git cherry-pick --continue
Cancel:
git cherry-pick --abort
Key Points
- Cherry-pick applies a specific commit.
- It does not merge the entire branch.
- The cherry-picked commit gets a new commit hash.
- Useful for targeted bug fixes or selected changes.
- Avoid excessive cherry-picking when a normal merge is more appropriate.
- Conflicts can occur during cherry-pick.
3. What is a Git tag, and how is it used for application releases?
What is a Git Tag?
A Git tag is a named reference used to mark a specific commit, commonly an important point in the project's history.
Tags are frequently used to identify application releases.
For example:
v1.0.0
v1.1.0
v2.0.0
Suppose:
A --- B --- C --- D --- E
↑ ↑
v1.0.0 v2.0.0
Here:
C → Version 1.0.0
E → Version 2.0.0
The tags make those release commits easy to identify.
Simple Syntax
Create a tag:
git tag v1.0.0
View tags:
git tag
Example:
v1.0.0
v1.1.0
v2.0.0
Annotated Tag
For releases, an annotated tag is commonly preferred:
git tag -a v1.0.0 -m "Release version 1.0.0"
It stores additional metadata such as the tagger, date, and message.
Push a Tag
Creating a tag locally does not automatically push it to the remote repository.
Push one tag:
git push origin v1.0.0
Push all local tags:
git push origin --tags
How Tags Are Used for Releases
Suppose your application is ready for production.
Your history is:
A --- B --- C --- D
↑
main
Create a release tag:
git tag -a v1.0.0 -m "Production release 1.0.0"
Now:
A --- B --- C --- D
↑
main
v1.0.0
Push it:
git push origin v1.0.0
A CI/CD pipeline can then be configured to deploy or package the application when a release tag such as v1.0.0 is created.
For example:
Development complete
↓
Merge into main
↓
Testing completed
↓
Create v1.0.0 tag
↓
Push tag
↓
CI/CD pipeline
↓
Production release
Key Points
- A tag identifies a specific point in Git history.
- Tags are commonly used for application versions/releases.
- Common version names are
v1.0.0,v1.1.0, etc. - Tags normally remain pointing at the same commit.
- Annotated tags are useful for formal releases.
- Tags must normally be pushed separately to the remote.
4. What is the difference between a branch and a tag?
Both branches and tags are references to Git commits, but their purposes are different.
Branch
A branch moves forward automatically as you make new commits on that branch.
Initially:
A --- B
↑
main
Create another commit:
A --- B --- C
↑
main
The main branch moved automatically from B to C.
Tag
A tag normally stays attached to the commit where it was created.
Suppose:
A --- B
↑
v1.0.0
Then you create more commits:
A --- B --- C --- D
↑ ↑
v1.0.0 main
main moves forward, but:
v1.0.0
still points to B.
This makes tags suitable for identifying fixed releases.
Branch vs Tag
| Branch | Tag |
|---|---|
| Used for development | Used to mark a specific commit |
| Moves when new commits are made | Normally remains fixed |
Examples: main, feature/login |
Examples: v1.0.0, v2.0.0 |
| Used for ongoing work | Commonly used for releases |
| You normally commit while on a branch | Tag marks an existing commit |
Simple Visualization
v1.0.0
↓
A --- B --- C --- D --- E --- F
↑
main
Later another commit is created:
v1.0.0
↓
A --- B --- C --- D --- E --- F --- G
↑
main
Notice:
main → moved from F to G
v1.0.0 → stayed at D
Key Points
- A branch represents ongoing development.
- A tag marks a specific point in history.
- Branch references move as new commits are added.
- Tags normally stay fixed.
- Branches are commonly used for features, fixes, and development.
- Tags are commonly used to identify releases such as
v1.0.0.
Interview Summary
git stash
→ Temporarily saves unfinished work
git stash apply
→ Restore and keep the stash
git stash pop
→ Restore and remove the successfully applied stash
git cherry-pick <commit>
→ Apply one specific commit to the current branch
git tag -a v1.0.0 -m "Release v1.0.0"
→ Mark a release commit
Branch
→ Moves with development
Tag
→ Normally remains fixed at a specific commit