简体   繁体   中英

How to always show git branch in Windows 11 Terminal?

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.

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 .

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
},

Your self-answer shows that I misunderstood your question. Apparently what you really want to know is how to run Git Bash in Windows Terminal.

That's even easier than your answer -- Just install the newest release of Git Bash. It should automatically create a profile in Windows Terminal using a recent WT feature called a JSON fragment extension .

After reinstalling Git Bash using the latest version, just restart Windows Terminal and you should have a new profile for Git Bash.

Of course, you should delete (or rename) your existing manually created profile first to avoid conflicts.

I was experiencing the same issue. Clean install of windows 11. Installed Git using chocolatey. Windows terminal had git-bash as an option when I first opened it, but it would open in a new window.

For me after reading this aa few other post, I just fixed it by updating my "settings.json" file. It was originally pointing to git-bash.exe

"commandline" : "C:/Program Files/Git/Git-bash.exe",

To fix it, I changed it to point to bash.exe instead

"commandline" : "C:/Program Files/Git/bin/bash.exe",

如果有人正在寻找不需要 Git Bash 的 PowerShell 解决方案,请查看此答案

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM