Run tests only for phased files: `git stash -k -u` and` git stash pop` will cause conflicts on partial phased files

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 failed>-------------| 
          |
    <if test success>
          |
[commit staged files by `git commit`]
          |
          V
keep development or next commit

stash pop, / . .

: script

@torek script :

#!/bin/sh -e

# stash all unstaged changes
# (-k: unstaged files; -u: new added files; -q: quite)
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;

# run test only with staged files
echo '-------------------'
echo '---- Run tests ----'
echo '-------------------'
<run your tests here> ||      #### <=== replace your test command here
(echo '\n\n---- Tests failed! Please fix it before commit. ----\n\n')

# restore all stashed changes
# http://stackoverflow.com/questions/41304610/
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')
+5
3

- . "git - -" ? , , git stash, .

, :

  • git stash save -k -u , - (, git rev-parse refs/stash ).
  • .
  • git reset --hard && git clean -df (, -q ). git reset --hard , , git clean , .
  • git stash pop --index. , --index. -q.

save pop --index create apply --index ( / , ). , , git stash script, .


, , , :

  • .
  • , . ( git checkout-index . GIT_WORK_TREE GIT_DIR --git-dir --work-tree front t git. )
  • .
  • .

git stash save , 2, . : , - , . .

+2

, @Paul. , .

:

  • git stash -k -u / , .
  • . , 3; , 4.
  • , / . :
    • git commit -m 'temp', .
    • git stash pop, / . - (, ) .
    • git checkout --theirs -- . && git reset, , , reset unmerged unstaged. , git reset .
    • git reset HEAD^ --soft, staged.
    • (/ / ), 1. unstaged staged, . , git , .
  • , git commit .

: (1) , (2) (3) , , git commit --amend, . , .

0

, . 2.

# 1. Stash all changes, then discard all unstaged changes
$ git stash --include-untracked --keep-index

# 2. Run tests, run linter, check types, etc. Don't modify files. Commit if desired.

# 3a. If no commit was made, restore to initial state
$ git stash pop

# 3b. If a commit was made, restore unstaged changes
$ git checkout stash -- . ; git stash pop ; git reset

~/.gitconfig:

[alias]
  stash-unstaged = stash --keep-index --include-untracked

  restore-unstaged = "!git checkout stash -- . ; git stash pop ; git reset"

  # Shortcuts
  stun = stash-unstaged
  restun = restore-unstaged

( , stash-unstaged , stash . .)

:

  1. git stash-unstaged

  2. , ..

  3. git stash pop git restore-unstaged, ,

0
source

Source: https://habr.com/ru/post/1664699/


All Articles