How to undo "git rm -r -f *"

I used git for the first time, I had a directory with several programs written in it and took the following steps

  • I did git add .
  • then git commit , then he received an Aborting commit due to empty commit message.
  • Then I thought, let me commit a group of files under a common message. So I thought about deleting all added files.
  • So I did git rm -r -f
  • When I do ls , I lost all my code. Is there a way that I can return them to my stupidity, I do not even have a backup.

Things I've observed so far

I searched for some of the commands found, but they do not work

git stash If I type this command, I get

fatal: poor revision of 'HEAD' fatal: poor version of 'HEAD' fatal: one revision required You do not have an initial commit yet

git reset HEAD , if I type this command, I get

Fatal: undefined argument 'HEAD': unknown version or path not to the working tree. Use '-' to separate paths from versions

I really need to return these files!

The steps that I followed to create GIT

  • mkdir BareRepo
  • In the BareRepo Directory I made git init , git status , git config --bool core.bare true
  • Then I cloned BareRepo git clone BareRepo/ Programs/
  • In the Programs directory, I did all of the above.
+4
source share
2 answers

You may be able to recover files from an interrupted commit

Based on the observation of @ the-malkolm.

From the information in the question there are no commits, and the files were not tracked at any time. Since git is not aware of any files that have been deleted.

However, there is hope. Just because you tried to migrate your files before deleting them, there is a phantom commit floating around with all your files.

Here is an example:

 $ git init Initialised empty Git repository in /tmp/so/.git/ $ echo "find this text" > README.md $ git add README.md $ git commit -v Aborting commit due to empty commit message. $ git rm -rf . rm 'README.md' $ git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track) 

The above simulates the events in the question, and usually it's time to rewrite the code again. No commit, file missing.

Determine that interrupted commit

However, checking the .git repository gives some information:

 $ tree .git/objects/ .git/objects/ ├── 91 │  └── 9cdf847a6af7c655c8de1d101385f47f33e0f9 ├── d6 │  └── 7d51abe2521dcd00cec138b72f5605125c1e41 ├── info └── pack 

There are objects in the git repository, despite the lack of commits. You must determine which of the two objects is tree :

 $ git ls-tree 919cdf fatal: not a tree object $ git ls-tree d67d51 100644 blob 919cdf847a6af7c655c8de1d101385f47f33e0f9 README.md $ 

The first link is a blob representing the README.md file - in the repository there will be one frame per file, the second in this example, a link to a tree.

Playback copy

Once the tree hash is identified, it can be used to restore the index using read-tree :

 $ git read-tree d67d51abe2521dcd00cec138b72f5605125c1e41 $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: README.md # # Changes not staged for commit: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: README.md $ 

At this time, the working copy is empty, but the lost files are set for commit.

Lock them:

 $ git commit -m "phew" 

And check to match the committed state of the repository:

 $ git checkout . $ ls README.md 

And then all the files exist and were committed.

+6
source

I'm not sure if this will work 100%, but definitely worth a try, I think:

git reflog is your team

reflog records every action you perform inside git ... so I bet on that.

reflog will create a list of commands that you executed in your repo

To restore your work, look at the log generated by reflog and do git reset --hard HEAD@ {<x>}

.. replace x with a number specifying the state you want to be in ...

.... what should reset your repo to its original state (hopefully)

-4
source

Source: https://habr.com/ru/post/1492559/


All Articles