Imagine you have already staged and committed a number of changes but then decided that the changes ought to be separated into two commits. For newly created files, you can stage removing them from the commit without removing them from disk (with git rm --cached filename
). Is there a similar command for modified files?
Essentially I would like to be able to stage the version of the file from the previous commit, and afterwards be able to re-run git add -p filename
to select which changes belong in the first new commit vs the second. I don't want anything on-disk to change; my only goal is to split one commit into two with minimal work.
git checkout HEAD~1 -- filename
stages what I want, but removes the changes from disk. I need them to stay on disk so I can proceed to add them to the next commit. (Note: You can undo this command with git reset filename && git checkout filename
)
git add HEAD~1 -- filename
feels like it should do what I want, but the command fails to run with fatal: pathspec 'HEAD~1' did not match any files