Skip to content

Commit

Permalink
Merge stashing snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed Apr 14, 2024
1 parent ed6a797 commit 0ae77a0
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 128 deletions.
10 changes: 7 additions & 3 deletions content/languages/git.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,20 @@ references:
https://git-scm.com/docs/git-shortlog
git stash apply: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-apply--index-q--quietltstashgt
git stash apply <stash>: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-apply--index-q--quietltstashgt
git stash clear: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-clear
git stash drop <stash>: >-
git stash drop: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-drop-q--quietltstashgt
git stash list: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-listltlog-optionsgt
git stash save: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-save-p--patch-S--staged-k--no-keep-index-u--include-untracked-a--all-q--quietltmessagegt
git stash push: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-push-p--patch-S--staged-k--no-keep-index-u--include-untracked-a--all-q--quiet-m--messageltmessagegt--pathspec-from-fileltfilegt--pathspec-file-nul--ltpathspecgt82308203
git stash pop: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-pop--index-q--quietltstashgt
git stash show: >-
https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-show-u--include-untracked--only-untrackedltdiff-optionsgtltstashgt
git status: >-
https://git-scm.com/docs/git-status
git submodule add <upstream-path> <local-path>: >-
Expand Down
18 changes: 18 additions & 0 deletions content/redirects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2794,3 +2794,21 @@
- from: /git/s/automatic-find-commit-with-bug
to: /git/s/find-commit-with-bug
status: 301!
- from: /git/s/apply-latest-stash
to: /git/s/stashing
status: 301!
- from: /git/s/apply-stash
to: /git/s/stashing
status: 301!
- from: /git/s/delete-stash
to: /git/s/stashing
status: 301!
- from: /git/s/delete-stashes
to: /git/s/stashing
status: 301!
- from: /git/s/list-stashes
to: /git/s/stashing
status: 301!
- from: /git/s/save-stash
to: /git/s/stashing
status: 301!
19 changes: 0 additions & 19 deletions content/snippets/git/s/apply-latest-stash.md

This file was deleted.

19 changes: 0 additions & 19 deletions content/snippets/git/s/apply-stash.md

This file was deleted.

19 changes: 0 additions & 19 deletions content/snippets/git/s/delete-stash.md

This file was deleted.

20 changes: 0 additions & 20 deletions content/snippets/git/s/delete-stashes.md

This file was deleted.

20 changes: 0 additions & 20 deletions content/snippets/git/s/list-stashes.md

This file was deleted.

28 changes: 0 additions & 28 deletions content/snippets/git/s/save-stash.md

This file was deleted.

109 changes: 109 additions & 0 deletions content/snippets/git/s/stashing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
title: A guide to Git stashing
shortTitle: Git stashing
type: story
language: git
tags: [repository,stash]
cover: purple-leaves
excerpt: Learn all you need to know about Git stashing, including how to stash changes, apply, list, and delete stashes.
dateModified: 2024-04-05
---

Git's stashing feature allows you to save your local changes **temporarily** and switch to another branch or commit. It is useful when you want to work on a different task without committing your current changes. Let's explore how to use Git stashing effectively.

## Stashing changes

In order to stash your changes, you can use the `git stash push` command. This command saves the **current state** of the working directory and index into a new stash. You can optionally provide a message for the stash, and include **untracked files** using the `-u` option.

```shell
# Usage: git stash push [-u] [<message>]

git stash push
# Creates a new stash

git stash push -u
# Creates a new stash, including untracked files

git stash push "Bugfix WIP"
# Creates a new stash with the message "Bugfix WIP"
```

> [!NOTE]
>
> You can **omit** the `push` subcommand when using `git stash`. Additionally, you might have seen `git stash save` being used in **older versions** of Git. This command is now **deprecated** in favor of `git stash push`.
## Listing stashes

Stashes are stored as a **stack**, with the most recent stash at the top. You can view a list of all stashes using the `git stash list` command. This command displays the stash reference, the branch or commit the stash was created on, and the message associated with the stash.

```shell
# Usage: git stash list

git stash list
# stash@{0}: WIP on patch-1: ee52eda Fix network bug
# stash@{1}: WIP on master: 2b1e8a7 Add new feature
```

## Viewing stash changes

To view the changes stored in a stash, you can use the `git stash show` command. This command displays the changes introduced by the stash relative to the parent commit. You can provide the **stash reference** to view a specific stash, or omit it to view the latest stash.

```shell
# Usage: git stash show [<stash>]

git stash show
# Displays the changes in the latest stash

git stash show stash@{1}
# Displays the changes in the stash with the reference stash@{1}
```

## Applying a stash

To apply a specific stash, you can use the `git stash apply` command. This command applies the changes from the specified stash to the working directory. You can provide the **stash reference** to apply a specific stash, or omit it to apply the **latest stash**.

```shell
# Usage: git stash apply [<stash>]

git stash apply
# Applies the latest stash

git stash apply stash@{1}
# Applies the stash with the reference stash@{1}
```

Similarly, you can use the `git stash pop` command to apply the changes from the specified stash and **remove it from the stash list**.

```shell
# Usage: git stash pop [<stash>]

git stash pop
# Applies the latest stash and removes it from the stash list

git stash pop stash@{1}
# Applies the stash with the reference stash@{1} and
# removes it from the stash list
```

## Deleting stashes

You can **delete stashes** using the `git stash drop` command. This command removes the specified stash from the stash list.

```shell
# Usage: git stash drop <stash>

git stash drop
# Deletes the latest stash

git stash drop stash@{1}
# Deletes the stash with the reference stash@{1}
```

Moreover, you can **delete all stashes** using the `git stash clear` command.

```shell
# Usage: git stash clear

git stash clear
# Deletes all stashes
```

0 comments on commit 0ae77a0

Please sign in to comment.