简体   繁体   中英

How can I get my diff back after git add and git checkout without git commit?

I modified some files in branch A, and then I use git add to add these modified files. But I forgot to use git commit to commit the modifications to the current branch. Then I used git checkout B to switch to another branch. When I switched to branch A again, I found nothing modified can be found here. Is there any way that I can get the modified files back?

If git allowed you to switch branch, even with uncommitted changes, those changes are carried to the branch you switched to. So if you check diff between those two branches, it wont show any change. Try to take diff with any other base branch. hope

git add normally writes the object into the object database. If it is no longer reachable from the index or any commit, it will be a dangling blob.

$ git fsck --no-reflogs | grep 'dangling blob'
dangling blob 5cd8df407a53f3cf1a00d7df9b92973fc7124649
dangling blob a15ac56b6ac98b0b26c4d30bd299cf5cea817b83
dangling blob a052e2e82624bfa14d7004a30e971cfc543d3759
dangling blob b3cae5cf028ad4728b344193a7c6fe4e76e36af3
dangling blob bb428320e3f8496ee844dfe1826e5f705e47b0c8
dangling blob aa9bc6e7a3078a91a895f9ade9ecb0d3f46783a0
dangling blob 0094f013e9120ac2168248245ee492f1c551754d
dangling blob 896c4a4752fab07a7be22cc068c50d4b8021c8a9
dangling blob f6fc725dc3f01cedee7933651e202fa1e46101a4
$ git cat-file -p 5cd8df407a53f3cf1a00d7df9b92973fc7124649
...

You can print them out one-by-one to try to find what you lost. (See the --lost-found option also.)

I found them in the stash. After git add most of the files, I use git stash expecting to put the remaining files to stash. But it seems the modified files are all put to the stash whether they've been added using git add or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM