Add changes generated by pre-commit
See original GitHub issueLoosely related to #806 .
I sometimes find myself doing the following:
- Edit some files.
- Run
git add -p
and stage only some of my edits. - Run
git commit -m "Some message"
.- pre-commit stashes all the unstaged changes,
- runs all hooks, which change the working tree,
- pops the stashed edits,
- and rejects the commit.
The changes generated by the pre-commit hooks are now intermixed with the unstaged edits. I can’t just run git add -u && !!
, as that will stage too much. Instead I need to run git add -p
and select only the unstaged changes generated by my hooks.
Could pre-commit generate a patch of changes it generates and store that patch in .git
? Then I could do something like the following:
git add -p
git commit -m "Some message."
cat .git/pre-commit/patch
git apply --index .git/pre-commit/patch && !-2
Or is there already a way to do this?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
pre-commit
Git hook scripts are useful for identifying simple issues before submission to code review. We run our hooks on every commit to automatically...
Read more >Can a Git hook automatically add files to the commit?
I'd like to add an automatically generated file to the same commit using a pre- or post-commit hook in Git, dependent on the...
Read more >How to commit files modified by pre-commit hook in Git
Pre-commit hook is executed automatically when you do "git commit". It must have an executable bit set. So try to add some files...
Read more >Automatically format and lint code with pre-commit - Interrupt
How to use pre-commit for automated linting, formatting, and styling firmware code by using Python, clang-format, clang-tidy, and prettier.
Read more >How to use git pre-commit hooks, the hard way and the easy ...
The hook runs before a commit is accepted to git, and if the hook script fails (ie if it returns a non-zero exit...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I often run into this myself…
A workaround is also to use
git stash -k
(--keep-index
) before committing, so any changes from pre-commit can be added usinggit add -u
.But it needs to be done before.
I would like if pre-commit could just not restore the changes from the patch (optionally), allowing for
git add -u
, and then restoring it manually - a command to do so could be given for copy’n’paste.as you’ve learned from #806 and #747 – pre-commit will not modify your staging area since this is judged as an unsafe operation
as for generating a patch, there is one which is stored in
~/.cache/pre-commit
which represents the parts pre-commit unstages. You’re free to reverse-apply that, then staged, then commit, then apply the patch again and you’ll be back to your desired state.Let me know if this works for you!
Here’s a sample workflow for that:
And when I commit:
Now I can remove the parts I didn’t want to commit:
And then commit and then restore: