Git Stash: a powerful tool for temporarily saving and switching work status

Git is an indispensable version control system in our daily work. It provides many powerful features, one of which is Git Stash. Git Stash can help us temporarily save current modifications so that they can be reapplied later when switching branches or saving unfinished work. This article will introduce the purpose of Git Stash, common scenarios and how to use this feature.

Git Stash application

Git Stash is very versatile and is especially suitable for the following common scenarios:

  1. **Branch switching:** When you are developing on a certain branch but need to urgently fix bugs in other branches or perform other tasks, you can use Git Stash to save the changes on the current branch. This way, you can switch to another branch, return to the original branch after completing urgent tasks, and reapply your previously saved changes by restoring Stash.
  2. **Temporarily save working status:** Sometimes you may need to interrupt the current work, but do not want to commit unfinished changes. Use Git Stash to temporarily save your changes so you can continue working later. This is useful when you need to temporarily switch to another task, participate in a meeting, or deal with other urgent issues.
  3. **Resolve code conflicts:** When you encounter code conflicts when merging branches or pulling remote updates, you can use Git Stash to save the current modifications and restore the workspace to a clean state. You can then resolve the conflict and reapply your previously saved changes.

Use of Git Stash

Here are some commonly used Git Stash commands:

  • git stash: Save the modifications of the current workspace, but do not save the submitted modifications;
  • git stash save "message": Save the modifications of the current workspace and add a description message;
  • git stash list: List all storage records;
  • git stash apply [stash@{n}]: Apply a storage record and delete it from the storage list;
  • git stash pop [stash@{n}]: Apply a storage record and delete it from the storage list, while restoring the current working directory to the state when it was stored.

Suppose we modify the index.html file now and submit the index.html filegit add index.html to the temporary storage area, and then execute git ls-files to view the temporary storage area The file results are as follows:

Now we temporarily saveindex.htmlmodification statusgit stash, and there will be one more record in the storage record:

Of course, you can’t be so casual at work, otherwise there will be too many records and it will be difficult to find the records you want to restore. Usually use the git stash save "message" command:

Restore saved changes There are two common ways to restore saved changes. One is to use the git stash apply command, which will reapply the latest stash and keep a copy of the stash. The other is to use the git stash pop command, which will apply the latest stash and remove it from the stash list.

If you have multiple stashes, you can apply a specific stash through the git stash apply stash@{n} command, where n is the index number of the stash.

Once you no longer need a stash, you can use the git stash drop command to remove it from the stash list.

VS Code extension that enhances Git

I think most people don’t like typing commands in a black and white window. We can use the VS Code extension to enhance the use of Git. I recommend a commonly used Git plug-in for me to use GitLens. The installation is as shown below:

After installation, we can intuitively see all Git Stash storage records.

In this way, adding and saving temporary working status is just a click of the mouse.

There are other operating functions, but they are not described too much. Everyone can learn them with a little bit of installation.

Summarize

Git Stash is a powerful and flexible tool that helps developers manage changes more efficiently when switching branches, saving temporary working status, and resolving code conflicts. By properly utilizing Git Stash, we can handle multi-tasking development, merge changes, and handle emergencies, thereby increasing work efficiency and ensuring code integrity. Mastering Git Stash is a recommended skill that can bring convenience and flexibility to your version control process.

Follow the official account [Linglanshe] and reply "666", which will bring you into the [Human high-quality front-end communication group~]
More article links: https://linglan01.cn/about

Guess you like

Origin blog.csdn.net/qq_45472813/article/details/133745078