To make the repository name visible in your terminal prompt, you typically need to customize the command prompt (PS1) to include information about the current directory's Git repository status. The PS1 variable defines how your prompt looks.
Here's a basic guide on how to do this on Unix-like systems (i.e., Linux, macOS):
-
Open your shell configuration file: This could be ~/.bashrc, ~/.bash_profile, ~/.zshrc, or another profile file depending on your shell and system configuration. For example, if you're using Bash, you would typically use ~/.bashrc.
nano ~/.bashrc # or use any other text editor you prefer
-
Update the PS1 variable: You will need to append code to PS1 that will check for a Git repository and, if it's found, add the repository name to your prompt. Most of the time, Git provides a convenient script called git-prompt.sh that does this for you.
Add the following lines to your configuration file (for bash or compatible shell):
source /etc/bash_completion.d/git-prompt # Path to git-prompt.sh may vary
PROMPT_COMMAND='__git_ps1 "\u@\h:\w" " \\\$ "'
# Uncomment the following line to also add colors.
# GIT_PS1_SHOWCOLORHINTS=true
Here, \u stands for the username, \h for the hostname, and \w for the current working directory. The __git_ps1 function will be invoked just before displaying the prompt, and if you're in a Git repo, it will append the branch name to your prompt.
Note that the source path to git-prompt.sh might be different on your system, so you might have to locate it using find or by searching in the common init files that come with Git.
-
Reload your profile to see changes: Source your profile file or restart your terminal.
source ~/.bashrc # Or the appropriate file you've edited
Or just close and re-open the terminal.
-
(Optional) Use a shell theme or framework: If you want a more advanced setup or would like to include extra information in your prompt, consider using a shell theme or framework like oh-my-zsh for zsh shell users or bash-it for bash. These frameworks come with themes that already include Git repository information in the prompt.
For example, if you're using oh-my-zsh, you can simply choose a theme that displays Git information, such as agnoster or robbyrussell.
Certain environments or setups may require additional steps or different methods. For example, if you're using the Fish shell or PowerShell, the steps will be different. You can always search for specific instructions for your shell environment.