How to use the command 'git stash' (with examples)

How to use the command 'git stash' (with examples)

Git stash is a useful command that allows you to temporarily save your local changes in a separate area, without committing them. This is helpful when you need to switch to a different branch or perform other tasks without losing your current changes. The git stash command provides various options for stashing and managing your changes.

Use case 1: Stash current changes, except new (untracked) files

Code:

git stash push -m optional_stash_message

Motivation:

  • You have made some changes to your codebase and want to temporarily save them before switching to a different branch.
  • You do not want to stash any new untracked files.

Explanation:

  • push: Pushes the stash onto the stash stack.
  • -m optional_stash_message: Specifies an optional message for the stash.

Example output:

Saved working directory and index state WIP on feature/branch: optional_stash_message

Use case 2: Stash current changes, including new (untracked) files

Code:

git stash -u

Motivation:

  • Similar to the previous use case, you want to stash your changes before switching branches.
  • You also want to include any new untracked files in the stash.

Explanation:

  • -u: Stashes both tracked and untracked files.

Example output:

Saved working directory and index state WIP on feature/branch: optional_stash_message

Use case 3: Interactively select parts of changed files for stashing

Code:

git stash -p

Motivation:

  • You have made changes to multiple files and want to selectively stash only parts of those changes.
  • This can be useful when you want to separate unrelated changes from each other.

Explanation:

  • -p: Allows you to choose which changes to include in the stash. Git will interactively prompt you to select individual hunk changes.

Example output:

diff --git a/file1.txt b/file1.txt
index abc123..def456 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,3 @@
-Hello
+Hi
 World

Use case 4: List all stashes

Code:

git stash list

Motivation:

  • You want to see a list of all the stashes you have created.
  • This allows you to keep track of your saved changes.

Explanation:

  • list: Displays a list of all stashes, including the stash name, related branch, and message.

Example output:

stash@{0}: On feature/branch: optional_stash_message
stash@{1}: On feature/branch: another_stash_message

Use case 5: Show the changes as a patch between the stash and the commit back when stash entry was first created

Code:

git stash show -p stash@{0}

Motivation:

  • You want to see the changes made to a specific stash in detail.
  • This can help you understand the changes before applying or removing the stash.

Explanation:

  • show: Shows the changes made in the stash. By default, it shows the latest stash, but you can specify a particular stash using the stash name.
  • -p: Displays the changes as a patch.

Example output:

diff --git a/file1.txt b/file1.txt
index abc123..def456 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,3 @@
-Hello
+Hi
 World

Use case 6: Apply a stash

Code:

git stash apply {{optional_stash_name_or_commit}}

Motivation:

  • You want to apply a specific stash to your current working directory.
  • This allows you to bring back the saved changes.

Explanation:

  • apply: Applies the changes from the specified stash to the current working directory. By default, it applies the latest stash, but you can specify a particular stash using the stash name or commit.

Example output:

On branch feature/branch
Changes not staged for commit:
modified:   file1.txt

Use case 7: Drop or apply a stash and remove it from the stash list if applying doesn’t cause conflicts

Code:

git stash pop {{optional_stash_name}}

Motivation:

  • You want to apply a specific stash to your current working directory, and if there are no conflicts, remove it from the stash list.
  • This is useful when you no longer need the stash and want a clean stash list.

Explanation:

  • pop: Applies the changes from the specified stash to the current working directory and removes the stash from the stash list if applying doesn’t cause conflicts.

Example output:

On branch feature/branch
Changes not staged for commit:
modified:   file1.txt

Use case 8: Drop all stashes

Code:

git stash clear

Motivation:

  • You want to remove all stashes from the stash list.
  • This is useful when you no longer need any of the saved changes.

Explanation:

  • clear: Removes all stashes from the stash list.

Example output:

No output, the stash list is empty.

Related Posts

Vifm (VI File Manager) Examples (with examples)

Vifm (VI File Manager) Examples (with examples)

Vifm is a command-line file manager that allows users to navigate and manage files and directories using the VI text editor keybindings.

Read More
How to use the command adb (with examples)

How to use the command adb (with examples)

The command adb stands for Android Debug Bridge and allows users to communicate with an Android emulator instance or connected Android devices.

Read More
How to use the command rhash (with examples)

How to use the command rhash (with examples)

The rhash command is a utility that allows users to calculate or check common message digests.

Read More