Why does Git allow pushing to a checked-out branch in an added worktree? How shall I recover?

后端 未结 1 521
梦毁少年i
梦毁少年i 2020-12-11 12:29

Git normally refuses pushing to the (single) currently-checked-out branch of a normal, non---bare repository:

$ git push upstream m         


        
相关标签:
1条回答
  • 2020-12-11 12:59
    1. Yes, it is (obviously) a bug: Git's "is this a current branch" test, in the receive pack code, is using the pre-multiple-work-trees test, which is just to check the main work-tree's HEAD. It needs to be modified to check all work-trees the same way git worktree list does.

      (I'm working on two systems connected via VPN where only one of them has a server running at the moment, so I'm using an asymmetric fetch/push model here.)

    2. Fortunately, I had no uncommitted work, so recovery was trivial: in the secondary work-tree (on branch pytest), just run:

      git reset --hard HEAD
      

      which cleans up both the index and work-tree.

      If I were not, however, the easiest trick would be to force a new branch to exist at the previous value of the branch, then check out that branch. This can be done in one step:

      git checkout -b tempbranch pytest@{1}
      

      The work-tree would now be on a different (pre-push) branch, where temporary work can now be committed or stashed or rebased or whatever, and dealt with in the usual way.

    0 讨论(0)
提交回复
热议问题