question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add changes generated by pre-commit

See original GitHub issue

Loosely 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
blueyedcommented, Mar 22, 2019

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 using git 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.

2reactions
asottilecommented, Dec 7, 2018

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:

$ git diff --staged
diff --git a/t b/t
index f00c965..dc76404 100644
--- a/t
+++ b/t
@@ -1,6 +1,6 @@
 1
 2
-3
+3 # trailing whitespace 
 4
 5
 6
$ git diff
diff --git a/t b/t
index dc76404..8e6f928 100644
--- a/t
+++ b/t
@@ -4,7 +4,7 @@
 4
 5
 6
-7
+7 # don't stage me
 8
 9
 10

And when I commit:

$ git commit -m "hi"
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /Users/asottile/.cache/pre-commit/patch1544222126.
Trim Trailing Whitespace.................................................Failed
hookid: trailing-whitespace

Files were modified by this hook. Additional output:

Fixing t

Fix End of Files.........................................................Passed
Check Yaml...........................................(no files to check)Skipped
Check for added large files..............................................Passed
[INFO] Restored changes from /Users/asottile/.cache/pre-commit/patch1544222126.

Now I can remove the parts I didn’t want to commit:

$ git apply --reverse /Users/asottile/.cache/pre-commit/patch1544222126
$ git add -u
$ git diff --staged
diff --git a/t b/t
index f00c965..50cfb57 100644
--- a/t
+++ b/t
@@ -1,6 +1,6 @@
 1
 2
-3
+3 # trailing whitespace
 4
 5
 6

And then commit and then restore:

$ git commit -m "wat"
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Passed
Check Yaml...........................................(no files to check)Skipped
Check for added large files..............................................Passed
[master 13f3873] wat
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git apply /Users/asottile/.cache/pre-commit/patch1544222126
$ git diff
diff --git a/t b/t
index 50cfb57..6900639 100644
--- a/t
+++ b/t
@@ -4,7 +4,7 @@
 4
 5
 6
-7
+7 # don't stage me
 8
 9
 10
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found