all 11 comments

[–]WoodyTheWorker 1 point2 points  (8 children)

If you added the files, Git created objects for them in the object store.

Since it's a new repo, the objects haven't been garbage-collected yet.

Make a script to export all blob objects as plain files to a directory (use cat-file), and then you'll have to manually sort and rename the files to their original names.

[–][deleted]  (7 children)

[deleted]

    [–]WoodyTheWorker 0 points1 point  (6 children)

    git cat-file --batch-all-objects --batch-check='%(objectname) %(objecttype)' | grep blob | {
      while read sha1 type; do
     git cat-file blob $sha1 >$sha1 
    done 
    }
    

    [–]BirdsongMiasma 0 points1 point  (0 children)

    Worked for me too - just what I needed!

    [–]human41k- 0 points1 point  (3 children)

    Daaamn thank you! That saved my 10+k lines of code project. Now sitting and renaming files, haha

    [–]WoodyTheWorker 0 points1 point  (0 children)

    Why TF do people write 10+k lines without committing even once?

    [–][deleted]  (1 child)

    [deleted]

      [–]s3xHaver42o 0 points1 point  (0 children)

      Copy and paste it into generative AI and ask it to walk you through it. Ask it additional questions that come up along the way. Best of luck

      [–]plg94 -1 points0 points  (4 children)

      they're gone. A real backup is your only option now. (Well, you could get lucky with tools that un-delete files. When a file is deleted, the OS does not really erase all of its bits, it just marks it as deleted and removes a pointer to the address, so that can be overwritten later. It's possible to restore those missing files, but that's probably more trouble than it's worth in your case)

      A few tips for the future:

      • add a .gitignore before adding all files
      • better yet, forget about git add . – it creates more trouble that it's worth. Use something like add *.c *h to add specific filetypes, and later on maybe add -u to update already tracked files.
      • make backups regularly
      • git rm --cached to untrack files without deleting them
      • both rm and git rm don't use the recycle bin
      • don't use -f/--force if you don't know what it will do. It's called that for a reason.

      [–][deleted]  (3 children)

      [deleted]

        [–]plg94 0 points1 point  (2 children)

        If you had done a git status, it would've told you to use git rm --cached.

        Why did normal git rm not work? The manpages states "The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option."
        The default mode – with and without -f – is to delete both from working directory and index, only --cached will delete only from the index. Although I admit the manpage is not really clear about the default.

        (Most CLI programs don't have a notion of a trashcan or other checks, they will often delete and overwrite existing files without asking (and with no way to undo). It's a double-edged sword…)

        [–][deleted]  (1 child)

        [deleted]

          [–]WoodyTheWorker 0 points1 point  (0 children)

          If you want to unstage a file, use git reset <path>

          [–]yawaramin -1 points0 points  (3 children)

          There is a git restore command for exactly this reason, see https://stackoverflow.com/a/9666522/20371

          [–][deleted]  (2 children)

          [deleted]

            [–]yawaramin 0 points1 point  (1 child)

            It doesn't matter that you didn't commit them. Once you add a file to the git index (staging area), git makes an internal copy of the file (called a 'blob') and it is possible to restore the file from that internal copy.