41

git is capable of stashing of individual hunks.

With --patch, you can interactively select hunks from the diff between HEAD and the working tree to be stashed. The stash entry is constructed such that its index state is the same as the index state of your repository, and its worktree contains only the changes you selected interactively. The selected changes are then rolled back from your worktree. See the “Interactive Mode” section of git-add[1] to learn how to operate the --patch mode.

Is it possible to use it with magit frontend? Magit simplifies patch mode usage for staging spectacular. Could it do the same work for stashes?

3 Answers 3

62

You can stage whatever changes you want to stash and then call magit-stash-index (zi).

2
  • It seems to be difficult to keep renamed files out of the staging, though. (But I am not a git expert.)
    – Harald
    Commented Nov 24, 2021 at 7:46
  • If I only stage part of the change in a file and zi, the staged part of the change will be in unstaged status.
    – CodyChan
    Commented Apr 23, 2022 at 2:51
1

magit-stash-index (zi) and magit-stash-worktree (zw) work well if you can easily sequester what you want to stash into an otherwise empty index or worktree. When that's not the case, I use magit-git-topdir (!!) to run a command like:

git stash push -m "my stash title" my/file1 my/file2

I just raised a feature request to make this easier.

0

Nowadays you can use:

 magit-stash-push

Which has an individual files option --.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.