wfxr / forgit

:zzz: A utility tool powered by fzf for using git interactively.
MIT License
4.33k stars 136 forks source link

Checkout remote branch #153

Closed thecuvii closed 3 years ago

thecuvii commented 3 years ago

Check list

Environment info

Problem / Steps to reproduce

Cannot checkout into remote branch

  1. gcb
  2. select remote branch, like: origin/feat1
wfxr commented 3 years ago

I can't repro. Seems works fine?

image

thecuvii commented 3 years ago

Xnip2021-06-01_20-34-59 Xnip2021-06-01_20-35-27 Weird. In my project, There are 'detached HEAD' problem. I'll try in other projects later. Thx.

thecuvii commented 3 years ago

I tried in forgit repo, there are still 'detached HEAD' problem.

image

wfxr commented 3 years ago

@Xwil This is expected. You are already switched to that branch. The message tells you the remote branch (theoretically read-only) is not actually a normal local branch and you can run git switch -c <new-branch-name> to create a corresponding local branch if necessary.

You will find that run git checkout origin/revert-117-master directly have the same result:

$ git clone https://github.com/wfxr/forgit
$ cd forgit
$ git checkout origin/revert-117-master
Note: switching to 'origin/revert-117-master'.

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 0f3cfda Revert "Attempt to make this a fish plugin"

This message seems to appear only once (so does the forgit::checkout::branch):

$ git checkout origin/dev
Previous HEAD position was 0f3cfda Revert "Attempt to make this a fish plugin"
HEAD is now at 29bc286 fix: improve compatibility for vim (#138)
thecuvii commented 3 years ago

Thanks.

What if forgit provide an option, when I try to checkout a remote branch, I can pass some arguments? like git checkout --track origin/revert-117-master.

In my case, I want to directly checkout into a remote branch, and I do not want to type git switch -c and branch name again.

wfxr commented 3 years ago

Thank you @Xwil. I understand your needs. Unfortunately there is not an easy way to achieve this.

What if forgit provide an option, when I try to checkout a remote branch, I can pass some arguments?.

gcb regard it's argument as the branch name for now,

https://github.com/wfxr/forgit/blob/d5e0fd86e946a3f147f21b9b7a3cef0e874ee684/forgit.plugin.zsh#L221

and the trouble is we can only checkout the same remote branch with --track once because the corresponding local branch will be created after the first run. So an option like FORGIT_LOG_GRAPH_ENABLE is also not applicable. And you want to temporarily checkout a remote branch that happenly has the same name as a local branch, this will also fail with --track option.

One way I can think of is to let user decide whether or not track it after selecting a remote branch. track the remote branch when possible.

wfxr commented 3 years ago

@Xwil I added the auto track logic in forgit::checkout::branch. You can update and try it.

thecuvii commented 3 years ago

Works fine, very nice work, thx.

wfxr commented 3 years ago

@cjappl I forgot to open a pr to implement this feature. You can look into the commit https://github.com/wfxr/forgit/commit/2db37aa4ecc94e41247a0eecb1b11896fa25cded to update the fish part when you have time.

crazy-matt commented 2 years ago

Proposing https://github.com/wfxr/forgit/pull/163 to make everyone happy