From 5b1292006d81571b5e2721993f0f28539a511c92 Mon Sep 17 00:00:00 2001 From: kyu08 <49891479+kyu08@users.noreply.github.com> Date: Sun, 13 Jul 2025 00:42:01 +0900 Subject: [PATCH 1/3] Add new command "Checkout previous branch" --- pkg/config/user_config.go | 2 + pkg/gui/controllers/branches_controller.go | 10 ++++ pkg/i18n/english.go | 2 + .../tests/branch/checkout_previous_branch.go | 51 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 5 files changed, 66 insertions(+) create mode 100644 pkg/integration/tests/branch/checkout_previous_branch.go diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 5a7838601..5465a376c 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -497,6 +497,7 @@ type KeybindingBranchesConfig struct { CopyPullRequestURL string `yaml:"copyPullRequestURL"` CheckoutBranchByName string `yaml:"checkoutBranchByName"` ForceCheckoutBranch string `yaml:"forceCheckoutBranch"` + CheckoutPreviousBranch string `yaml:"checkoutPreviousBranch"` RebaseBranch string `yaml:"rebaseBranch"` RenameBranch string `yaml:"renameBranch"` MergeIntoCurrentBranch string `yaml:"mergeIntoCurrentBranch"` @@ -957,6 +958,7 @@ func GetDefaultConfig() *UserConfig { ViewPullRequestOptions: "O", CheckoutBranchByName: "c", ForceCheckoutBranch: "F", + CheckoutPreviousBranch: "-", RebaseBranch: "r", RenameBranch: "R", MergeIntoCurrentBranch: "M", diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go index ef27ba01d..ed2b01509 100644 --- a/pkg/gui/controllers/branches_controller.go +++ b/pkg/gui/controllers/branches_controller.go @@ -89,6 +89,11 @@ func (self *BranchesController) GetKeybindings(opts types.KeybindingsOpts) []*ty Description: self.c.Tr.CheckoutByName, Tooltip: self.c.Tr.CheckoutByNameTooltip, }, + { + Key: opts.GetKey(opts.Config.Branches.CheckoutPreviousBranch), + Handler: self.checkoutPreviousBranch, + Description: self.c.Tr.CheckoutPreviousBranch, + }, { Key: opts.GetKey(opts.Config.Branches.ForceCheckoutBranch), Handler: self.forceCheckout, @@ -483,6 +488,11 @@ func (self *BranchesController) forceCheckout() error { return nil } +func (self *BranchesController) checkoutPreviousBranch() error { + self.c.LogAction(self.c.Tr.Actions.CheckoutBranch) + return self.c.Helpers().Refs.CheckoutRef("-", types.CheckoutRefOptions{}) +} + func (self *BranchesController) checkoutByName() error { self.c.Prompt(types.PromptOpts{ Title: self.c.Tr.BranchName + ":", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index bfac1fc20..7259ae372 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -139,6 +139,7 @@ type TranslationSet struct { ForceCheckoutTooltip string CheckoutByName string CheckoutByNameTooltip string + CheckoutPreviousBranch string RemoteBranchCheckoutTitle string RemoteBranchCheckoutPrompt string CheckoutTypeNewBranch string @@ -1182,6 +1183,7 @@ func EnglishTranslationSet() *TranslationSet { ForceCheckoutTooltip: "Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch.", CheckoutByName: "Checkout by name", CheckoutByNameTooltip: "Checkout by name. In the input box you can enter '-' to switch to the last branch.", + CheckoutPreviousBranch: "Checkout previous branch", RemoteBranchCheckoutTitle: "Checkout {{.branchName}}", RemoteBranchCheckoutPrompt: "How would you like to check out this branch?", CheckoutTypeNewBranch: "New local branch", diff --git a/pkg/integration/tests/branch/checkout_previous_branch.go b/pkg/integration/tests/branch/checkout_previous_branch.go new file mode 100644 index 000000000..8a8698d85 --- /dev/null +++ b/pkg/integration/tests/branch/checkout_previous_branch.go @@ -0,0 +1,51 @@ +package branch + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var CheckoutPreviousBranch = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Checkout to the previous branch using the checkout previous branch functionality", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell. + CreateNCommits(3). + NewBranch("previous-branch"). + EmptyCommit("previous commit"). + Checkout("master"). + EmptyCommit("master commit") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Branches(). + Focus(). + Lines( + Contains("master").IsSelected(), + Contains("previous-branch"), + ) + + // Press the checkout previous branch key (should checkout previous-branch) + t.Views().Branches(). + Press(keys.Branches.CheckoutPreviousBranch). + Lines( + Contains("previous-branch").IsSelected(), + Contains("master"), + ) + + // Verify we're on previous-branch + t.Git().CurrentBranchName("previous-branch") + + // Press again to go back to master + t.Views().Branches(). + Press(keys.Branches.CheckoutPreviousBranch). + Lines( + Contains("master").IsSelected(), + Contains("previous-branch"), + ) + + // Verify we're back on master + t.Git().CurrentBranchName("master") + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 9de69d3b0..d5f4f5c21 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -40,6 +40,7 @@ var tests = []*components.IntegrationTest{ bisect.Skip, branch.CheckoutAutostash, branch.CheckoutByName, + branch.CheckoutPreviousBranch, branch.CreateTag, branch.Delete, branch.DeleteMultiple, From c75d92f2704fbae7fa4650ee43dfc1ddfae8e191 Mon Sep 17 00:00:00 2001 From: kyu08 <49891479+kyu08@users.noreply.github.com> Date: Sun, 13 Jul 2025 00:42:16 +0900 Subject: [PATCH 2/3] Update cheatsheets and schema --- docs/Config.md | 1 + docs/keybindings/Keybindings_en.md | 1 + docs/keybindings/Keybindings_ja.md | 1 + docs/keybindings/Keybindings_ko.md | 1 + docs/keybindings/Keybindings_nl.md | 1 + docs/keybindings/Keybindings_pl.md | 1 + docs/keybindings/Keybindings_pt.md | 1 + docs/keybindings/Keybindings_ru.md | 1 + docs/keybindings/Keybindings_zh-CN.md | 1 + docs/keybindings/Keybindings_zh-TW.md | 1 + schema/config.json | 4 ++++ 11 files changed, 14 insertions(+) diff --git a/docs/Config.md b/docs/Config.md index 77a3932ab..03706e95b 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -627,6 +627,7 @@ keybinding: copyPullRequestURL: checkoutBranchByName: c forceCheckoutBranch: F + checkoutPreviousBranch: '-' rebaseBranch: r renameBranch: R mergeIntoCurrentBranch: M diff --git a/docs/keybindings/Keybindings_en.md b/docs/keybindings/Keybindings_en.md index 24c0b65f6..5cc2e30c6 100644 --- a/docs/keybindings/Keybindings_en.md +++ b/docs/keybindings/Keybindings_en.md @@ -171,6 +171,7 @@ _Legend: `` means ctrl+b, `` means alt+b, `B` means shift+b_ | `` O `` | View create pull request options | | | `` `` | Copy pull request URL to clipboard | | | `` c `` | Checkout by name | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` - `` | Checkout previous branch | | | `` F `` | Force checkout | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | Delete | View delete options for local/remote branch. | | `` r `` | Rebase | Rebase the checked-out branch onto the selected branch. | diff --git a/docs/keybindings/Keybindings_ja.md b/docs/keybindings/Keybindings_ja.md index 0d71062c3..0fa74a23c 100644 --- a/docs/keybindings/Keybindings_ja.md +++ b/docs/keybindings/Keybindings_ja.md @@ -367,6 +367,7 @@ _凡例:`<c-b>` はctrl+b、`<a-b>` はalt+b、`B` はshift+bを意味 | `` O `` | プルリクエスト作成オプションを表示 | | | `` `` | プルリクエストURLをクリップボードにコピー | | | `` c `` | 名前でチェックアウト | 名前でチェックアウトします。入力ボックスに「-」を入力すると、最後のブランチをチェックアウトすることができます。 | +| `` - `` | Checkout previous branch | | | `` F `` | 強制チェックアウト | 選択したブランチを強制的にチェックアウトします。これにより、選択したブランチをチェックアウトする前にワーキングディレクトリ内のすべてのローカル変更が破棄されます。 | | `` d `` | 削除 | ローカル/リモートブランチの削除オプションを表示します。 | | `` r `` | リベース | チェックアウトしたブランチを選択したブランチ上にリベースします。 | diff --git a/docs/keybindings/Keybindings_ko.md b/docs/keybindings/Keybindings_ko.md index 655ea308b..43556588f 100644 --- a/docs/keybindings/Keybindings_ko.md +++ b/docs/keybindings/Keybindings_ko.md @@ -207,6 +207,7 @@ _Legend: `` means ctrl+b, `` means alt+b, `B` means shift+b_ | `` O `` | 풀 리퀘스트 생성 옵션 | | | `` `` | 풀 리퀘스트 URL을 클립보드에 복사 | | | `` c `` | 이름으로 체크아웃 | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` - `` | Checkout previous branch | | | `` F `` | 강제 체크아웃 | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | 삭제 | View delete options for local/remote branch. | | `` r `` | 체크아웃된 브랜치를 이 브랜치에 리베이스 | Rebase the checked-out branch onto the selected branch. | diff --git a/docs/keybindings/Keybindings_nl.md b/docs/keybindings/Keybindings_nl.md index 474582d37..65ff02a2c 100644 --- a/docs/keybindings/Keybindings_nl.md +++ b/docs/keybindings/Keybindings_nl.md @@ -104,6 +104,7 @@ _Legend: `` means ctrl+b, `` means alt+b, `B` means shift+b_ | `` O `` | Bekijk opties voor pull-aanvraag | | | `` `` | Kopieer de URL van het pull-verzoek naar het klembord | | | `` c `` | Uitchecken bij naam | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` - `` | Checkout previous branch | | | `` F `` | Forceer checkout | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | Delete | View delete options for local/remote branch. | | `` r `` | Rebase branch | Rebase the checked-out branch onto the selected branch. | diff --git a/docs/keybindings/Keybindings_pl.md b/docs/keybindings/Keybindings_pl.md index 53350645b..4d8fe74ad 100644 --- a/docs/keybindings/Keybindings_pl.md +++ b/docs/keybindings/Keybindings_pl.md @@ -137,6 +137,7 @@ _Legenda: `` oznacza ctrl+b, `` oznacza alt+b, `B` oznacza shift+b_ | `` O `` | Zobacz opcje tworzenia pull requesta | | | `` `` | Kopiuj adres URL żądania ściągnięcia do schowka | | | `` c `` | Przełącz według nazwy | Przełącz według nazwy. W polu wprowadzania możesz wpisać '-' aby przełączyć się na ostatnią gałąź. | +| `` - `` | Checkout previous branch | | | `` F `` | Wymuś przełączenie | Wymuś przełączenie wybranej gałęzi. To spowoduje odrzucenie wszystkich lokalnych zmian w drzewie roboczym przed przełączeniem na wybraną gałąź. | | `` d `` | Usuń | Wyświetl opcje usuwania lokalnej/odległej gałęzi. | | `` r `` | Przebazuj | Przebazuj przełączoną gałąź na wybraną gałąź. | diff --git a/docs/keybindings/Keybindings_pt.md b/docs/keybindings/Keybindings_pt.md index b5527c602..641d3d033 100644 --- a/docs/keybindings/Keybindings_pt.md +++ b/docs/keybindings/Keybindings_pt.md @@ -97,6 +97,7 @@ _Legend: `` means ctrl+b, `` means alt+b, `B` means shift+b_ | `` O `` | View create pull request options | | | `` `` | Copiar URL do pull request para área de transferência | | | `` c `` | Checar por nome | Checar por nome. Na caixa de entrada você pode inserir '-' para trocar para a última branch | +| `` - `` | Checkout previous branch | | | `` F `` | Forçar checagem | Forçar checagem da branch selecionada. Isso irá descartar todas as mudanças no seu diretório de trabalho antes cheque a branch selecionada | | `` d `` | Apagar | Ver opções de exclusão para a branch local/remoto. | | `` r `` | Refazer | Refazer a branch checada na branch selecionada | diff --git a/docs/keybindings/Keybindings_ru.md b/docs/keybindings/Keybindings_ru.md index 7eafc1f5e..befa85d47 100644 --- a/docs/keybindings/Keybindings_ru.md +++ b/docs/keybindings/Keybindings_ru.md @@ -205,6 +205,7 @@ _Связки клавиш_ | `` O `` | Создать параметры запроса принятие изменений | | | `` `` | Скопировать URL запроса на принятие изменений в буфер обмена | | | `` c `` | Переключить по названию | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` - `` | Checkout previous branch | | | `` F `` | Принудительное переключение | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | Delete | View delete options for local/remote branch. | | `` r `` | Перебазировать переключённую ветку на эту ветку | Rebase the checked-out branch onto the selected branch. | diff --git a/docs/keybindings/Keybindings_zh-CN.md b/docs/keybindings/Keybindings_zh-CN.md index b5bbdc3aa..b5c4769ff 100644 --- a/docs/keybindings/Keybindings_zh-CN.md +++ b/docs/keybindings/Keybindings_zh-CN.md @@ -228,6 +228,7 @@ _图例:`` 意味着ctrl+b, `意味着Alt+b, `B` 意味着shift+b_ | `` O `` | 创建拉取请求选项 | | | `` `` | 复制拉取请求 URL 到剪贴板 | | | `` c `` | 按名称检出 | 按名称检出。在输入框中,您可以输入'-' 来切换到最后一个分支。 | +| `` - `` | Checkout previous branch | | | `` F `` | 强制检出 | 强制检出所选分支。这将在检出所选分支之前放弃工作目录中的所有本地更改。 | | `` d `` | 删除 | 查看本地/远程分支的删除选项 | | `` r `` | 变基 | 将检出的分支变基到所选的分支上。 | diff --git a/docs/keybindings/Keybindings_zh-TW.md b/docs/keybindings/Keybindings_zh-TW.md index 6c4601cba..767862772 100644 --- a/docs/keybindings/Keybindings_zh-TW.md +++ b/docs/keybindings/Keybindings_zh-TW.md @@ -280,6 +280,7 @@ _說明:`` 表示 Ctrl+B、`` 表示 Alt+B,`B`表示 Shift+B | `` O `` | 建立拉取請求選項 | | | `` `` | 複製拉取請求的 URL 到剪貼板 | | | `` c `` | 根據名稱檢出 | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` - `` | Checkout previous branch | | | `` F `` | 強制檢出 | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | 刪除 | View delete options for local/remote branch. | | `` r `` | 將已檢出的分支變基至此分支 | Rebase the checked-out branch onto the selected branch. | diff --git a/schema/config.json b/schema/config.json index c070e4ce0..9f73e8974 100644 --- a/schema/config.json +++ b/schema/config.json @@ -836,6 +836,10 @@ "type": "string", "default": "F" }, + "checkoutPreviousBranch": { + "type": "string", + "default": "-" + }, "rebaseBranch": { "type": "string", "default": "r" From d41668f5657495fd8a86eef9846c1f57687f473b Mon Sep 17 00:00:00 2001 From: kyu08 <49891479+kyu08@users.noreply.github.com> Date: Sun, 13 Jul 2025 00:48:35 +0900 Subject: [PATCH 3/3] Fix `last branch` to `previous branch` in `TranslationSet.CheckoutByNameTooltip` --- docs/keybindings/Keybindings_en.md | 2 +- docs/keybindings/Keybindings_ko.md | 2 +- docs/keybindings/Keybindings_nl.md | 2 +- docs/keybindings/Keybindings_ru.md | 2 +- docs/keybindings/Keybindings_zh-TW.md | 2 +- pkg/i18n/english.go | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/keybindings/Keybindings_en.md b/docs/keybindings/Keybindings_en.md index 5cc2e30c6..d6b2586b6 100644 --- a/docs/keybindings/Keybindings_en.md +++ b/docs/keybindings/Keybindings_en.md @@ -170,7 +170,7 @@ _Legend: `` means ctrl+b, `` means alt+b, `B` means shift+b_ | `` o `` | Create pull request | | | `` O `` | View create pull request options | | | `` `` | Copy pull request URL to clipboard | | -| `` c `` | Checkout by name | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` c `` | Checkout by name | Checkout by name. In the input box you can enter '-' to switch to the previous branch. | | `` - `` | Checkout previous branch | | | `` F `` | Force checkout | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | Delete | View delete options for local/remote branch. | diff --git a/docs/keybindings/Keybindings_ko.md b/docs/keybindings/Keybindings_ko.md index 43556588f..2f91cc31c 100644 --- a/docs/keybindings/Keybindings_ko.md +++ b/docs/keybindings/Keybindings_ko.md @@ -206,7 +206,7 @@ _Legend: `` means ctrl+b, `` means alt+b, `B` means shift+b_ | `` o `` | 풀 리퀘스트 생성 | | | `` O `` | 풀 리퀘스트 생성 옵션 | | | `` `` | 풀 리퀘스트 URL을 클립보드에 복사 | | -| `` c `` | 이름으로 체크아웃 | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` c `` | 이름으로 체크아웃 | Checkout by name. In the input box you can enter '-' to switch to the previous branch. | | `` - `` | Checkout previous branch | | | `` F `` | 강제 체크아웃 | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | 삭제 | View delete options for local/remote branch. | diff --git a/docs/keybindings/Keybindings_nl.md b/docs/keybindings/Keybindings_nl.md index 65ff02a2c..7fbaaffc1 100644 --- a/docs/keybindings/Keybindings_nl.md +++ b/docs/keybindings/Keybindings_nl.md @@ -103,7 +103,7 @@ _Legend: `` means ctrl+b, `` means alt+b, `B` means shift+b_ | `` o `` | Maak een pull-request | | | `` O `` | Bekijk opties voor pull-aanvraag | | | `` `` | Kopieer de URL van het pull-verzoek naar het klembord | | -| `` c `` | Uitchecken bij naam | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` c `` | Uitchecken bij naam | Checkout by name. In the input box you can enter '-' to switch to the previous branch. | | `` - `` | Checkout previous branch | | | `` F `` | Forceer checkout | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | Delete | View delete options for local/remote branch. | diff --git a/docs/keybindings/Keybindings_ru.md b/docs/keybindings/Keybindings_ru.md index befa85d47..44ef15f9c 100644 --- a/docs/keybindings/Keybindings_ru.md +++ b/docs/keybindings/Keybindings_ru.md @@ -204,7 +204,7 @@ _Связки клавиш_ | `` o `` | Создать запрос на принятие изменений | | | `` O `` | Создать параметры запроса принятие изменений | | | `` `` | Скопировать URL запроса на принятие изменений в буфер обмена | | -| `` c `` | Переключить по названию | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` c `` | Переключить по названию | Checkout by name. In the input box you can enter '-' to switch to the previous branch. | | `` - `` | Checkout previous branch | | | `` F `` | Принудительное переключение | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | Delete | View delete options for local/remote branch. | diff --git a/docs/keybindings/Keybindings_zh-TW.md b/docs/keybindings/Keybindings_zh-TW.md index 767862772..4991b7cfa 100644 --- a/docs/keybindings/Keybindings_zh-TW.md +++ b/docs/keybindings/Keybindings_zh-TW.md @@ -279,7 +279,7 @@ _說明:`` 表示 Ctrl+B、`` 表示 Alt+B,`B`表示 Shift+B | `` o `` | 建立拉取請求 | | | `` O `` | 建立拉取請求選項 | | | `` `` | 複製拉取請求的 URL 到剪貼板 | | -| `` c `` | 根據名稱檢出 | Checkout by name. In the input box you can enter '-' to switch to the last branch. | +| `` c `` | 根據名稱檢出 | Checkout by name. In the input box you can enter '-' to switch to the previous branch. | | `` - `` | Checkout previous branch | | | `` F `` | 強制檢出 | Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch. | | `` d `` | 刪除 | View delete options for local/remote branch. | diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 7259ae372..2b307780e 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -1182,7 +1182,7 @@ func EnglishTranslationSet() *TranslationSet { ForceCheckout: "Force checkout", ForceCheckoutTooltip: "Force checkout selected branch. This will discard all local changes in your working directory before checking out the selected branch.", CheckoutByName: "Checkout by name", - CheckoutByNameTooltip: "Checkout by name. In the input box you can enter '-' to switch to the last branch.", + CheckoutByNameTooltip: "Checkout by name. In the input box you can enter '-' to switch to the previous branch.", CheckoutPreviousBranch: "Checkout previous branch", RemoteBranchCheckoutTitle: "Checkout {{.branchName}}", RemoteBranchCheckoutPrompt: "How would you like to check out this branch?",