Skip to content

Conversation

@kyu08
Copy link
Contributor

@kyu08 kyu08 commented Jul 12, 2025

PR Description

I added a new feature to checkout to the last branch.
I think it would be convenient to be able to checkout to the last branch with just one key stroke(-) in some cases, such as when switching between multiple branches during code review.

Off course, I know that you can same thing by entering "-" to the text input of "Checkout by name" or using custom command but I think git checkout - is occasional use case. So I feel this feature can be a part of default features.

Another advantage of this feature is that you can checkout to the last branch regardless of cursor position.
Being able to switch branches without worrying about cursor position reduces stress I think.

This is a demo that showing switching between checkout-previous-branch and foo branch.

2025-07-12.at.15.55.37.mov

Please check if the PR fulfills these requirements

  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see here for the integration test guide)
  • Text is internationalised (see here)
  • If a new UserConfig entry was added, make sure it can be hot-reloaded (see here)
    • I could not confirm how to enable hot reload for the keybinding config I added. Please tell me something I should do if needed.
  • Docs have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

@stefanhaller
Copy link
Collaborator

Thanks for the contribution. Nice test coverage.

I use c - enter in the branches panel quite frequently myself, and I have sometimes wondered whether it should be built in to lazygit to make it more discoverable. However, I think I'm against adding this, for a few reasons:

  • it uses a quite precious key, and I'm concerned that we might want to use it for something else in the future (like, for example, folding merge commits in the commits panel if we ever figure out how this might be implemented), and then regret that the key is already taken. (Oh, and it is being used already for Collapse All in the files panel.)
  • having it available globally feels wrong to me; this is functionality that belongs in the branches panel.
  • it strikes me as too error prone to hit accidentally. While it's usually harmless, it could still be confusing, and it could be really annoying if you're in the middle of a rebase. But adding a confirmation popup would somehow defeat the purpose of saving keystrokes.

This is very easy to do with a custom command:

customCommands:
  - key: "-"
    command: git checkout -
    context: global
    description: "Checkout previous branch"

@kyu08
Copy link
Contributor Author

kyu08 commented Jul 12, 2025

Thanks for the quick response.

  • it uses a quite precious key, and I'm concerned that we might want to use it for something else in the future (like, for example, folding merge commits in the commits panel if we ever figure out how this might be implemented), and then regret that the key is already taken. (Oh, and it is being used already for Collapse All in the files panel.)
  • having it available globally feels wrong to me; this is functionality that belongs in the branches panel.
  • it strikes me as too error prone to hit accidentally. While it's usually harmless, it could still be confusing, and it could be really annoying if you're in the middle of a rebase. But adding a confirmation popup would somehow defeat the purpose of saving keystrokes.

Just to be clear, this PR allows - key to work only in local branches context. So the influence on other functionality is not huge. Of cource I understand that it is not that there is no effect.

Even if someone presses - key in the branches panel, in many cases they might be able to get back to the previous state to press - once again.

On that premise, is ther any chance you would consider merging this?

Of course, I respect your decision but I just want to avoid any misunderstandings.

@stefanhaller
Copy link
Collaborator

Just to be clear, this PR allows - key to work only in local branches context.

Oh, ok. 😄 I didn't even read the diff, I was mislead by the "checkout to the last branch regardless of cursor position" in the PR description, which I understood as the command being available globally.

Ok then, I'd be fine with adding this. A few things:

  • is "last branch" the best term to use? I wonder if "previous branch" is better. But I'm not a native English speaker...
  • The change to the japanese language file needs to be dropped. We don't change translations here in git, this happens on https://crowdin.com/project/lazygit. The second commit should be renamed to "update keybindings and schema" or some such, and only contain the changes from running make generate. If you then want to provide a japanese translation, that would have to happen on crowdin, but I need to upload the english language file first, after merging, before you can do that. BTW, if you want to contribute to finishing or improving the japanese translation on crowdin, that would be very welcome.

CheckoutByName: "Checkout by name",
CheckoutByNameTooltip: "Checkout by name. In the input box you can enter '-' to switch to the last branch.",
CheckoutLastBranch: "Checkout last branch",
CheckoutLastBranchTooltip: "Checkout to the last branch.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't find this tooltip very valuable when it adds almost nothing beyond what's already in the name of the command. I'd either drop it, or make it more elaborate, e.g. "Checkout the previously checked out branch again" or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.
I just added tooltip description somehow. I'll delete it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have deleted it from this PR diff.

@kyu08
Copy link
Contributor Author

kyu08 commented Jul 12, 2025

Oh, ok. 😄 I didn't even read the diff, I was mislead by the "checkout to the last branch regardless of cursor position" in the PR description, which I understood as the command being available globally.

Ah... it's my fault. I'm sorry for that misleading description.

is "last branch" the best term to use? I wonder if "previous branch" is better. But I'm not a native English speaker...

I feel previous is better too. I choose it just because last is used here.

I couldn't find useful reference in git branch --help, but I found previous is used here like below.

git checkout - checks out the previously checked out branch.

I will replace all names to using previous including the commit message of first commit. I think this PR content is not too complicated, so I'll force-push for entire cleanness.

The change to the japanese language file needs to be dropped. We don't change translations here in git, this happens on https://crowdin.com/project/lazygit. The second commit should be renamed to "update keybindings and schema" or some such, and only contain the changes from running make generate. If you then want to provide a japanese translation, that would have to happen on crowdin, but I need to upload the english language file first, after merging, before you can do that.

OK. I see. I'll do it.

BTW, if you want to contribute to finishing or improving the japanese translation on crowdin, that would be very welcome.

Yes! I'd love to contribute to lazygit. So I'd be grad to help. Is there any document for starting contributing to translation on cronwdin?

@stefanhaller
Copy link
Collaborator

I choose it just because last is used here.

I see. If you want, you might add a separate commit that changes it to "previous" there, too.

I think this PR content is not too complicated, so I'll force-push for entire cleanness.

Sure, that's fine in this case.

Is there any document for starting contributing to translation on cronwdin?

No, we have been working with this in a very informal way so far. Just make an account and edit away. 😄

@kyu08 kyu08 changed the title Add new command "Checkout last branch" Add new command "Checkout previous branch" Jul 12, 2025
@kyu08 kyu08 force-pushed the checkout-previous-branch branch 3 times, most recently from b336cf4 to b4fafa7 Compare July 12, 2025 16:04
@kyu08
Copy link
Contributor Author

kyu08 commented Jul 12, 2025

I see. If you want, you might add a separate commit that changes it to "previous" there, too.

Fixed in 06632ed.

I'm done with all the fixes. Please review that all changes are fine.

No, we have been working with this in a very informal way so far. Just make an account and edit away. 😄

I joined to the project on crowdin. I'll find some time to look into how to use it.

@stefanhaller stefanhaller added the enhancement New feature or request label Jul 12, 2025
@stefanhaller
Copy link
Collaborator

Thanks for your work. I pushed one more fixup (a0a74e4). Will merge when CI passes.

@kyu08
Copy link
Contributor Author

kyu08 commented Jul 12, 2025

@stefanhaller
Ah! Thanks for the follow up! a0a74e4

@stefanhaller stefanhaller force-pushed the checkout-previous-branch branch from a0a74e4 to d41668f Compare July 12, 2025 16:18
@stefanhaller stefanhaller enabled auto-merge July 12, 2025 16:18
@stefanhaller stefanhaller merged commit 1795cb9 into jesseduffield:master Jul 12, 2025
13 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Aug 4, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [jesseduffield/lazygit](https://github.com/jesseduffield/lazygit) | minor | `v0.53.0` -> `v0.54.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>jesseduffield/lazygit (jesseduffield/lazygit)</summary>

### [`v0.54.0`](https://github.com/jesseduffield/lazygit/releases/tag/v0.54.0)

[Compare Source](jesseduffield/lazygit@v0.53.0...v0.54.0)

<!-- Release notes generated using configuration in .github/release.yml at v0.54.0 -->

Again we don't have any major new features this time (unless you count the support for alt-backspace for deleting words in the commit message editor, which is one of my favorite additions), but lots of smaller quality-of-life improvements and bug fixes. The most notable one is probably the fix for the stale index.lock problem, which was a very long-standing bug that seemed to affect some users much more than others for some reason.

#### What's Changed

##### Enhancements 🔥

- Add confirmation for hard reset by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4704
- Provide user config defaults for UI-changeable settings by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4717
- Improve mouse handling of suggestions panel by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4726
- Add new command "Checkout previous branch" by [@&#8203;kyu08](https://github.com/kyu08) in jesseduffield/lazygit#4728
- Add confirmation for nuking the working tree by [@&#8203;DawidPietrykowski](https://github.com/DawidPietrykowski) in jesseduffield/lazygit#4727
- Support Alt+Backspace for word deletion in text areas by [@&#8203;rtzll](https://github.com/rtzll) in jesseduffield/lazygit#4741
- Don't use hunk mode for added or deleted files even when useHunkModeInStagingView config is on by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4758
- Show \[0] keybinding in main view title by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4754
- Draw divergence from base branch right-aligned in branches view by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4785
- Enable hunk staging mode by default by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4780

##### Fixes 🔧

- Fix scrolling hunk into view when selecting next hunk by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4709
- Fix stale main view content when entering/exiting filtering view by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4719
- Detect double-clicks properly by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4725
- Fix commit searching during rebase or in divergence from upstream view by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4730
- Fix amending commits whose commit message is empty by [@&#8203;aidancz](https://github.com/aidancz) in jesseduffield/lazygit#4732
- Several small fixes to filtering mode (by path or author) by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4749
- Show diff for renamed file when filtering by path by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4750
- Allow rewording or dropping commits in filtering mode by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4756
- Fix index out of bounds panic when repository has massive tags by [@&#8203;chojs23](https://github.com/chojs23) in jesseduffield/lazygit#4776
- When pressing `a` to stage all files, don't include untracked files when showing only tracked files by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4779
- Fix commit hash colors when filtering by path or aythor by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4789
- Improve temp dir handling by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4784
- Terminate git processes more gracefully to avoid the stale index.lock problem by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4782

##### Maintenance ⚙️

- Raise sponsors MRs as a draft by [@&#8203;jesseduffield](https://github.com/jesseduffield) in jesseduffield/lazygit#4694
- Update the peter-evans/create-pull-request action to v7 by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4695
- Update release workflow by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4703
- Clean up the .gitignore file by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4706
- Remove unused code and texts by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4715
- Remove deprecated edit configs by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4716
- Bump minimum required git version to 2.32 by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4718
- Use a better way of pinning the version of golangci-lint by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4733
- Make the minimum required git version a placeholder in the error text by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4778
- refactor: use slices.Equal to simplify code by [@&#8203;jishudashu](https://github.com/jishudashu) in jesseduffield/lazygit#4764

##### Docs 📖

- Fix broken markdown in auto-generated keybindings documentation by [@&#8203;KEY60228](https://github.com/KEY60228) in jesseduffield/lazygit#4690
- Remove the homebrew tap from the readme by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4705

##### I18n 🌎

- Update translations from Crowdin by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4791

##### Performance Improvements 📊

- Fix performance regression on startup in repos with many tags by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4777

#### New Contributors

- [@&#8203;KEY60228](https://github.com/KEY60228) made their first contribution in jesseduffield/lazygit#4690
- [@&#8203;DawidPietrykowski](https://github.com/DawidPietrykowski) made their first contribution in jesseduffield/lazygit#4727
- [@&#8203;rtzll](https://github.com/rtzll) made their first contribution in jesseduffield/lazygit#4741
- [@&#8203;chojs23](https://github.com/chojs23) made their first contribution in jesseduffield/lazygit#4776
- [@&#8203;jishudashu](https://github.com/jishudashu) made their first contribution in jesseduffield/lazygit#4764

**Full Changelog**: jesseduffield/lazygit@v0.53.0...v0.54.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS40Ni43IiwidXBkYXRlZEluVmVyIjoiNDEuNDYuNyIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants