App-GitHubPullRequest icon indicating copy to clipboard operation
App-GitHubPullRequest copied to clipboard

"checkout" should switch to the local branch if it already exists

Open moollaza opened this issue 10 years ago • 3 comments

Sometimes I forget which branches I've already checked out. If I try to git pr checkout a branch that already exists locally, the checkout fails, and I'm told it already exists, which is okay.

It would be pretty awesome though, if it just checked out the local branch for me when it already exists, because that's exactly what I'll need to do when this situation occurs.

moollaza avatar Oct 03 '14 16:10 moollaza

Sorry about the extremely late response. This is a good idea. How would you go about performing this check for an existing branch to avoid the error. Do you have a suggestion for which git commands to run to add this feature (in a way that works if people use a non-English terminal language).

robinsmidsrod avatar Apr 30 '18 08:04 robinsmidsrod

@robinsmidsrod when the checkout fails, the name of the branch can be constructed from the number provided by the user, ie. "pr/$input".

If the checkout fails because the branch exists, running "git checkout pr/$input" should work. I assume git's error 128 means the branch exists, and we know what the error number is.

(in a way that works if people use a non-English terminal language) I'm not sure if this works for non-English terminals. I would assume so though?


Here's an example:

~/projects/foo master
❯ git pr checkout 360
Fetching changes from 'foo/bar'
fatal: A branch named 'pr/360' already exists.
git failed with error 128 when trying to check out branch.

~/projects/foo master
❯ git checkout pr/360
Switched to branch 'pr/360'
Your branch is up to date with 'foo/bar'.

moollaza avatar Apr 30 '18 14:04 moollaza

I assume git's error 128 means the branch exists, and we know what the error number is.

I'm wrong. Apparently it just means it was an abnormal exit.

I suppose you could try checking out the branch locally, before creating the new branch?

moollaza avatar Apr 30 '18 14:04 moollaza