I encountered the above problem, for convenience, I will use a small example to recreate the scene
nick@nick-linux:~/Desktop/bugtest$ git init
Initialized empty Git repository in /home/nick/Desktop/bugtest/.git/
nick@nick-linux:~/Desktop/bugtest$ echo original > file1.txt
nick@nick-linux:~/Desktop/bugtest$ mkdir data
nick@nick-linux:~/Desktop/bugtest$ echo datafile2 > data/file2.txt
nick@nick-linux:~/Desktop/bugtest$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
data/
file1.txt
nothing added to commit but untracked files present (use "git add" to track)
nick@nick-linux:~/Desktop/bugtest$ git add .
nick@nick-linux:~/Desktop/bugtest$ git commit -m "init commit"
[master (root-commit) 28ce869] init commit
2 files changed, 2 insertions(+)
create mode 100644 data/file2.txt
create mode 100644 file1.txt
nick@nick-linux:~/Desktop/bugtest$ echo datafile3 > data/file3.txt
nick@nick-linux:~/Desktop/bugtest$ echo /data/* > .gitignore
nick@nick-linux:~/Desktop/bugtest$ cat .gitignore
/data/*
nick@nick-linux:~/Desktop/bugtest$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
nick@nick-linux:~/Desktop/bugtest$ echo datafile4 > data/file4.txt
nick@nick-linux:~/Desktop/bugtest$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
nick@nick-linux:~/Desktop/bugtest$ git rm -r --cached data/
rm 'data/file2.txt'
nick@nick-linux:~/Desktop/bugtest$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: data/file2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nick@nick-linux:~/Desktop/bugtest$ git add .
nick@nick-linux:~/Desktop/bugtest$ git commit -m "rm data, upload gitignore"
[master 399dd82] rm data, upload gitignore
2 files changed, 1 insertion(+), 1 deletion(-)
create mode 100644 .gitignore
delete mode 100644 data/file2.txt
nick@nick-linux:~/Desktop/bugtest$ git status
On branch master
nothing to commit, working directory clean
nick@nick-linux:~/Desktop/bugtest$ echo change > file1.txt
nick@nick-linux:~/Desktop/bugtest$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file1.txt
no changes added to commit (use "git add" and/or "git commit -a")
nick@nick-linux:~/Desktop/bugtest$ ll
./
../
data/
file1.txt
.git/
.gitignore
nick@nick-linux:~/Desktop/bugtest$ git stash -u
Saved working directory and index state WIP on master: 399dd82 rm data, upload gitignore
HEAD is now at 399dd82 rm data, upload gitignore
nick@nick-linux:~/Desktop/bugtest$ ll
./
../
file1.txt
.git/
.gitignore
nick@nick-linux:~/Desktop/bugtest$ git stash pop
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file1.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (acb5e64200a875485799cb942c3898e1791abc5b)
nick@nick-linux:~/Desktop/bugtest$ ll
./
../
file1.txt
.git/
.gitignore
You can see that at the end the data folder disappears.I want to know how it works, and how can I recover my lost folder. Thank you!
My working configuration is "git version 2.7.4; ubuntu 16.04LTS".
The above is an example, here I present my actual operation in the project. It may be helpful to find the missing folder.
I wanted to create a branch locally to provide development productivity, so I read the documentation for Git Branch and Git Stash, but I had trouble actually doing it. I've been working on the master branch, and there are some changes that haven't been committed yet, so
git stash --keep-index -u
git stash apply
git stash -u
git checkout -b newbranch
git stash pop
git stash drop stash@{0}
After working on the new branch for a few hours, I suddenly realized that I had lost several folders that had previously been untracked on the master branch and marked in .gitignore. In addition, I also found that some untracked folders were not lost, and they were also marked in .gitignore.
I didn't notice when these folders were lost, after git stash -u or after git checkout -b ? I switched back to the matser branch
git stash -u
git checkout master
and these folders are still missing.
After research, I reproduced the above simple example, and guessed that this is the reason for the loss.