Skip to content

Custom Commands Compendium

David Chocholatý edited this page Aug 12, 2022 · 48 revisions

If you have implemented a custom command(s) that you find useful, please add it here so others can find it too. If a particular command proves popular, we will merge it into the codebase for all to use.

If you want to know how to implement your own custom commands see here

Creating a review in Gerrit

customCommands:
  - key: '<c-p>'
    command: "git push origin HEAD:refs/for/{{.CheckedOutBranch.Name}}"
    context: 'global'
    loadingText: 'pushing'

Pushing to a specific remote repository

This custom command allows picking between already used remote repositories and pushing with force and/or with lease.

  - key: '<c-P>'
    description: "Push to a specific remote repository"
    context: 'global'
    loadingText: 'Pushing ...'
    prompts:
      - type: 'menuFromCommand'
        title: 'Which remote repository to push to?'
        command: bash -c "git remote --verbose | grep '/.* (push)'"
        filter: '(?P<remote>.*)\s+(?P<url>.*) \(push\)'
        valueFormat: '{{ .remote }}'
        labelFormat: '{{ .remote | bold | cyan }} {{ .url }}'
      - type: 'menu'
        title: 'How to push?'
        options:
          - value: 'push'
          - value: 'push --force-with-lease'
          - value: 'push --force'

Pushing to a specific remote branch

customCommands:
  - key: '<c-p>'
    context: 'global'
    loadingText: 'pushing'
    prompts:
      - type: 'input'
        title: 'which branch do you want to push to?'
    command: "git push origin {{index .PromptResponses 0}}"

Creating an annotated tag

  - key : 'N'
    description: 'create annotated tag'
    command: "git tag -a {{index .PromptResponses 0}} -m \"{{index .PromptResponses 1}}\""
    context: 'tags'
    prompts:
      - type: 'input'
        title: 'Annotated tag name:'
      - type: 'input'
        title: 'Annotated tag message:'

Open GitHub Pull Request

customCommands:
  - key: "<c-r>"
    command: "gh pr create --fill --web"
    context: "global"
    loadingText: "Creating pull request on GitHub"

This can be customized to fit your needs. Run gh pr create --help to see what flags are available.

Checkout branch via Github Pull Request id

customCommands:
  - key: "v" # couldn't think of a better keybinding
    prompts:
      - type: 'input'
        title: 'PR id:'
    command: "hub pr checkout {{index .PromptResponses 0}}"
    context: "localBranches"
    loadingText: "checking out PR"

Opening git mergetool

customCommands:
  - key: "M"
    command: "git mergetool {{ .SelectedFile.Name }}"
    context: "files"
    loadingText: "opening git mergetool"
    subprocess: true

Pruning deleted remote branches

customCommands:
  - key: "<c-p>"
    command: "git remote prune {{.SelectedRemote.Name}}"
    context: "remotes"
    loadingText: "Pruning..."
    description: "prune deleted remote branches"

Pruning merged local branches

customCommands:
  - key: "b"
    command: "git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d"
    context: "localBranches"
    loadingText: "Pruning..."
    description: "prune local branches that have been merged to master"

committing via Commitizen (git cz)

customCommands:
  - key: "C"
    command: "git cz"
    context: "files"
    loadingText: "opening commitizen commit tool"
    subprocess: true

Searching the repo's history for a word appearing in a specified subtree

You need to build lazygit from master branch for this to work!

customCommands:
  - key : '<c-a>'
    description: 'Search the whole history (From a ref and down) for an expression in a file'
    command: "git checkout {{index .PromptResponses 3}}"
    context: 'commits'
    prompts:
      - type: 'input'
        title: 'Search word:'
      - type: 'input'
        title: 'File/Subtree:'
      - type: 'input'
        title: 'Ref:'
        initialValue: "{{index .CheckedOutBranch.Name }}"
      - type: 'menuFromCommand'
        title: 'Commits:'
        command: "git log --oneline {{index .PromptResponses 2}} -S'{{index .PromptResponses 0}}' --all -- {{index .PromptResponses 1}}"
        filter: '(?P<commit_id>[0-9a-zA-Z]*) *(?P<commit_msg>.*)'
        valueFormat: '{{ .commit_id }}'
        labelFormat: '{{ .commit_id | green | bold }} - {{ .commit_msg | yellow }}'

Example Usage on lazygit repo:

  • Search word: lazygit
  • Subtree/File: **/commits.go
  • Ref: master Note also that color functions are supported in labelFormat

Fetch a remote branch as a new local branch

customCommands:
  - key: '<c-f>'
    description: 'fetch a remote branch as a new local branch'
    command: "git fetch {{index .SelectedRemote.Name }} {{index .PromptResponses 0}}:{{index .PromptResponses 1}}"
    context: 'remotes'
    prompts:
      - type: 'input'
        title: 'Remote Branch Name'
        initialValue: ''
      - type: 'input'
        title: 'New Local Branch Name'
        initialValue: ''
    loadingText: 'fetching branch'

Commit as non-default author

customCommands:
  - key: '<c-c>'
    description: 'commit as non-default author'
    command: 'git commit -m "{{index .PromptResponses 0}}" --author="{{index .PromptResponses 1}} <{{index .PromptResponses 2}}>"'
    context: 'files'
    prompts:
      - type: 'input'
        title: 'Commit Message'
        initialValue: ''
      - type: 'input'
        title: 'Author Name'
        initialValue: ''
      - type: 'input'
        title: 'Email Address'
        initialValue: ''
    loadingText: 'commiting'

Amend the author of last commit

customCommands:
  - key: '<c-a>'
    description: 'amend the author of last commit'
    command: 'git commit --amend --author="{{index .PromptResponses 0}} <{{index .PromptResponses 1}}>"'
    context: 'commits'
    prompts:
      - type: 'input'
        title: 'Author Name'
        initialValue: ''
      - type: 'input'
        title: 'Email Address'
    subprocess: yes

        initialValue: ''
    subprocess: true

Blame via tig

customCommands:
  - key: b
    command: tig blame -- {{.SelectedFile.Name}}
    context: files
    description: blame file at tree
    subprocess: yes
  - key: b
    command: tig blame {{.SelectedSubCommit.Sha}} -- {{.SelectedCommitFile.Name}}
    context: commitFiles
    description: blame file at revision
    subprocess: yes
  - key: B
    command: tig blame -- {{.SelectedCommitFile.Name}}
    context: commitFiles
    description: blame file at tree
    subprocess: yes

Browse files at revision via tig

customCommands:
  - key: t
    command: tig show {{.SelectedSubCommit.Sha}}
    context: subCommits
    description: tig commit (`t` again to browse files at revision)
    subprocess: yes
  - key: t
    command: tig show {{.SelectedLocalBranch.Name}}
    context: localBranches
    description: tig branch (`t` again to browse files at revision)
    subprocess: yes
  - key: t
    command: tig show {{.SelectedRemoteBranch.RemoteName}}/{{.SelectedRemoteBranch.Name}}
    context: remoteBranches
    description: tig branch (`t` again to browse files at revision)
    subprocess: yes

File history via tig

customCommands:
  - key: t
    command: tig {{.SelectedSubCommit.Sha}} -- {{.SelectedCommitFile.Name}}
    context: commitFiles
    description: tig file (history of commits affecting file)
    subprocess: yes
  - key: t
    command: tig -- {{.SelectedFile.Name}}
    context: files
    description: tig file (history of commits affecting file)
    subprocess: yes

Extract diff into index

This requires some explanation: say you've got a PR that has merged in master a few times and so it's a bit of a mess to follow the changes, but there's actually not that many lines changed in total. In that case, you probably just want to take the actual changes and put them in a single commit on top of the head of the master branch.

customCommands:
  - key: 'D'
    command: git diff {{.SelectedLocalBranch.Name}} > /tmp/lazygit.patch && git reset --hard {{.SelectedLocalBranch.Name}} && git apply /tmp/lazygit.patch 
    context: localBranches
    description: Extract diff into index

Add empty commit

  - key: 'E'
    description: 'Add empty commit'
    context: 'commits'
    command: 'git commit --allow-empty -m "empty commit"'
    loadingText: 'Committing empty commit...'
Clone this wiki locally