Git Tutorial: Using the Stash Command

Corey Schafer
17 Apr 201510:32

Summary

TLDRThis video tutorial explores the Git stash command, a useful tool for temporarily saving changes when you're not ready to commit them. It demonstrates how to use 'git stash save' to store modifications, 'git stash list' to view stashes, and 'git stash apply' or 'git stash pop' to reapply changes. The video also covers 'git stash drop' to remove stashes and 'git stash clear' to delete all stashes. Practical examples are provided, such as switching branches without committing and handling uncommitted changes across branches.

Takeaways

  • ๐Ÿ“š The git stash command is useful for temporarily saving changes that are not ready to be committed.
  • ๐Ÿ”„ Git stash allows you to switch branches without committing changes, and revert to the previous state of the working directory.
  • ๐Ÿ’พ Stashed changes are saved with a unique ID and a descriptive message to help you remember what was stashed.
  • ๐Ÿ” You can view the list of stashes with 'git stash list', which shows the stash ID and the message associated with each stash.
  • ๐Ÿ”™ To reapply stashed changes, you can use 'git stash apply' followed by the stash ID, which reapplies the changes but does not remove the stash.
  • ๐Ÿ“‘ 'git stash pop' is used to apply the most recent stash and then remove it from the stash list.
  • ๐Ÿ—‘๏ธ 'git stash drop' can be used to remove a specific stash from the stash list, freeing up space and cleaning up the stash history.
  • ๐Ÿงผ 'git stash clear' is a command to remove all stashes at once, which is useful for discarding all temporary changes.
  • ๐Ÿ”€ Stashing is particularly helpful when you need to switch branches but have uncommitted changes that you don't want to commit on the current branch.
  • ๐Ÿ› ๏ธ The script provides a practical example of using git stash to work on a Python file, demonstrating how to save, apply, and drop stashes.

Q & A

  • What is the primary purpose of the git stash command?

    -The git stash command is used to temporarily save changes made to tracked files in a dirty working directory, allowing you to switch branches or revert to a clean state without committing the changes.

  • How can you create a stash in Git?

    -You can create a stash by using the command `git stash save` followed by a descriptive message to remind yourself of what the stash contains.

  • What happens to your working directory after stashing changes?

    -After stashing changes, your working directory is cleaned up, and the changes are no longer visible in the file system, but they are saved in a stash.

  • How can you view the list of stashes you have created?

    -You can view the list of stashes by using the command `git stash list` which shows all the stashes along with their IDs and messages.

  • What is the difference between `git stash apply` and `git stash pop`?

    -`git stash apply` re-applies the changes from a stash to the working directory but keeps the stash in the stash list, whereas `git stash pop` both reapplies the changes and removes the stash from the list.

  • How can you remove a specific stash in Git?

    -You can remove a specific stash by using the command `git stash drop` followed by the stash's ID or index number.

  • What does `git stash clear` do?

    -`git stash clear` is used to remove all stashes from the stash list, effectively discarding all the changes that were stashed.

  • How can you apply the most recent stash and remove it from the list?

    -You can apply the most recent stash and remove it from the list by using the command `git stash pop`.

  • What is a common use case for `git stash` when switching branches?

    -A common use case for `git stash` when switching branches is when you have uncommitted changes on one branch and need to switch to another branch without committing or discarding those changes.

  • How can you add a new function to your code and stash the changes before switching branches?

    -You can add a new function to your code, then use `git stash save` with a message to stash the changes, and then switch branches using `git checkout`.

Outlines

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Mindmap

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Keywords

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Highlights

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Transcripts

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now
Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Git StashVersion ControlCode ManagementGit TutorialSoftware DevelopmentCoding TipsGit CommandsBranching StrategyDeveloper ToolsCode Stashing