Betsy Dupuis
Betsy Dupuis

Reputation: 523

Switched Branch After .gitignore and lost .gitinored files

I am new to git, so sorry if this question has already been answered. I'm having trouble finding the answer to this.

I wanted to ignore a set of files that had never been committed before for a commit and used the github app to select them and ignore them. I switched to another branch for a while and when I returned to my branch, my ignored files were gone.

Are these files deleted?

I used git checkout to switch back to the branch and then git status --ignored . My missing files are not there.

Upvotes: 9

Views: 3881

Answers (3)

Adam
Adam

Reputation: 29039

Looks like you ignored the files in branch A, then switched to branch B but didn't ignore the files there.

If you ignore the files only in branch A but not in branch B, then they get deleted when switching back from B to A. However, they should reappear if you switch back from A to B.

This is normal git behavior if you checkout from any branch X to Y, all files that are not in branch Y but in branch X are removed. If a file is ignored in Y but not in X, then you have this case.

To prevent your files from being deleted, you may add them in the .gitignore file in your other branch B.

Upvotes: 1

alexw
alexw

Reputation: 8668

You can do this by using git reflog --all to get the SHA code from when the Github App automatically stashed your ignored files before switching to the other branch. See https://stackoverflow.com/a/8865242/2970321.

For example, I fetched a pull request (/pr/364) using the app (SHA 2ba129d). Before I switched back to gh-pages, it stashed my ignored files (SHA 36edfc6).

finding the SHA code of a dropped stash

So to recover, all I needed to do was:

git checkout 36edfc6

My files magically reappeared, and I was able to safely stash them manually somewhere else before switching back to gh-pages again.

Upvotes: 5

vonbrand
vonbrand

Reputation: 11791

git doesn't delete files it doesn't care about unless specifically told to do so (by checkout -f in some cases of checking out, or clean). As git knows nothing about them, it can't resurecct them either. Use git stash before moving around and doing possibly damaging stuff. Consider carefully which files go into .gitignore, add all non-automatically-generated files to version control.

Upvotes: 1

Related Questions