When you switch between Git branches, put the branch name in your prompt instead of keeping it in your head. Git provides a helper for this: __git_ps1. It comes from a separate script, though; installing Git does not guarantee that your shell has loaded it.
This recipe is for Bash, with Git installed and available on PATH. If a prompt theme already handles Git status, configure that theme instead: assigning PS1 below replaces your current prompt.
Load Git’s prompt helper
Get git-prompt.sh from Git’s official repository. Select the release tag matching your installed Git version (git --version), then save the raw file as ~/.git-prompt.sh. Read it before loading it; this is shell code that will run in your session. If your Git package already includes the file, you can copy that installed version instead. Its location depends on the package.
Add these lines to ~/.bashrc, after any existing prompt configuration:
. "$HOME/.git-prompt.sh"
PS1='\u@\h:\w$(__git_ps1 " (%s)")\$ 'The first line defines __git_ps1; the second calls it whenever Bash displays a prompt. Keep the single quotes around the PS1 value so that the branch is evaluated at each prompt, rather than once when you set the variable. %s is the placeholder for Git’s status text.
Apply the change to your current Bash session:
. "$HOME/.bashrc"For future sessions, Bash reads ~/.bashrc automatically in an interactive non-login shell. A login shell reads different files. If yours uses ~/.bash_profile, make sure it loads ~/.bashrc, as described in the Bash startup-file documentation:
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fiCheck the result
Inside a repository on main, the prompt will look something like this:
alex@laptop:~/project (main)$Switch branches and the next prompt should show the new name. Outside a repository, the Git portion disappears. With a detached HEAD, there is no current branch: the helper shows a commit description, such as an exact tag or an abbreviated commit ID, instead. During a merge or rebase, it can also show the operation in progress.
If you see __git_ps1: command not found, check that ~/.git-prompt.sh exists and that the loading line ran before the PS1 assignment. If the branch stays frozen after switching, check the quoting and whether a later theme or prompt hook overwrites PS1.






