You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Proposals for how to checkout a branch with HEAD detached #4291
I would like there to be an interactive way to checkout a branch with HEAD detached, as with git checkout --detach. Below are two proposals for how this could be done.
For an example of why one would want to do this, see this SO answer by me.
Thanks
edit: Apologize for the many edits, I accidentally published my issue prematurely.
If the user tries to checkout a branch which is already checked out in another worktree, the user would be prompted something like this: "The branch master is already checkout out in worktree wrktr, do you wish to check it out head detached (y/n)?". There could be a variable that if set, skipped the prompt and always checked out with detached head or didn't check out at all.
An alternative under magit-branch > checkout > "local branch HEAD detached". I am less in favor of this.
changed the title [-]Checkout branch detached if user tries to checkout a branch which is already checked out in another worktree[/-][+]Proposals for checking out branches with HEAD detached[/+]on Jan 13, 2021
changed the title [-]Proposals for checking out branches with HEAD detached[/-][+]Proposals for how to checkout a branch with HEAD detached[/+]on Jan 13, 2021
PS: If this is of interest I will gladly try implementing it myself. Just tell me which proposal, or how you think it would best be implemented, and reserve it for me for some amount of time and I will see what I can do :-).
You can already check out the commit at point using b b followed by M-n (next-history-element) and then RET.
This is a technique is not my invention, some commands that are part of Emacs also use the initial "next history element" to provide some secondary default like this.
I can't see your screencast, but your point needs to be at the commit hash for the M-n trick to work. Another approach: you can append ^0 (short for ^{commit}) to a ref to dereference the object, getting the associated commit (e.g., b b master^0 will created a detached head at the commit that master points to).
I can't see your screencast, but your point needs to be at the commit hash for the M-n trick to work.
Hmm, I tried it with point at the commit hash. For commits without branches on them, I get the hash, otherwise I get branch name.
Another approach: you can append ^0 (short for ^{commit}) to a ref to dereference the object, getting the associated commit (e.g., b b master^0 will created a detached head at the commit that master points to).
That works and is indeed an interactive way of checking out a branch headlessly. Sorry for imlplying that it was not possible interactively, I concede, thanks for showing me.
Either way, I still think that prompting the user to checkout a branch headlessly, when trying to checkout a branch already checked out in another worktree, would be an improvement compared to just failing with a git error.
I will close this issue now and request this behaviour in a clean new issue.
your point needs to be at the commit hash for the M-n trick to work
I cannot figure out where that is implemented anymore. Please point me to the code that makes that happen. 😊
I am trying to figure out why this works with ivy-mode but apparently not selectrum-mode. I would have expected something like in magit-submodule-read-name-for-path, but cannot see anything like that in this case.
Please point me to the code that makes that happen
I'm using ivy, and, taking another look, it looks like the behavior comes from the ivy's wrapper for next-history-element (which explains why @fossegrim reported not seeing the behavior). The commit at point is inserted by the marked line below:
We probably should implement this for the default completion mechanism as well. But I am currently staring down the rabbit hole that is completion and am not sure I want to take a leap right now. It's pretty scary.
Activity
[-]Checkout branch detached if user tries to checkout a branch which is already checked out in another worktree[/-][+]Proposals for checking out branches with HEAD detached[/+][-]Proposals for checking out branches with HEAD detached[/-][+]Proposals for how to checkout a branch with HEAD detached[/+]olavfosse commentedon Jan 13, 2021
PS: If this is of interest I will gladly try implementing it myself. Just tell me which proposal, or how you think it would best be implemented, and reserve it for me for some amount of time and I will see what I can do :-).
tarsius commentedon Jan 13, 2021
You can already check out the commit at point using
b bfollowed byM-n(next-history-element) and thenRET.This is a technique is not my invention, some commands that are part of Emacs also use the initial "next history element" to provide some secondary default like this.
tarsius commentedon Jan 13, 2021
Of course I meant
b b...olavfosse commentedon Jan 13, 2021
I don't understand how that allows me to check out a branch HEAD detached.
Screencast.2021-01-13.13.35.16.mp4
tarsius commentedon Jan 13, 2021
25f432551 * origin/master magit-commit-ask-to-stage: Reduce width of doc-string.b b. This gives you a minibuffer with contentCheckout:.M-n. This changes the minibuffer's content toCheckout: 25f432551.RETto checkout25f432551directly.olavfosse commentedon Jan 13, 2021
That does not work for me, even with
emacs -qI just getorigin/diff-parse.kyleam commentedon Jan 14, 2021
I can't see your screencast, but your point needs to be at the commit hash for the
M-ntrick to work. Another approach: you can append^0(short for^{commit}) to a ref to dereference the object, getting the associated commit (e.g.,b b master^0will created a detached head at the commit that master points to).olavfosse commentedon Jan 14, 2021
Hmm, I tried it with point at the commit hash. For commits without branches on them, I get the hash, otherwise I get branch name.
That works and is indeed an interactive way of checking out a branch headlessly. Sorry for imlplying that it was not possible interactively, I concede, thanks for showing me.
Either way, I still think that prompting the user to checkout a branch headlessly, when trying to checkout a branch already checked out in another worktree, would be an improvement compared to just failing with a git error.
I will close this issue now and request this behaviour in a clean new issue.
Thanks.
tarsius commentedon Feb 17, 2021
I cannot figure out where that is implemented anymore. Please point me to the code that makes that happen. 😊
I am trying to figure out why this works with
ivy-modebut apparently notselectrum-mode. I would have expected something like inmagit-submodule-read-name-for-path, but cannot see anything like that in this case.Hm, I should probably go to bed.
kyleam commentedon Feb 18, 2021
I'm using ivy, and, taking another look, it looks like the behavior comes from the ivy's wrapper for
next-history-element(which explains why @fossegrim reported not seeing the behavior). The commit at point is inserted by the marked line below:And
ivy--defaultis set to the thing at point inivy--reset-state:tarsius commentedon Feb 18, 2021
Ha! So we both thought we had implemented this.
We probably should implement this for the default completion mechanism as well. But I am currently staring down the rabbit hole that is completion and am not sure I want to take a leap right now. It's pretty scary.
I'll reopen this issue as a reminder.
phil-s commentedon Mar 1, 2021
As a workaround, with point at the hash in question, you can of course do this:
M-d b b C-y RETAdd additional default when reading branch or commit