Stashing in Git
Stashing helps you to move your uncommitted changes to a new location. Simply, it will assist you in tidying up your working tree.
Use Case:
Being a developer we normally worked on different stories. As a normal scenario, at day end we stage our changes to our working directory. Lets we already stage our changed from last 2 days, but still not committed as its still pending. committing modifications to my local repository is awaiting.
Suddenly we got instruction from our senior, “whatever changes you were doing till, Drop it now. We must patch bugs and make emergency changes.”
When you run git status, your working tree should be clean. But there are still some uncommitted changes. Something is in your working tree, directory. Having something in your working directory prevents you from starting something new.
We have 2 Options now: Drop or commit these changes. But Unable to commit because it's pending. Neither we drop our work of last 2 days.
Solution
Stash your last 2 days changes, which are staged but not yet committed to your working directory. On Stage, your uncommitted changes will be moved to a new reserved location, form there you call back it again at any time.
it created a temporary kind of commit for you. It's not committed, exactly, but it's a temporary kind of commit.
Do some changes in develop branch and stage it, but do not commit it
Stash our last change and check status before and after stash
We can see that
- before stash we have uncommitted stash.txt file in working directory
- after stash our working directory is clean and ready for different/ emergency work
Return back you changes from stash area
once your emergency work done, we can revert our changes back from stash area
Check list of stashes so far
Copy this unique key to revert our changes from stash area
This will help you go back to the same state.Now check the status
Delete Stash and All Uncommitted Commits
Check Status now
Event stash.txt is removed