Home > OS >  How to always show git branch in Windows 11 Terminal?
How to always show git branch in Windows 11 Terminal?

Time:10-19

I was looking for an option to show the git branch in Windows 11 terminal when navigating to a local git directory. And if possible auto-complete support for git commands.

So far I was using GitBash for this, is there a way we can do this in the default windows terminal in the new windows 11 OS.

CodePudding user response:

There are quite a few ways to do this, and they all really depend on your shell, as both the prompt and auto-completion are shell features.

I'll provide three possibilities:

  • My preferred setup for this is to install Windows Subsystem for Linux (WSL) with a distribution. Ubuntu is the default WSL distribution, which is fine, but I tend to prefer openSUSE Tumbleweed since it has more recent software packages. And I install the fish shell as my default shell.

    The default prompt in fish includes support for showing the branch when you are in a local repo directory.

    It also includes out-of-the-box support for git completions (and many other commands -- fish will actually parse man pages to build completions even for commands it doesn't know about).

    And, of course, if you'd like a "fancier" prompt, fish has plenty of options available as well. Both of the ones mentioned below (Starfish and oh-my-posh) work in fish, or you can use a "pure fish" prompt such as Tide, which seems fairly popular among the fish users on Reddit.

    Personally, I've tried a few different options, but I keep going back to the default, out-of-the-box prompt for the most part. It's just easier to work with that rather than spend too much time configuring one.

  • If you prefer PowerShell, you can also install a prompt such as oh-my-posh (quite popular) or the Starship prompt (newer, written in Rust).

    I've never used git completions on PowerShell, but a quick search shows post-git was an answer for that a decade prior, and it still seems to be in active development.

  • And if you'd prefer to keep using Windows Git Bash, there's no reason that you can't use Windows Terminal for it. In fact, Git Bash now automatically installs a JSON fragment for Windows Terminal so that you should see a profile for it directly on the menu.

    And I'm sure you can configure it as well for what you are asking. Both Starship and oh-my-posh support Bash, of course, so you can configure your prompt there.

    And this SO question covers enabling git completions in Git Bash. The first/highest-rated answer, unfortunately seems to ignore the question (Git Bash), but the second and third seem on-point. I would read this answer in particular, with some additional detail in this one.

CodePudding user response:

So this documentation helped me to set up a powerful terminal in Windows 11 Set up Powerline in Windows Terminal

  • Prerequisites

Install a Powerline font, fonts can be installed from the Cascadia Code GitHub releases page.

  • Set up Powerline in PowerShell

Using PowerShell, install Posh-Git and Oh-My-Posh:

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser -RequiredVersion 2.0.412
  • Customize your PowerShell prompt

Open your PowerShell profile with notepad $PROFILE or the text editor of your choice. This is not your Windows Terminal profile. Your PowerShell profile is a script that runs every time PowerShell starts. In your PowerShell profile, add the following to the end of the file:

Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme paradox
  • Set Cascadia Code PL as your font

o set the Cascadia Code PL font for use with PowerLine (after downloading the Cascadia Code font, unzipping, and installing on your system), you will need to open your profile settings in your settings.json file by selecting Settings (Ctrl ,) from your Windows Terminal dropdown menu while holding down Shift. This can also be done using the settings UI if you have it enabled.

{
    // Make changes here to the powershell.exe profile.
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "name": "Windows PowerShell",
    "commandline": "powershell.exe",
    "fontFace": "Cascadia Code PL",
    "hidden": false
},
  • Related