Git Stash: A Beginner's Guide Git Checkout Explained

By Thomas

Understanding Git Stash and Git Checkout

Git Stash
Git Stash

In the realm of version control, Git offers a plethora of commands to manage your repository effectively. Two of these commands, 'git stash' and 'git checkout', serve distinct purposes and are often used in tandem to keep your working directory clean and your development process organized. Let's delve into what each command does and how they can help streamline your Git workflow.

25 GIT COMMANDS WITH EXAMPLES
25 GIT COMMANDS WITH EXAMPLES

Before we dive into the details, let's ensure we're on the same page. A 'stash' in Git refers to a list of untracked changes that can be temporarily shelved and reapplied later. On the other hand, 'checkout' is used to switch between different branches or restore working tree files from the latest commit.

Git Stash: Temporarily Shelve Changes

What is Git? (Day 1 Beginner Guide)
What is Git? (Day 1 Beginner Guide)

Imagine you've been working on a feature branch, and you suddenly receive a bug report that requires immediate attention. However, you don't want to commit yourhalf-done work or lose your progress. This is where 'git stash' comes to the rescue. It allows you to temporarily put aside your changes, clean up your working directory, and switch to another branch without losing your progress.

But what happens to your stashed changes? Git moves them to a hidden .git directory, where they remain until you're ready to reapply them. Here's how to use 'git stash':

Git Stash | W3Docs Online Git Tutorial
Git Stash | W3Docs Online Git Tutorial

Basic Git Stash Command

  • git stash: Stashes only the untracked changes (files you haven't added with git add).
  • git stash save [message]: Stashes the changes and allows you to include a descriptive message.
  • git stash list: Lists all the stashed changes with an index number.
  • git stash apply [stash@{num}]: Applies the stashed changes identified by a number. The stashed changes remain in the stash list.
  • git stash pop [stash@{num}]: Applies the stashed changes and removes them from the stash list.
  • git stash clear: Removes all the stored stash entries.
  • What Gets Stashed and What Doesn't?

    the git branching strategy for real devops projects infographical graphic by
    the git branching strategy for real devops projects infographical graphic by

    When you stash changes, Git packs up your modified tracked files, as well as any untracked files. However, deleted files and changes to ignored files are not stashed. If you want to stash everything, you can use git stash --include-untracked.

    Git Checkout: Switch Branches and Restore Files

    Once you've stashed your changes, it's time to switch to the bug-fixing branch using 'git checkout'. This command moves your HEAD (the current branch reference) to another branch, updating your working directory and staging area to match the_step_with_commit_time modalities of that branch.

    the git essentials info sheet is displayed
    the git essentials info sheet is displayed

    However, 'git checkout' has a caveat. If there are uncommitted changes in your working directory, Git won't allow you to switch branches. This is where 'git stash' comes in handy. By stashing your changes first, you ensure a clean working directory for switching branches.

    Basic Git Checkout Command

    the git - gitthub checklist is shown in black and white
    the git - gitthub checklist is shown in black and white
    What is Git? A Beginner's Guide to Version Control | Git Basics Explained
    What is Git? A Beginner's Guide to Version Control | Git Basics Explained
    Git & GitHub Commands Cheat Sheet 💻 | Essential Developer Guide
    Git & GitHub Commands Cheat Sheet 💻 | Essential Developer Guide
    Git Stash Basics
    Git Stash Basics
    Git for Beginners: Version Control Every Developer Needs
    Git for Beginners: Version Control Every Developer Needs
    Git
    Git
    Essential Git Commands Cheat Sheet for Beginners | Save This Coding Reference
    Essential Git Commands Cheat Sheet for Beginners | Save This Coding Reference
    🚀 Git Commands Every Beginner Should Memorize
    🚀 Git Commands Every Beginner Should Memorize

    • git checkout [branch-name]: Switches to the specified branch. If the branch doesn't exist, Git creates a new branch and switches to it.
    • git checkout -b [branch-name]: Creates a new branch and switches to it in one step.
    • git checkout -- [file]: Restores the specified file from the latest commit.

    Using Git Checkout for File Restoration

    In addition to switching branches, 'git checkout' can also be used to restore files from the latest commit. This can be useful if you've accidentally modified a file and want to revert it to its committed state. Simply specify the file name after the '--' flag.

    Combining Git Stash and Git Checkout for Seamless Workflow

    The real power of 'git stash' and 'git checkout' lies in their combined use. Here's a common workflow:

    1. Stash your changes with git stash or git stash save [message] (if you want to include a message).
    2. Switch to the desired branch with git checkout [branch-name].
    3. Once you're done with the other branch, switch back with git checkout [your-original-branch].
    4. Apply your stashed changes with git stash pop or restore any specific stash with git stash apply [stash@{num}].

    And that's it! With 'git stash' and 'git checkout', you can maintain a clean workspace, efficiently switch between branches, and prevent lost work. Embrace these tools to elevate your Git game and streamline your development process.

    As you continue your Git journey, remember that practice makes perfect. experiment with these commands, and you'll soon find them becoming second nature. Happy coding!