DEV Community

Cover image for Configuring Linux terminal to show current branch in versioned directories
Ernane Ferreira
Ernane Ferreira

Posted on

Configuring Linux terminal to show current branch in versioned directories

Make your life easier as a developer and know the current branch in your workspace without complications. And the best? Without installing anything additional!

One of the gifts of a good developer is knowing exactly where he's pushing his changes. Currently, the best way to do this, especially using code versioning with Git, is to validate the current working directory branch. We have a few ways to do this:

  • Use git's own command to show the current branch:
$ git status

# On branch master
# Your branch is up to date with 'origin/master'.

# nothing to commit, working tree clea
Enter fullscreen mode Exit fullscreen mode

Or yet:

$ git branch

# * master
Enter fullscreen mode Exit fullscreen mode
  • We can install some other third-party terminal (such as Oh My ZSH), but that's a topic for another post! 😎
  • Another option is to simply manipulate your native terminal to show your current branch whenever you have a git-versioned directory. This alternative is what we will see in this post!

Changing settings to display the current branch

Normally, the terminal settings do not show the branch of the current directory if it is versioned by git, and by default only the user, host (machine name) and path (current path) are shown. However, it has some settings that can be manipulated from a variable in its file named PS1. To do so, simply open the .bashrc or .bash_profile file in your preferred text editor and add the following command at the end:

export PS1='\u@\h[\033[01;34m] \w[\033[0;32m]$(__git_ps1 " (%s)")[\033[01;34m]$[\033[00m] '
Enter fullscreen mode Exit fullscreen mode

To apply the settings, it is necessary to reload the file. We can do this by restarting the system, logging out or simply reloading the configuration file:

source ~/.bashrc # or source ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

Ready! 🎉 We already have our current branch showing in directories that are versioned by git.

Additional settings

Why not go further? Below I will leave some possible additional color settings, for example, that can be performed to make the present information as relevant as possible to your use case.

Values:

  • \u: User currently logged on the machine;
  • \h: Name of the machine (host), previously registered;
  • \H: Full machine name, previously registered;
  • \w: Full current working directory;
  • \W: Minified current working directory, base name only (last segment);
  • $(__git_ps1 "%s"): Current branch if it's in a git-versioned repository, otherwise nothing will be displayed.

Colors

  • Blue: [\033[0;34m];
  • Red: [\033[0;31m];
  • Fluorescent Red: [\033[1;31m];
  • Green: [\033[0;32m];
  • Fluorescent Green: [\033[1;32m];
  • Strong White: [\033[1;37m];
  • Gray: [\033[0;37m];
  • Default: [\033[0m].

Ready! This is the range of simple and powerful configurations available for use that will make it even easier for you to self-localize in the development process and, in addition, allow customization in the way you want and find valid. Mine looks like this at the time I write this post:

👾 ~/path/path(master)$
Enter fullscreen mode Exit fullscreen mode

Yes, with emoji! 😬

What has been shown here is just a little of what is possible to be done. Therefore, as usual, I will leave some links that I used as a reference to write and that you can use to go further on the subject.

Good reading!

I hope you enjoyed this post and that it helped you find what you were looking for! 💙

Links:

Top comments (2)

Collapse
 
pythonisnotasnake profile image
PythonIsNotASnake

At work another software engineer show me a project on github to show git informations on bash. bash-git-prompt is easy to use and worked for me on WSL too.
Anyway I liked to read your post. It is very helpful. Keep on 🙂

Collapse
 
ernanej profile image
Ernane Ferreira

Very cool! Thanks for sharing.