Why are newly added files deleted after aborting git rebase?

145 Views Asked by At

I was doing a rebase when some commit had conflicts. During resolving the conflicts I accidentally added all of my untracked files. Because I too don't keep track of untracked files I could not remember which files to unstage. Or which files where staged as part of the conflicting commit.

Instead of figuring it out, I decided to git rebase --abort hoping to just start over. However, those files that were mistakenly added have now disappeared.

I would like to understand what had happened. And why git rebase --abort does not restore my working tree to its original state. And if possible, where to find these files. Some of them have valuable work.

1

There are 1 best solutions below

3
knittl On

Nothing has been lost!

The Git reflog records all commits made on local branches ("references" or "refs" for short) and also for all commits that HEAD pointed to.

Run git reflog and you should see all commits made during your aborted rebase process. The reflog should contain an entry "rebase (abort): returning to ...".

Use git log or gitk to inspect this commit (or any other commit of the reflog) and then git branch hash-of-that-commit to create a new branch pointing to that commit. You can then switch to that branch to extract the files.

As to why the files were removed when aborting the rebase: Git only leaves untracked files alone during its operations. Once you tracked these files (intentionally or not), they became managed by Git. Switching branches or checking out different commits will remove (tracked) files from your working tree which are part of the "previous" commit but not part of the to-be-checked-out commit.

The good thing is that Git doesn't easily lose data that has been committed once.


If files never were committed, but the content of the file was staged (with git add), then it is still possible to recover the content, but it is a bit more work. Run git fsck to find any dangling blobs and then use git cat-file -p or git show with the object id of the respective blob. Once you have found the correct blob(s), redirect the output of cat-file/show to a file with the desired name. Repeat for all your files.