How to Use the Git Stash Command

发布时间:2023年12月30日

The git stash command is used to temporarily save changes that you have made in your working directory but do not want to commit yet. It allows you to switch to a different branch or apply changes from another commit without committing the current changes. The basic syntax of git stash is as follows:

git stash

Here’s a step-by-step explanation of how git stash works:

  1. Suppose you have made some changes in your working directory, but you are not ready to commit them yet.

  2. To save these changes, you can run the following command:

    git stash
    
  3. Git will save the changes in a new stash and revert your working directory to the state of the last commit. The stash will be stored in a stack of stashes.

  4. You can switch to a different branch or apply changes from another commit without committing the changes you stashed.

  5. To apply the stashed changes back to your working directory, you can use the following command:

    git stash apply
    

    This command applies the most recent stash to your working directory, but it does not remove the stash from the stack. If you have multiple stashes, you can specify a specific stash to apply by using git stash apply stash@{n}, where n is the index of the stash.

  6. If you want to remove the stash from the stack after applying it, you can use the following command:

    git stash drop
    

    This command removes the most recent stash from the stack. Again, if you have multiple stashes, you can specify a specific stash to drop by using git stash drop stash@{n}.

  7. If you want to apply and remove the most recent stash in one command, you can use the following command:

    git stash pop
    

    This command is equivalent to running git stash apply followed by git stash drop.

The git stash command is useful when you want to temporarily save changes and switch to a different branch or apply changes from another commit without committing the current changes. It allows you to work on different tasks or switch contexts without losing your work.

文章来源:https://blog.csdn.net/yuguo_im/article/details/135299193
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。