Git: how to reinstall phased files in pre-commit

I am writing a git pre-commit hook.
The script can reformat some code so that it can modify the phased files.

How to reinstall all already configured files?

+4
source share
2 answers

Without context, pre-commit hookyou can get a list of phased files with the following command:

git diff --name-only --cached

So, if you want to reindex staged files, you can use:

git diff --name-only --cached | xargs -l git add

In context, pre-commit hookyou should follow David Winterbottom’s advice and make unidentified changes before anything else.

, . , :

# Stash unstaged changes
git stash -q --keep-index

# Edit your project files here
...

# Stage updated files
git add -u

# Re-apply original unstaged changes
git stash pop -q
+4

@tzi; , . , , , , . ,

  • ( A)
  • ( B)
  • ( A), ( B)

, (v. A), ( v. B). , , (v. B), () . , script , ( v. A WD v. B).

#!/bin/sh

... # other pre-commit tasks

## Stash unstaged changes, but keep the current index
### Modified files in WD should be those of INDEX (v. A), everything else HEAD
### Stashed was the WD of the original state (v. B)

git stash save -q --keep-index "current wd"

## script for editing project files
### This is editing your original staged files version (v. A), since this is your WD 
### (call changed files v. A')

./your_script.sh

## Check for exit errors of your_script.sh; on errors revert to original state 
## (index has v. A and WD has v. B)

RESULT=$?
if [ $RESULT -ne 0 ]; then
git stash save -q "original index"
git stash apply -q --index stash@{1}
git stash drop -q; git stash drop -q
fi
[ $RESULT -ne 0 ] && exit 1

## Stage your_script.sh modified files (v. A')

git add -u

git stash pop , , (v. A) (v. B) . , , script , git stash pop script (v. A ') (v. ). , script (v. A ') (v. B) (, , your_script.sh , , v. A v. A ' ).

: , , . , -, , ( ) , ( , )! .

+1

All Articles