Answer the question
In order to leave comments, you need to log in
How to make a partial git stash?
From time to time, it becomes necessary to stash not all the changed files, but only some. How to implement it? Yes, there is git stash --keep-index (--no-keep-index)
or git stash --patch
. But (here I can be mistaken) neither one nor the other gives me complete control over what is happening. With the --keep-index flag, all staged files will be added to stash ( to be committed
, as a normal commit would do , what is needed), as well as everything not staged for commit
in addition, which I do not need at all. Why can't you just add to stash only those that are ready to be committed? Alternatively, in order to achieve the desired result, you can make commits and then cancel them, but this is already some kind of hint of a Monsieur who knows a lot ...
Thank you.
UPD. Thanks to all who answered.
Brief summary: adding only selected files to stash will not work. If such a need arose, then most likely you are using git incorrectly. Look in the direction of other schemes for using the git: rebase
, cherry-pick
, merge
workflows.
Answer the question
In order to leave comments, you need to log in
You look at the stash a little wrong, it is not intended for preparing commits.
It is intended to hide changes that are not needed at the moment, which is why it is called stash, in translation - hide, hide.
Somehow you wrote it wrong.
With the --keep-index flag, all changes will go to the stash except those that have already been added to the index (prepared for the commit). If you also want to add untracked files to the stash, you must specify --include-untracked.
If you do it by hand, you can create a separate branch and save what you need there, and then just do a git cherry-pick of the desired commit, but this is a bit inconvenient and requires more body movements.
I would suggest doing a branch, commit there, and then, if necessary, merge the commits into one more meaningful one. If you need to put some files aside, just do it on another branch. Then you can merge or cherry-pick.
Actually, this is the Git way: commit, commit and commit again.
But to the question
Why can't you just add to stash only those that are ready to be committed?
Well, yes, adding only selected files to stash will not work if you do not read the documentation and do not find the command
git stash push -- [filename]
PS The very fact that you needed to hide only part of the files suggests that you are going to make a good and understandable commit and don't want unwanted changes to get into it. It seems to me that this, on the contrary, indicates the correct use of the git, and not the wrong one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question