git checkout force deletes tracked directory containing untracked files · Issue #4125 · libgit2/libgit2 · GitHub
Skip to content

git checkout force deletes tracked directory containing untracked files #4125

Open

Description

@moyamo

Under certain conditions a forced checkout will delete untracked files by default.

Reproduction steps

Initialize a test git repo with one commit and a directory with a tracked file and an untracked file

mkdir test-repo
cd test-repo
git init

echo committed > committed
git add committed
git commit -m "Initial commit"

mkdir tracked-dir
echo "tracked" > tracked-dir/tracked-file
echo "untracked" > tracked-dir/untracked-file

git add tracked-dir/tracked-file

Compile and run a git checkout force program

#include <git2.h>

int main(int argc, char **argv)
{
        git_libgit2_init();
        git_repository *repo = NULL;
        git_repository_open(&repo, "./test-repo");

        git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
        opts.checkout_strategy = GIT_CHECKOUT_FORCE;
        git_checkout_head(repo, &opts);

        return 0;
}

Expected behavior

The file tracked-dir/tracked-file should be deleted. tracked-dir and tracked-dir/untracked-file should remain present. This is what git checkout --force does.

Actual behavior

tracked-dir is deleted. This means that the tracked-dir/untracked-file was also deleted. Checkout force should keep all untracked files by default.

Version of libgit2 (release number or SHA1)

a59545d

Operating system(s) tested

Linux (Ubuntu Xenial x86-64)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions