DEV Community

Cover image for How to see branch name on terminal in ubuntu 22.04
Limon Safayet
Limon Safayet

Posted on

How to see branch name on terminal in ubuntu 22.04

To see the current branch name in the terminal when you are working with Git repositories in Ubuntu 22.04, you can use a combination of commands and customizing your shell prompt. Here's a step-by-step guide on how to do this:

  1. Check if Git is Installed: Make sure you have Git installed on your Ubuntu system. You can do this by running the following command:
   git --version
Enter fullscreen mode Exit fullscreen mode

If Git is not installed, you can install it using apt:

   sudo apt update
   sudo apt install git
Enter fullscreen mode Exit fullscreen mode
  1. Install a Git Prompt Tool (Optional): There are various tools and scripts available that can help you display Git branch information in your terminal prompt. One popular option is "git-prompt." You can install it using apt:
   sudo apt install git-extras
Enter fullscreen mode Exit fullscreen mode
  1. Configure Your Shell Prompt: You can customize your shell prompt to include the current Git branch. To do this, you need to edit your shell configuration file (e.g., ~/.bashrc for Bash or ~/.zshrc for Zsh) and add the following lines:

For Bash (default Ubuntu shell):

   # Add the following to your ~/.bashrc file
   parse_git_branch() {
       git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
   }

   PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$(parse_git_branch)\$ "
Enter fullscreen mode Exit fullscreen mode

For Zsh:

   # Add the following to your ~/.zshrc file
   parse_git_branch() {
       git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
   }

   PROMPT='%n@%m:%~$(parse_git_branch) %# '
Enter fullscreen mode Exit fullscreen mode
  1. Reload the Shell Configuration: After making changes to your shell configuration file, you need to either open a new terminal or run the following command to apply the changes to your current terminal session:
   source ~/.bashrc  # For Bash
   source ~/.zshrc   # For Zsh
Enter fullscreen mode Exit fullscreen mode

Now, when you navigate to a Git repository in your terminal, the current branch name should be displayed in your prompt. If you're using a different shell or prefer a different prompt style, you can adjust the customization accordingly.

Top comments (0)