manuman
manuman

Reputation: 33

Accidentally removed from Git repository

When removing files from a Git repository I accidentally also removed a file that had uncommitted changes.

Reverting the Remove resulted in the file returning without the uncommitted changes. Is there any way of getting back the uncommitted state of the file?

Taking advice from jmort253 heres are the steps...

  1. Made changes to file (it had previously been committed & pushed so it is in git)

  2. Using SourceTree, removed some files accidentally including the previously mentioned file.

  3. Pushed to origin

  4. Having realised mistake reversed the commit (using SourceTree).

  5. File is now back but it missing the changes made in step 1.

    Hope that helps.

Upvotes: 3

Views: 463

Answers (2)

Eduardo
Eduardo

Reputation: 22834

You should try the following looking into the reflog:

git rev-list --all --oneline mylostfile.txt

replacing mylostfile.txt with your actual file path.

If the file exists on git, either inside a commit pointed to by a branch or a lost commit it will show up.

This will show all commit hashes where changes to that file exist, so you may need to look at them individually to see if one of them has the file you want.

If it doesn't show up than git probably don't know about that file.

Upvotes: 1

Francois G
Francois G

Reputation: 11985

In short : no. An uncommited file is (nearly by definition) a file the version control system does not know anything about, so the version control system will no more be able to help you as if you had deleted a random file on your disk (e.g. a file that had never been in the file hierarchy of your repository).

That is, unless you have a stash on a previous state of your repository that includes the uncommited changes ? (git stash list should give you an idea)

If you don't, you should turn to the file recovery methods appropriate for your operating system and filesystem.

Upvotes: 2

Related Questions