jss jss - 4 months ago 19
Git Question

Git deleted my files. Can they be recovered?

I created a git repository on BitBucket to keep track of my scripts. I only have very limited experience with git. I use a combination of Linux and Windows but I have been adding files to the repository using git bash shell on Windows which has a drive mapped to the Linux share.

I have been adding files over the last few days, but on one of my commits I entered the wrong comment. I wanted to fix this and googled for a solution. The suggestion I found was to use rebase. I tried this but it wasn't successful so I decided to just live with the wrong comment.

After this I created two more scripts which I then wanted to add to the repository with 'git add'.

When I tried to commit these I got the message "You are currently editing a commit while rebasing branch 'master' on '3ac74cb'". After this message was a list of files and then another line with suggestions as what to do. Unfortunately all this has now moved out of the terminal buffer so I can't track back what the message were, but I basically followed the messages.

One of the messages also suggested that I do a pull - this was due to the fact that I made changes to a text file which I edited in BitBucket. So I also did a pull.

I have basically done a pull, then a commit again and then a push. None of these seem to have worked so I did some more googling to get rid of the rebase message. Based on the answer I found I did a 'git rebase -abort'.

After this, the two scripts that I was trying to add disappeared.

Can I get my files back from somewhere or are they just deleted? How can I avoid this from happening again because I can't afford to lose work like this. The whole point of using git is to not lose work.

If you need any further information to try and make sense of this mess please just ask. I would really appreciate any help.

Answer

It sounds like you were in the middle of a rebase operation when you created your new files in the working directory. Unfortunately using git rebase --abort will reset HEAD back to where it was when the rebase operation started. If git status doesn't show any untracked files then I'm afraid your files may have been lost.

git rebase is a very useful command when used correctly but it can also cause a lot of trouble when misused.

For future reference, you can change the commit message for your most recent commit using git commit -m <message> --amend. This should prevent you running in to trouble for the same reason in the future.

I hope this helps.