How to stash only staged changes in Git?

How to stash only staged changes in Git?

Yes, it is possible to stash only staged changes in Git. By using a default command called “git stash” we can modify both staged and unstaged tracks in files. However, if you want to stash only the staged changes, continue reading this blog as we’ll explore different methods to achieve this.

Table of Contents:

Methods to stash only staged changes in git

Few methods in git that will help you to stash only staged changes:

Method 1: Using git stash push with –staged

  • Git introduced the “git stash push” command in a recent version 2.13, which is the easiest method if you want to create a new stash containing only the changes that are currently staged in your index.
  • Simply run: git stash push – – staged  “your stash message”.This command will stash only the staged changes and leave the unstaged changes untouched.
  • You can use the “- –  staged” option to stash only the staged changes.

Method 2: Using git diff and git apply

If you’re working with an older version of Git that doesn’t support “–staged”, git diff and git apply are two powerful Git commands that work together to manage changes in your codebase.

1. Git Diff 

You can use “git diff” to generate a patch file that contains the changes you’re interested in.

Steps for using the “git diff” command.

Step 1: First, create a patch of staged changes using “git diff” with the  “- -cached”

git diff --cached > staged_changes.patch

This “git diff” command is used to show the differences between different versions of files in your Git repository.

Step 2: Then, you can use git reset to unstaged the changes.

git reset

Step 3: After that, you can stash the staged changes by applying the patch later.

git apply staged_changes.patch

You can also delete the patch file afterward using “rm staged_changes.patch”, if you no longer need it .

2. Git Apply

This command takes a patch generated by git diff and applies those changes to your files. It’s a way to bring changes from one version to another.

git apply <patch-file>

Here <patch-file> is the path to the patch file you want to apply.

Method 3: Creating an alias

If you frequently stash only staged changes, you can create an alias for the command. Add the following line to your Git config file:

[alias]
stashs = stash push -S

Now you can use git stash -m “Your stash message” to stash only staged changes.  

Conclusion

Stashing only staged changes in Git is possible and can be achieved in various ways. For newer versions, you can use “ git stash push – – staged” and for older versions of Git you can use “git diff and git apply”. Additionally creating an “alias” can simplify the process for frequent use. You can explore git commands and our advanced DevOps course for in-depth knowledge about GitHub.

Get 100% Hike!

Master Most in Demand Skills Now!

FAQs

1. What is the use of patches?

Patches can be used for code reviews, allowing reviewers to see the changes clearly.

2. What happens if “git apply” can't apply a patch cleanly?

When you use “git apply”, it may create “.rej files”. These files show the changes that couldn’t be applied automatically. You’ll need to open these files, fix the conflicts manually, and then use “git add” to stage the resolved changes.

3. How do I use git diff and git apply together?

“git diff” shows the differences between your working directory and the last commit, while “git apply” applies a patch to your files and makes the changes.

About the Author

Senior Cloud Computing Associate

Rupinder is a distinguished Cloud Computing & DevOps associate with architect-level AWS, Azure, and GCP certifications. He has extensive experience in Cloud Architecture, Deployment and optimization, Cloud Security, and more. He advocates for knowledge sharing and in his free time trains and mentors working professionals who are interested in the Cloud & DevOps domain.

EPGC Cloud