git stash恢复已删除和重命名文件的索引状态

Eli*_*ady 12 git git-stash

当存储暂存的已删除或重命名的文件,然后将其取消存储时,它们将以已删除和未删除的状态进行恢复.

在以下示例状态中:

$ git status s

A  file0
D  file1
R  file2 -> file3
?? file4
Run Code Online (Sandbox Code Playgroud)

运行git stash push -k -u然后git stash pop --index会让我处于以下状态:

$ git status s

A  file0
D  file1
R  file2 -> file3
?? file1
?? file2
?? file4
Run Code Online (Sandbox Code Playgroud)

我希望最终处于原始状态,而删除的文件没有重新显示为未跟踪pop.

有什么方法吗?

编辑:这是一个重新创建问题的脚本(在Mac OS X 10.13.2上使用git 2.16.1进行测试)

#!/usr/bin/env bash

echo -e "\nInitializing a fresh git dir..."
mkdir gitStashIssue && cd $_
rm -rf * .*
git init


echo -e "\nPreparing git state for test..."
# Create files and commit them
echo 'abc' > file1
echo 'aabbcc' > file2
echo 'aaabbbccc' > file3
echo 'aaaabbbbcccc' > file4
git add .
git commit -m 'initial commit'

# Make changes and add them to stage
echo `cat file1` >> file1
echo `cat file2` >> file2
git add .

# Make another change to a staged file without
# staging it, making it partially staged
echo `cat file1` >> file1

# Delete and rename files
git rm file3
git mv file4 fileRenamed

# Add untracked file
echo "untracked" > untrackedFile

# git status -s should now show
# MM file1
# M  file2
# D  file3
# R  file4 -> fileREnamed
# ?? untrackedFile

echo -e "\nCurrent git status is:"
git status -s

echo -e "\nStasing changes..."
git stash save -u -k

# git status -s should now show
# M  file1
# M  file2
# D  file3
# R  file4 -> fileREnamed
# ?? file3
# ?? file4

echo -e "\ngit status after stashing files is:"
git status -s

echo -e "\ncleaning up deleted and renamed files..."
git clean ./ -f

echo -e "\ngit status after cleanup:"
git status -s

echo -e "\nCommiting unstashed changes..."
git commit -m 'commit unstashed changes'

# This causes a conflict in file1
# git status -s should now show
# UU file1
# ?? untrackedFile
git stash pop --index

echo -e "\ngit status after unstashing:"
git status -s
Run Code Online (Sandbox Code Playgroud)

eph*_*err 1

您的脚本有输出。正如你所看到的,没有file3unstashfile4之后。该文件出现在stash -k. 这是正常行为,因为stash会恢复已删除的文件。-k 使其保留索引更改,即file3从索引中删除。所以你 同时拥有D file3和。?? file3

Initializing a fresh git dir...
rm: refusing to remove '.' or '..' directory: skipping '.'
rm: refusing to remove '.' or '..' directory: skipping '..'
Initialized empty Git repository in /home/azzel/projects/test/bash/gitstash/gitStashIssue/.git/

Preparing git state for test...
[master (root-commit) 7e891f6] initial commit
 4 files changed, 4 insertions(+)
 create mode 100644 file1
 create mode 100644 file2
 create mode 100644 file3
 create mode 100644 file4
rm 'file3'

Current git status is:
MM file1
M  file2
D  file3
R  file4 -> fileRenamed
?? untrackedFile

Stasing changes...
Saved working directory and index state WIP on master: 7e891f6 initial commit

git status after stashing files is:
M  file1
M  file2
D  file3
R  file4 -> fileRenamed
?? file3
?? file4

cleaning up deleted and renamed files...
Removing file3
Removing file4

git status after cleanup:
M  file1
M  file2
D  file3
R  file4 -> fileRenamed

Commiting unstashed changes...
[master 1156712] commit unstashed changes
 4 files changed, 2 insertions(+), 1 deletion(-)
 delete mode 100644 file3
 rename file4 => fileRenamed (100%)
Auto-merging file1
CONFLICT (content): Merge conflict in file1
Recorded preimage for 'file1'
Index was not unstashed.

git status after unstashing:
UU file1
?? untrackedFile
Run Code Online (Sandbox Code Playgroud)