Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/allwelldotdev/show-git-branch-bash-sh
Show git branch with color in bash shell prompt
https://github.com/allwelldotdev/show-git-branch-bash-sh
git linux
Last synced: about 1 month ago
JSON representation
Show git branch with color in bash shell prompt
- Host: GitHub
- URL: https://github.com/allwelldotdev/show-git-branch-bash-sh
- Owner: allwelldotdev
- Created: 2024-07-11T11:47:07.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-07-11T12:12:08.000Z (6 months ago)
- Last Synced: 2024-07-11T13:41:52.941Z (6 months ago)
- Topics: git, linux
- Language: Shell
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Show git branch in your Ubuntu bash shell prompt by editing the PS1
**What is PS1, you may ask?** It's certainly not PlayStation 1, if that's what you thought! 😂 PS1 stands for "Prompt String 1". It's an environment variable that defines the primary prompt string in your shell. This is the prompt you see each time you're ready to type a command. It's a crucial part of customizing your shell prompt in Ubuntu (and other Unix-like systems).
A default PS1 looks something like this:
```
\u@\h:\w\$
```
Where:
- \u is the current usernae
- \h is the hostname
- \w is the current working directory
- $ is a $ for regular users, # for root.### What to do...
Now we understand what we're working on, see the file `new_bashrc.sh` for the code.You are to paste the code in your user home directory `~/.bashrc` file. But we will not only paste the code, we'll perform a few tweaks as well.
- Glance through your `~/.bashrc` file and within it find the portion of the code file (`new_bashrc.sh`) that reads "THE 6 LINES BELOW..."
- Once you've found it in your user home directory `~/.bashrc` file then comment it out.
- Paste the uncommented part of the code in `new_bashrc.sh` into your `~/.bashrc` file.
- source your `~/.bashrc` file to apply the changes in the shell. Use command:
```bash
source ~/.bashrc
```
- Now, you should be able to see your git branch name prepended in your shell prompt. Like so.
![show git branch in bash shell prompt](./show-git-branch-bash-sh-prompt.png)Enjoy!
PS. You should know this solution causes some performance considerations. In large repositories, checking the git branch for every prompt can slow down your terminal. Some users opt for more optimized solutions or async prompts to mitigate this. Or they simply leave it as it came with Ubuntu (without seeing their git branch in shell prompt, unless they run `git branch` command in the terminal). Pick which works for you, but know the consequences.
Please raise an issue if you have questions. Thank you!