Home > database >  What exactly happens when you accidentally unstage GIT files on top of "changed" files?
What exactly happens when you accidentally unstage GIT files on top of "changed" files?

Time:07-30

Boy, did I screw up. I had a few files that were staged in a big project and was working all day on more changes, some of which I am sure were files that were in the "staged" area. I am in the process of restoring a backup to see if I can compare changes and get myself out of this mess. While I am waiting for the restore, it gave me time to beat myself up and ask this question.

My assumption is that whatever files were in the staged area just kiboshed (overwrote) the files in the "changes" area. Am I correct? I will keep searching/

Thank you so much in advance.

CodePudding user response:

git add adds the content to the repo, it's all still in there, unstage just means wiping an index pointer to it.

find .git/objects/?? -type f -exec ls -t {}   | awk -F/ '{print $3$4}'

will list your loose objects (that's stuff you've done locally since the last repack), latest first. To whittle it down to stuff you care about,

find .git/objects/?? -type f -exec ls -t {}   \
| awk -F/ '{print $3$4}' | git cat-file --batch-check \
| awk '$2=="blob" && n  <50'

which will show the fifty most-recently git added blobs. git show those ids (if you've added any media files maybe show them all through a pager to keep the binary stuff from being annoying) and put the ones you love back where they belong with

git update-index --cacheinfo 100644,$thatid,the.right.name

to get the index pointers back and then you can git checkout-index -a if you want everything back in your work tree too.

CodePudding user response:

Okay, I panicked. Combine that with a lack of knowledge of what I had just nuked from my staging area as well as a lack of knowledge of what "unstaging" actually did, I imagined the worst.

Here's my question (and an explanation in the hopes that it helps someone else understand) as to what prompted my original question. Here was my (after-the-panic) test. I'll make an example with a text file to keep it simple.

I start with an empty file, and now add the contents: AAAAAA. The file shows up as "changed" in my Git changes panel.

Now I stage the file. The file moves from "changed" to "staged".

Next, I change the content: AAAAAA,BBBBBB.

THIS is where I inadvertently UNSTAGED the file (and panicked, of course) and the. The Git panel showed the file only in the changed panel, as expected, but I feared that the contents of my file would be only AAAAAA and that I would have no idea (in real life with a hundred changed code files) what I was missing from the code files. The GOOD news is that the contents of my example file still showed AAAAAA,BBBBBB. My actual code files were safe with NOTHING changed from my errant mouseclick. ONLY the indication that they were no longer "staged". Thank goodness. I can live with that.

A couple of helpful links HERE and HERE. And special thanks to @jthill for be patient with me and holding my hand while I panicked. ;-)

  • Related