TL; DR
I want to run tests only with the supplied files before committing:
git stash save -k -uto hide unsupported / unverified files / changes before testing- run tests with phased files
git stash popto restore the changes / files in step 1.
A problem in use git stash popwill lead to conflicts in files with partial setting. Resolving conflicts will result in the loss of partial stepwise / undefined changes (you need to select separate lines again for re-setting).
Update. If you want to know the shell script to run this procedure, go to the last section for more information.
NOTE: only adjacent lines (or close enough) with partial arming will cause this problem. For example, if you have 6 new line changes in a file:
1 +| a (add to staged)
2 +| b (add to staged)
3 +| c (add to staged)
4 | d (keep unstaged)
5 | e (keep unstaged)
6 | f (keep unstaged)
Now use git stash -k -u, and then git stash popcause a conflict.
Show question
Git has three phases for the changes before you commit: staged, unstagedand untracked.
Any changes will be added to unstaged. Before committing, you can select some lines or files and add them stagedto git add.
Now, adding some code to staged, I want to run the tests only with the supplied files, to make sure that they are suitable for fixing, so I need to save the changes unstagedand untracked(new files) to git stash -k -uand save the changes staged.
, , 3 : A , B ( ), C - , .
[staged]
file A
file B (only stage some of code)
[unstaged]
file B
[untracked]
file C (new file)
git stash -k -u / .
[staged]
file A
file B (only stage some of code)
[unstaged/untracked]
<none, clean>
. , git stash pop, B, . , B .
, git stash pop - , , .
,
development start
|
[make changes (unstaged)]
|
(pick changes to staged for commit by `git add`)<
| |
V (pick other changes to fulfill tests)
[partial staged/unstaged] |
| |
(stash unstaged changes by `git stash -k -u`) |
| |
(run tests only with staged files for commit) |
| |
(restore stashed files by `git stash pop`) |
| |
|
|
<if test success>
|
[commit staged files by `git commit`]
|
V
keep development or next commit
stash pop, / . .
: script
@torek script :
echo '--------------------------------------------------------------'
echo '---- Stash all unstaged/untracked files (git stash -k -u) ----'
echo '--------------------------------------------------------------'
BEFORE_STASH_HASH=$(git rev-parse refs/stash)
git stash -k -u -q
AFTER_STASH_HASH=$(git rev-parse refs/stash)
if [ "$BEFORE_STASH_HASH" == "$AFTER_STASH_HASH" ]; then
echo '\n\n---- Stash failed! Please check and retry. ----\n\n';
exit 1;
fi;
echo '-------------------'
echo '---- Run tests ----'
echo '-------------------'
<run your tests here> ||
(echo '\n\n---- Tests failed! Please fix it before commit. ----\n\n')
echo '-----------------------------------------------------------'
echo '---- Restore all stashed files (git stash pop --index) ----'
echo '-----------------------------------------------------------'
git reset --hard -q &&
git clean -df -q &&
git stash pop --index -q ||
(echo '\n\n---- Restore failed! Please check and fix it. ----\n\n')