I've lost files added to git during "merge conflict" phase.
Step by step:
git pull
git status
Git informs me about "merge conflict", that's okay. Then I create a new file and add it to git.
vi test.txt
git add test.txt
After that, abort merge:
git merge --abort
I have not found the file "test.txt" neither in directory, nor via "git fsck", nor "git reflog". Is it possible to restore the file?
What have you tried with
git fsck
?see this SO question : Recover files that were added to the index but then removed by a git reset
The general answer is : the sequence of instructions you typed has removed the file from tracking (and from the disk), there is no guarantee that the content of your file can be retrieved.
However,
git
has a lot of safety mechanism, and one of them is : if some data is entered somewhere in the repo, it will not be deleted before two weeks.(
git
has a garbage collection mechanism, seegit help gc
)If you did indeed run
git add test.txt
at some point, and this action was recent enough, there should still be some trace of the content of the file stored within git :in
git
parlance, a file is ablob
:This should give you a list of internal git hashes :
note that
git add file.txt
stores the file's content, not the file's name ...If you remember a specific string in your file, you can try to narrow your research by using
git grep <string> <hash>
:You can then view the whole content of a blob using :
and hopefully find back the file you were looking for.
No, it's not. You had to make a
git commit
instead of agit merge --abort
. As @Muneer Bits put it, I advise you to read a bit on the git basics. The official site contains a lot of documentation, and examples. You can even try it online with an interactive tutorial. The point is, you can't lose your work if it has been commited or stashed. I strongly encourage you to read the advices git is outputting all the time (unless you have an old version, in which case you should upgrade too). These tips should have told you to mark the commit resolved by adding the file first, then finalize the merge by performing agit commit
.Merge commits are just special commits with two parents. That means they need to be commited at some point, be it automatically (if there is no conflict) or manually. When you aborted the merge, you reverted (reset) all your changes to the point you were before the merge. And thus you lost every changes you made.
Possible solution (depending on your workflow):
Note that the merge wasn't aborted.
Be also aware that merging is not always the best solution when pulling. You can also pull with rebase. But I strongly encourage you to become a bit more familiar with git before attempting risky things with your work. And, as always, the solution you choose in the end will depend on your workflow.
Poor english from chinese.
Thks for reply#1. i restore my files!!! Happy ^.^
I did like this:
And i saved files to named folder with diffrent keyword which i remembered like this:
If you lost too much content, you should execute above command many times with different keyword. and you will get many keyword folders.
I opened another vscode and open each keyword folder, and copy every $bold.ts code to my project file. do it careful because $bold.ts maybe repeat codes, you should distinguish the last code.