在将文件添加到git之前,尚未跟踪(和删除)检查文件

时间:2015-05-22 01:28:57

标签: git

在添加文件之前有没有办法检查,以前git中的路径是不知道的?

x220:/tmp$ cd git-test
x220:/tmp/git-test$ git init
Initialized empty Git repository in /tmp/git-test/.git/
x220:/tmp/git-test$ scrot IMG_0001.jpg
x220:/tmp/git-test$ git add IMG_0001.jpg
x220:/tmp/git-test$ git commit -a -m import
[master (root-commit) 76c4201] import
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 IMG_0001.jpg
x220:/tmp/git-test$ # view IMG_0001.jpg and actually i dont like it
x220:/tmp/git-test$ git rm IMG_0001.jpg
rm 'IMG_0001.jpg'
x220:/tmp/git-test$ git commit -a -m pruned
[master 57d4fc5] pruned
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 IMG_0001.jpg
x220:/tmp/git-test$ # but what happens, if I accidentally import the file again from my camera?
x220:/tmp/git-test$ scrot IMG_0001.jpg
x220:/tmp/git-test$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        IMG_0001.jpg

nothing added to commit but untracked files present (use "git add" to track)
x220:/tmp/git-test$ git add IMG_0001.jpg # don't want to do this!
x220:/tmp/git-test$ git commit -a -m import
[master 03932ba] import
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 IMG_0001.jpg

2 个答案:

答案 0 :(得分:0)

这是你的考试:

git whatchanged | grep -q $'D\tIMG_0001.jpg'

警告:$'...'是一种基础。如果这是POSIX脚本,则您必须使用文字TAB而不是\t

这是一个基于内容的内容,它会检查图像的内容是否在以前的任何文件中签入:

git show $(git hash-object IMG_0001.jpg) >/dev/null 2>&1

答案 1 :(得分:0)

我最终得到了:

test "$(git log --oneline -- $file | wc -l)" -gt 0

确定之前是否曾见过文件。请随意篡改script