Skip to content

Staged area modified after cross branch checkout #1355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fangpenlin opened this issue Mar 13, 2025 · 0 comments
Open

Staged area modified after cross branch checkout #1355

fangpenlin opened this issue Mar 13, 2025 · 0 comments

Comments

@fangpenlin
Copy link

fangpenlin commented Mar 13, 2025

I created two branches with commits like this:

Image

Then I call checkout to switch the branch from old_master to master. Somehow, the cached area is updated after checkout:

Image

Here's the code toe reproduce

import pathlib

import pygit2


def test_checkout(tmp_path: pathlib.Path):
    repo_dir = tmp_path

    git_author = pygit2.Signature(name="foo", email="[email protected]")
    repo = pygit2.init_repository(str(repo_dir))
    workdir = pathlib.Path(repo.workdir)
    readme = workdir / "README.md"

    readme.write_text("commit0")
    index = repo.index
    index.add(readme.relative_to(workdir).as_posix())
    index.write()
    tree = index.write_tree()
    commit0 = repo.create_commit("HEAD", git_author, git_author, "commit0", tree, [])

    master_branch = repo.branches.local["master"]
    old_master_branch = repo.branches.local.create("old_master", master_branch.peel())

    readme.write_text("commit1")
    index = repo.index
    index.add(readme.relative_to(workdir).as_posix())
    index.write()
    tree = index.write_tree()
    repo.create_commit(repo.head.name, git_author, git_author, "commit1", tree, [commit0])

    repo.checkout(old_master_branch)
    readme.write_text("commit2")
    index = repo.index
    index.add(readme.relative_to(workdir).as_posix())
    index.write()
    tree = index.write_tree()
    repo.create_commit(repo.head.name, git_author, git_author, "commit2", tree, [commit0])

    repo.checkout(master_branch)
    # the staged area should be empty after checkout
    assert not list(repo.index)

I tried it with git checkout master and it works fine without changing the staged area.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant