Detailed Explanation of Git Stash Command

Detailed Explanation of Git Stash Command

Introduction

In the process of project development, it’s common to work on multiple features or bug fixes on the same branch, but sometimes you need to switch branches or go back to a stable code state due to certain reasons. This is where the Git stash command comes in handy. The Git stash command can temporarily save the changes that have not been committed on the current branch and restore them later when needed.

Basic Usage of Git Stash Command

Command Format

The format of the Git stash command is as follows:

git stash [save [<message>]]
  • save [<message>]: Optional parameter. It can be used to attach a description text to the saved changes.

Example of Basic Usage

Before making any changes, use the git stash command to temporarily save the changes that have not been committed on the current branch. For example:

$ git stash
Saved working directory and index state WIP on master: b0027b0 add new function

After successfully stashing the changes, use the git stash list command to view all the stashed changes on the current branch. For example:

$ git stash list
stash@{0}: WIP on master: b0027b0 add new function

To restore the stashed changes, use the git stash apply command. For example:

$ git stash apply stash@{0}

After using the git stash apply command, the restored changes will return to the working directory. However, file deletion or modification operations executed before stashing will not be restored, so special attention needs to be paid.

If you want to directly restore the stashed changes and merge them into the current branch, you can use the git stash pop command. For example:

$ git stash pop stash@{0}

After using the git stash pop command, the restored changes will be merged directly into the current branch.

Examples in Different Situations

In actual development, there are many special situations where the git stash command needs to be used for stashing and restoring changes.

Stashing Changes but Not on the Current Branch

If the code being modified is not on the current branch, use the git stash --patch command to stash the changes. For example:

$ git stash --patch

This way, all changes can be stashed and returned to the main branch to perform other operations.

Stashing Changes and Switching to Another Branch

If you need to switch branches before performing other operations, you can use the git stash -u command. For example:

$ git stash -u
$ git checkout other-branch
$ # do something
$ git checkout master
$ git stash pop

This way, the changes will be stashed while switching branches.

Stashing Changes but Keeping Untracked Files

If you need to stash changes but want to keep untracked files, use the git stash --keep-index command. For example:

$ git stash --keep-index

This way, changes will be stashed without stashing untracked files.

Stashing Specific Changes

If you only need to stash a specific file or a specific function of changes, use the git stash push <path> command. For example:

$ git stash push filename
$ git stash push -p hunk

This way, changes can be stashed selectively.

Restoring a Deleted File

If a file has been modified and committed, and then deleted, use the git stash branch <branchname> <stash> command to restore the deleted file. For example:

$ git stash branch new-branch stash@{0}

This will create a new branch where the stashed changes are and restore the deleted file from the new branch. This command can also be used to restore other changes, not just limited to the situation where files are deleted.

Conclusion

During the project development process, using the git stash command can conveniently stash and restore uncommitted changes, which is particularly useful for multi-point modifications and frequent branch switching. Through this article, readers can learn about the basic usage of the Git stash command and its usage in different situations. We hope this article is helpful to readers.

Like(0)

Related

Git Tutorials
Git TutorialDifferences between Git and SVNGit vs. Github: What's the Difference?How to Install Git on WindowsHow to Install Git on LinuxHow to Install Git on MacConfiguring Git on Your Local EnvironmentGit Workflow Detailed ExplanationGit Basic Commands IntroductionGit add command detailed explanationGit amend Command: A Comprehensive GuideA comprehensive guide to Git apply commandGit branch Command: A Comprehensive GuideGit Blame command: A detailed explanationGit Config Command in DetailGit clone Command: A Detailed ExplanationA Comprehensive Guide to the Git Clean CommandUnderstanding and Using Git Commit CommandA Comprehensive Guide to Git Checkout commandGit cherry-pick Command: A Comprehensive Guide to Its UsageA Detailed Guide to Git Diff CommandGit Fetch Command: A Comprehensive GuideA Comprehensive Guide to Git Format-Patch CommandGit ignore Command: A Detailed GuideGit init Command: A Comprehensive GuideA Comprehensive Guide to Using Git Log CommandGit merge command tutorialA Comprehensive Guide to Using Git mv CommandGit push command: A Detailed Guide on Using itA Comprehensive Guide to Using Git Pull CommandGit remote command usageGit Revert Command Detailed ExplanationGit reset Command: A Comprehensive GuideGit Restore Command: A Comprehensive GuideA Comprehensive Guide to Git Rebase CommandGit reflog Command Usage ExplainedGit rm Command Usage OverviewGit Status Command - A Detailed GuideDetailed Explanation of Git Stash CommandGit show Command: An In-depth GuideGit Switch Command: A Comprehensive GuideComplete Guide to Using Git Tag CommandGit .gitignore File: A Comprehensive Guide