Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why PS1 is not updated with the git branch name?

Tags:

git

bash

PS1="\\w (`git branch | grep '^*' | cut -b 3-100`)$ "

I set my PS1 as above, but it will not update the branch name in the prompt after I change the branch with git checkout, or change the repository with the cd. How to fix this?

like image 471
exebook Avatar asked Jul 18 '16 05:07

exebook


People also ask

What is a valid branch name in Git?

In Git, the git branch branch-name command is used to create a new branch called branch-name . Branches should be named something that describes the purpose of the branch. Note that branch names can't contain whitespace: new-feature and new_feature are valid branch names, but new feature is not.

How do I add a branch to git terminal?

New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.

What is git branch command for?

The git branch command lets you create, list, rename, and delete branches. It doesn't let you switch between branches or put a forked history back together again. For this reason, git branch is tightly integrated with the git checkout and git merge commands.


1 Answers

The backtick-quoted bit is evaluated when PS1 is set (once), not when the prompt is displayed. Use single quotes to save the expansion for the latter event:

PS1='\w (`git branch | grep "^*" | cut -b 3-100`)$ '
like image 174
Ry- Avatar answered Nov 01 '22 05:11

Ry-