Skip to content
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

Commit is lost when switching from the local branch to the origin branch #1352

Closed
jtpio opened this issue Oct 10, 2024 · 4 comments
Closed
Labels

Comments

@jtpio
Copy link
Member

jtpio commented Oct 10, 2024

Description

Using the branches UI to switch between branches:

image

Seems to be reverting local changes.

Reproduce

  1. Clone a repo
  2. Make a change to a file
  3. Stage, commit
  4. Switch to the corresponding remote branch, for example origin/main
  5. Switch back to the local branch
  6. Changes are gone (the commit disappeared)
jupyterlab-git-branch-switch-issue.webm

Expected behavior

I would expect to be able to temporarely switch to the remote branch, and then switch back to the local branch and see the new changes again.

Context

  • JupyterLab: 4.2.5
  • Python package version: 0.50.1
  • Extension version: 0.50.1
  • Git version:
  • Operating System and its version:
@jtpio jtpio added the bug label Oct 10, 2024
@jtpio jtpio changed the title Commit is lost when switching to the origin branch Commit is lost when switching from the local branch to the origin branch Oct 10, 2024
@gjmooney
Copy link
Contributor

So this issue is caused by the -B option here. When you checkout a remote branch, you're not really checking out the remote, it creates a local copy based on the remote, and if a local copy already exists, it gets reset to the remote, hence the vanishing commit.

I've been trying to find a fix that maintains the current behavior of creating a local copy of the remote branch, but without losing any commits. I think the best so far is doing git checkout <local_branch_name> when clicking the remote, which will create a local copy based on the remote if it doesn't exist, and then doing a rebase after the checkout to sync the local branch with the remote. Obviously this could lead to conflicts, so I'm not sure if that's the way to go, but I guess it's better than losing commits.

@jtpio
Copy link
Member Author

jtpio commented Oct 24, 2024

To add more context, this is what happens when using the git command in a terminal directly, with the local main branch being one commit ahead of the remote branch:

$ git checkout upstream/main
Note: switching to 'upstream/main'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at b09e477 Create deploy.yml (#2)

Then running git checkout main restores the local main branch with the additional commit.

@jtpio
Copy link
Member Author

jtpio commented Oct 24, 2024

So this issue is caused by the -B option here. When you checkout a remote branch, you're not really checking out the remote, it creates a local copy based on the remote, and if a local copy already exists, it gets reset to the remote, hence the vanishing commit.

Right, wondering if this -B may be a little bit too aggressive?

It does seem to force the checkout of the branch, which does result in the loss of local changes: https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt--Bltnew-branchgt

Maybe a temporary workaround to not change the current behavior could be to show a confirmation dialog that would mention the potential loss of changes, and ask users if they want to proceed with the checkout of the remote branch.

@jtpio
Copy link
Member Author

jtpio commented Oct 25, 2024

Closing as fixed by #1366, thanks @gjmooney!

And opened #1368 as a follow-up.

@jtpio jtpio closed this as completed Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants