Skip to main content

stash

Git stash push -m 'name' --include-untracked

Git Shelf and Git Stash are two different mechanisms in Git for temporarily storing changes that are not ready to be committed. While they serve a similar purpose, they have some key differences in their functionality. Git Stash is used to temporarily save changes within a single working tree, while Git Shelf (Git Worktree) allows you to have multiple working trees representing different branches or commits. The choice between them depends on your specific use case and the level of isolation you require for your changes.

git stash

Git Stash allows you to save your modifications in a temporary area and revert your working directory to a clean state. It is useful when you want to switch branches but have uncommitted changes that you don't want to commit yet. Here's how it works:

  • To stash your changes, you run git stash command. Git will save your modifications, including both staged and unstaged changes, and revert your working directory to the most recent commit.

  • You can apply your stashed changes later by running git stash apply or git stash pop commands. The former applies the changes while keeping the stash, while the latter applies the changes and removes the stash.

  • Git Stash allows you to have multiple stashes and provides commands to manage them, such as git stash list, git stash drop, and git stash clear.

    Stashing is useful for quickly switching branches, trying out different changes, or temporarily putting aside your work.

this is how to store local changes before SWITCHING BRANCHES

  • untracked files
  • stashes all files, including untracked and ignored files. no longer touches ignored files
git stash --include-untracked
  • discard local changes
git reset --hard

stash only 1 file

git stash push path/to/file

Name & Retrieve Stash

This is how you do it:

git stash push -m "my_stash"

Where "my_stash" is the stash name.

add this flag in case you created new files

--include-untracked

Some more useful things to know: All the stashes are stored in a stack. Type:

git stash list

This will list down all your stashes.

To apply a stash and remove it from the stash stack, type:

git stash pop stash@{n}

To apply a stash and keep it in the stash stack, type:

git stash apply stash@{n}

Where n is the index of the stashed change.

Git Shelf (Git Worktree)

Git Shelf, also known as Git Worktree, allows you to have multiple working trees within a single git repository. Each working tree represents a different branch or commit, and you can make changes in each working tree independently without affecting the others. Here's how it works:

  • To create a new working tree, you run git worktree add <path> <branch/commit>. This command sets up a separate working directory at the specified path, which is associated with the specified branch or commit.
  • You can then switch to the newly created working tree using cd <path> and perform any operations within that context, such as making changes, committing, and pushing.
  • Git Shelf allows you to have multiple working trees simultaneously, each representing a different branch or commit.
  • You can remove a working tree using git worktree remove <path>.

Git Shelf is useful when you want to work on multiple branches or commits simultaneously without having to switch back and forth between them. It provides a more isolated environment for making changes and testing different scenarios.