DEV Community

Cover image for What is ๐†๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก
Megha Sharma
Megha Sharma

Posted on

What is ๐†๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก

๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก is a Git command that allows developers to save changes in their working directory without committing them. This is particularly useful when you are in the middle of working on a feature or a bug fix, and you need to switch to another branch, pull changes from a remote repository, or perform other operations that require a clean working directory.

๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก is a powerful command in a developerโ€™s arsenal, especially in dynamic development environments where tasks can change rapidly. It facilitates a smooth workflow by allowing developers to manage and switch between different tasks without the need for unnecessary commits. When used judiciously, git stash contributes to a more organized and efficient version control process in DevOps workflows.

Image description

Suppose a developer is working on a feature in a branch and he needs to pull changes from some other developerโ€™s branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot commit the partial code of the currently working feature. To add this new feature, you have to remove your current changes and store them somewhere else. For this type of situation, Git offers a very useful command known as ๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก. ๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก command saves the previously written code and then returns to the last commit for a fresh start. Now you can add the new feature without disturbing the old one as it is saved locally. After committing to the new feature you can go on with working on the old feature which was incomplete and uncommitted.

๐Ÿ‘‰ ๐Š๐ž๐ฒ ๐”๐ฌ๐ž ๐‚๐š๐ฌ๐ž๐ฌ:

โ€ข ๐“๐ž๐ฆ๐ฉ๐จ๐ซ๐š๐ซ๐ฒ ๐’๐ญ๐จ๐ซ๐š๐ ๐ž:
Developers can stash changes that are not ready to be committed but need to be set aside temporarily. This is common when interrupted by urgent tasks or when transitioning between different tasks.

โ€ข ๐๐ซ๐š๐ง๐œ๐ก ๐’๐ฐ๐ข๐ญ๐œ๐ก๐ข๐ง๐ :
Before switching branches, developers can stash changes to avoid conflicts. This is helpful when working on multiple features simultaneously or when switching to a branch with ongoing development.

โ€ข ๐๐ฎ๐ฅ๐ฅ๐ข๐ง๐  ๐‚๐ก๐š๐ง๐ ๐ž๐ฌ:
Before pulling changes from a remote repository, itโ€™s a good practice to stash local changes to prevent conflicts. After pulling changes, developers can apply the stash to reapply their work.

โ€ข ๐‚๐จ๐ง๐Ÿ๐ฅ๐ข๐œ๐ญ ๐‘๐ž๐ฌ๐จ๐ฅ๐ฎ๐ญ๐ข๐จ๐ง:
During a merge or rebase operation, conflicts may arise. Stashing changes allows developers to resolve conflicts more easily and cleanly.

โ€ข ๐‚๐ซ๐ž๐š๐ญ๐ข๐ง๐  ๐‚๐ฅ๐ž๐š๐ง ๐‚๐จ๐ฆ๐ฆ๐ข๐ญ๐ฌ:
Stashing can be used to create clean, logically separated commits. Developers can stash changes, commit the current state, and then apply the stash to continue working.

Benefits of Mastering Git Stash (Why You Should Care):

โ€ข Enhanced Workflow Efficiency: Seamlessly switch branches or pull updates without worrying about losing your progress. Git Stash ensures a smooth development flow, allowing you to context-switch between tasks effortlessly.

โ€ข Clearer Commit History: By keeping unfinished work out of your commits, you maintain a clean and concise commit history. This makes it easier for you and your teammates to understand the evolution of the codebase.

โ€ข Collaboration Made Easy: Working on a project with a team? Git Stash empowers effective collaboration. You can freely switch to different branches to address bugs or contribute to features without affecting your teammatesโ€™ work.

๐Ÿ‘‰ ๐๐ž๐ฌ๐ญ ๐๐ซ๐š๐œ๐ญ๐ข๐œ๐ž๐ฌ

โ€ข Always stash changes in a clean working directory to avoid conflicts during the stash application.
โ€ข Provide meaningful stash messages for clarity when reviewing stashes later.
โ€ข Regularly check and manage your stashes to avoid unnecessary clutter.

Image description

๐†๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐‚๐จ๐ฆ๐ฆ๐š๐ง๐๐ฌ

โ€ข ๐’๐ญ๐š๐ฌ๐ก ๐‚๐ก๐š๐ง๐ ๐ž๐ฌ
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก Stashes both staged and unstaged changes.

โ€ข ๐’๐ญ๐š๐ฌ๐ก ๐ฐ๐ข๐ญ๐ก ๐š ๐Œ๐ž๐ฌ๐ฌ๐š๐ ๐ž
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฌ๐š๐ฏ๐ž โ€œ๐˜๐จ๐ฎ๐ซ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฆ๐ž๐ฌ๐ฌ๐š๐ ๐žโ€ Stashes changes with a descriptive message.

โ€ข ๐’๐ญ๐š๐ฌ๐ก ๐”๐ง๐ญ๐ซ๐š๐œ๐ค๐ž๐ ๐…๐ข๐ฅ๐ž๐ฌ
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก -๐ฎ ๐จ๐ซ ๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก โ€” ๐ข๐ง๐œ๐ฅ๐ฎ๐๐ž-๐ฎ๐ง๐ญ๐ซ๐š๐œ๐ค๐ž๐ Stashes untracked files along with changes.

โ€ข ๐‹๐ข๐ฌ๐ญ ๐’๐ญ๐š๐ฌ๐ก๐ž๐ฌ
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฅ๐ข๐ฌ๐ญ Lists all stashes youโ€™ve created.

โ€ข ๐€๐ฉ๐ฉ๐ฅ๐ฒ ๐‹๐š๐ญ๐ž๐ฌ๐ญ ๐’๐ญ๐š๐ฌ๐ก
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐š๐ฉ๐ฉ๐ฅ๐ฒ Applies the latest stash to the working directory. The stash is not removed.

โ€ข ๐€๐ฉ๐ฉ๐ฅ๐ฒ ๐š ๐’๐ฉ๐ž๐œ๐ข๐Ÿ๐ข๐œ ๐’๐ญ๐š๐ฌ๐ก
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐š๐ฉ๐ฉ๐ฅ๐ฒ ๐ฌ๐ญ๐š๐ฌ๐ก@{๐ง} Applies the stash with index ๐ง.

โ€ข ๐๐จ๐ฉ ๐‹๐š๐ญ๐ž๐ฌ๐ญ ๐’๐ญ๐š๐ฌ๐ก (๐€๐ฉ๐ฉ๐ฅ๐ฒ ๐š๐ง๐ ๐ƒ๐ซ๐จ๐ฉ)
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฉ๐จ๐ฉ Applies the latest stash and removes it from the stash list.

โ€ข ๐ƒ๐ซ๐จ๐ฉ ๐š ๐’๐ญ๐š๐ฌ๐ก
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐๐ซ๐จ๐ฉ Removes the latest stash without applying it.

โ€ข ๐ƒ๐ซ๐จ๐ฉ ๐š ๐’๐ฉ๐ž๐œ๐ข๐Ÿ๐ข๐œ ๐’๐ญ๐š๐ฌ๐ก
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐๐ซ๐จ๐ฉ ๐ฌ๐ญ๐š๐ฌ๐ก@{๐ง} Removes the stash with index ๐ง.

โ€ข ๐‚๐ฅ๐ž๐š๐ซ ๐€๐ฅ๐ฅ ๐’๐ญ๐š๐ฌ๐ก๐ž๐ฌ
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐œ๐ฅ๐ž๐š๐ซ Removes all stashes.

โ€ข ๐‚๐ซ๐ž๐š๐ญ๐ž ๐š ๐๐ž๐ฐ ๐๐ซ๐š๐ง๐œ๐ก ๐š๐ง๐ ๐€๐ฉ๐ฉ๐ฅ๐ฒ ๐’๐ญ๐š๐ฌ๐ก
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐›๐ซ๐š๐ง๐œ๐ก <๐ง๐ž๐ฐ_๐›๐ซ๐š๐ง๐œ๐ก_๐ง๐š๐ฆ๐ž> Creates a new branch and applies the latest stash to it.

โ€ข ๐’๐ก๐จ๐ฐ ๐‚๐ก๐š๐ง๐ ๐ž๐ฌ ๐ข๐ง ๐š ๐’๐ญ๐š๐ฌ๐ก
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฌ๐ก๐จ๐ฐ -๐ฉ ๐ฌ๐ญ๐š๐ฌ๐ก@{๐ง} Shows the changes in a specific stash.

โ€ข ๐€๐ฉ๐ฉ๐ฅ๐ฒ ๐’๐ญ๐š๐ฌ๐ก ๐ฐ๐ข๐ญ๐ก ๐€๐ฎ๐ญ๐จ-๐Œ๐ž๐ซ๐ ๐ž
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐š๐ฉ๐ฉ๐ฅ๐ฒ โ€” 3๐ฐ๐š๐ฒ ๐ฌ๐ญ๐š๐ฌ๐ก@{๐ง} Attempts to apply the stash using a 3-way merge.

โ€ข ๐’๐ญ๐š๐ฌ๐ก ๐Ž๐ง๐ฅ๐ฒ ๐”๐ง๐ญ๐ซ๐š๐œ๐ค๐ž๐ ๐…๐ข๐ฅ๐ž๐ฌ
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฌ๐š๐ฏ๐ž โ€” ๐ข๐ง๐œ๐ฅ๐ฎ๐๐ž-๐ฎ๐ง๐ญ๐ซ๐š๐œ๐ค๐ž๐ Stashes only untracked files.

โ€ข ๐’๐ญ๐š๐ฌ๐ก ๐Š๐ž๐ž๐ฉ๐ข๐ง๐  ๐ˆ๐ง๐๐ž๐ฑ ๐‚๐ก๐š๐ง๐ ๐ž๐ฌ
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฌ๐š๐ฏ๐ž โ€” ๐ค๐ž๐ž๐ฉ-๐ข๐ง๐๐ž๐ฑ Stashes changes but keeps changes in the index.

โ€ข ๐’๐ญ๐š๐ฌ๐ก ๐‚๐ก๐š๐ง๐ ๐ž๐ฌ ๐ข๐ง๐ญ๐ž๐ซ๐š๐œ๐ญ๐ข๐ฏ๐ž๐ฅ๐ฒ
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฌ๐š๐ฏ๐ž -๐ฉ Allows you to interactively choose which changes to stash.

โ€ข ๐€๐ฉ๐ฉ๐ฅ๐ฒ ๐’๐ญ๐š๐ฌ๐ก ๐š๐ฌ ๐š ๐Œ๐ž๐ซ๐ ๐ž
๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐š๐ฉ๐ฉ๐ฅ๐ฒ -๐ฆ <๐ฆ๐ž๐ซ๐ ๐ž_๐ฉ๐š๐ซ๐ž๐ง๐ญ_๐ง๐ฎ๐ฆ๐›๐ž๐ซ> Applies the stash as a merge. Useful when the stash represents changes from a different branch.

Top comments (0)