threads / bug / 58276

Init-add-reset bug in a fresh repo

Subject: Init-add-reset bug in a fresh repo

## tl;dr

2 messages between Aug 7, 2022 and Aug 7, 2022.

replies: 1people: 2as markdown or json

Alex A. Davronov· Aug 7, 2022, 17:01 UTC · lore
Steps to reproduce your issue

If you have files in a folder and then initiate-add-reset git then you lose everything:

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
What did you expect to happen? (Expected behavior)
- Files created prior to repo aren't getting removed
- OR
- git user is warned that the files are going to get deleted if
resetting hard indexed ones
What happened instead? (Actual behavior)
- Files are gone.
What's different between what you expected and what actually happened?
- Pre-existing files should be kept
Anything else you want to add:

Please review the rest of the bug report below. You can delete any lines you don't wish to share.

[System Info] git version: git version 2.37.1 cpu: x86_64 no commit associated with this build sizeof-long: 8 sizeof-size_t: 8 shell-path: /bin/sh uname: Linux 5.15.0-43-generic #46-Ubuntu SMP Tue Jul 12 10:30:17 UTC 2022 x86_64 compiler info: gnuc: 11.2 libc info: glibc: 2.35 $SHELL (typically, interactive shell): /usr/bin/zsh

[Enabled Hooks]
[Additional info]
See for reference:
- https://gist.github.com/hinell/e023bad4652364b477decb036fc6a60a
Reto· Aug 7, 2022, 17:22 UTC · re: Alex A. Davronov · lore

Re: Init-add-reset bug in a fresh repo

> What did you expect to happen? (Expected behavior)
> - Files created prior to repo aren't getting removed
> What happened instead? (Actual behavior)
> - Files are gone.
That's literally what you instructed git to do...

--hard really means that, reset I don't care YOLO, --force, just do it mate.

>--hard
>   Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.
>   Any untracked files or directories in the way of writing any tracked files are simply deleted.
In other words, the flag does exactly what it's meant to do.

Kind regards, Reto

← back to recent threads