Skip to content

Instantly share code, notes, and snippets.

@hinell
Created August 7, 2022 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hinell/e023bad4652364b477decb036fc6a60a to your computer and use it in GitHub Desktop.
Save hinell/e023bad4652364b477decb036fc6a60a to your computer and use it in GitHub Desktop.

Init-add-reset bug in GIT

Abstract

This is a description of a bug I've discovered in git version 2.37.1

Description

Imagine you have a folder where you worked on some files. Now you want to turn the folder into a git repo.

If you have files in a folder and then initiate-add-reset git commands you lose everything you had in the folder.

Run the following in your bash shell to see:

    clear; \
    [[ -d /tmp/evilgit ]] && rm -rdI /tmp/evilgit; \
    mkdir /tmp/evilgit/; \
    cd /tmp/evilgit/; \
    echo "----------------- Create some files:"; \
    pwd; \
    echo -n "ls -l: "; \
    touch a.txt b.txt c.txt; \
    ls; \
    echo "----------------- Now, init the repo:"; \
    echo -n "git init: "; \
    git init 2> /dev/null; \
    echo "git add: a.txt b.txt c.txt"; \
    git add ./*; \
    echo -n "git reset --hard: "; \
    git reset --hard; \
    echo "\n----------------- Check files again:"; \
    echo "ls -l: "; \
    ls; \
    echo "No files! " ; \
    echo "\n----------------- Now, all the files we have created are gone!"; \
    cd .. ; \
    rm -rdI /tmp/evilgit

How did I discover this?

I had som side-project with snippets I've gathered over time. Then I decided to push my files into several repos and then I've initiated git repo and added these files for tracking, but I didn't commit them! Instead, I decided to --hard reset and everything was gone! No way to recover them! You can't use reflog or restore or anything!

Thankfully, I made few backups before that so files aren't lost. Imagine magnitude of the issue if that was some important project.

I've repoted the issue to the git@vger.kernel.org on August 7, 2022.


Sunday, August 7, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment