Skip to content

Hard Reset corrupts git directory if failed when cloned using git cli #878

@rohitsakala

Description

@rohitsakala

Steps to Reproduce

       openOptions := git.PlainOpenOptions{
		DetectDotGit: false,
	}

	// git clone --depth 1 https://git.rancher.io/charts --branch dev-v2.8
	repo, err := git.PlainOpenWithOptions("/Users/RSAKALA/temp/charts", &openOptions)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Ensure

	// Hard Reset to commit
	resetOpts := git.ResetOptions{
		Mode: git.HardReset,
	}
	resetOpts.Commit = plumbing.NewHash("6a2b15c6f6487b4db16f10d8ec69bb8bf91dcabd")

	// Validate hashCommit and reset options
	err = resetOpts.Validate(repo)
	if err != nil {
		fmt.Println("hardReset validation failure: %w", err)
	}

	// Open new worktree
	wt, err := repo.Worktree()
	if err != nil {
		fmt.Println("hardReset failure on WorkTree: %w", err)
	}

	// Reset
	err = wt.Reset(&resetOpts)
	if err != nil {
		fmt.Println("hardReset failure: %w", err)
	} else {
		fmt.Println("Hard Reset Worked")
		return
	}
  • Run git log

Output

fatal: bad object HEAD

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