Always Know Which Git Branch You’re In With This Linux Trick


Key Takeaways

  • Working with branches in Git is quick and easy, and we’re encouraged to do it frequently as good working practice.
  • Because it is such a trivial process, a change of branch doesn’t always stick in your mind.
  • Modify your command prompt to automatically show your current Git branch, and you’ll always know the branch you’re in.



Using branches in Git is so painless, we do it a lot. So much that keeping track of your current branch isn’t easy. With this trick, you’ll never forget your current branch.


Keeping Track of Git Branches

Branches are all-important in version control. They let you work on new features, or changes to existing code, without affecting the main source code branch.

Working with branches in Git is a breeze compared to other version control software. Creating new branches and changing branches are very lightweight processes, so they happen in the blink of an eye. And because it’s so fast and easy, developers tend to do this a lot.

It’s almost too easy. Because Git makes changing branches a trivial matter, you can do it almost as unconsciously as you might change directories. But being aware of which branch you’re in, is very important.


This trick helps by showing your current Git branch in your Bash command prompt. And the neat part is, if your current directory doesn’t contain a Git repository, you’ll see your regular command prompt.

Getting Your Git Branch as a String

For this to work, we need to be able to obtain the current Git branch as a string of text that we insert into the command prompt. Let’s issue the standard Git command to list the branches in the repository.

git branch 
Using the git branch command to list the branches in the current repository, with the current branch flagged with an asterisk.

This lists all the branches in the repository. The current branch, saved-game, is flagged with an asterisk. Conveniently, it’s the only line with an asterisk in it. That means we can pipe the output from the git command into sed, and use the sed command’s stream manipulation features to remove lines without an asterisk. For the single remaining line, we can use sed to remove the asterisk and space.


git branch | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' 
Piping the output of the git branch command into sed to isolate the current branch name.

That gives us the name of the current branch. There are simpler ways to do this, but for completeness’ sake, here’s how this command works.

We issue the standard git branch command.

git branch 

The output is piped into sed. There are two sed expressions. The first expression deletes lines that don’t contain an asterisk.

-e '/^[^*]/d' 

The second expression removes the leading asterisk and space, from the one remaining line.

-e 's/* \(.*\)/\1/' 

It works, but not everyone’s familiar or comfortable with sed. When you look at this in six months, is it likely to make any sense? We can leverage git and Bash to engineer a simpler method.


The Bash && operator lets us chain commands together so that they’re executed one after the other. For a command in the chain to execute, the previous command must complete successfully. As we’ll see, we can use this to advantage.

Another thing working in our favor is, since version 2.2, the git branch command has had an option called –show-current. This displays the name of the current branch on its own.

git branch  
Using the --show-current option with the git branch command to obtain the name fo the current branch.

That’s an easier way to get the unadorned name of the current Git branch. The only issue is, it generates an error message if the current directory doesn’t contain a Git repository. So we need to make sure the command doesn’t get executed if we’re not in a Git repository.


There’s a git command called git status. This reports on the state of the Git repository. If the command fails, as it will if there is no repository to report on, its exit value is 128.

git status
echo $?
Running the git status command outside of a repository, to see its failure exit code value.

In a directory with a repository, its exit value is zero.

git status
echo $?
Running the git status command inside a repository, to see its success exit code value.

We can chain this command and our git branch command, so the git branch command only executes if the git status command returns zero. The other thing we’ll do is redirect all screen output from the git status command to /dev/null, a pseudo file that silently consumes everything sent to it.


git status &> /dev/null && echo " $(git branch --show-current)"
cd taf
git status &> /dev/null && echo " $(git branch --show-current)"
Running two chained Git commands outside and insidea directory with a repository, to check that we only get screen output on success.

We’ve suppressed all output from the git status command, which is precisely what we need. Inside a directory that does not contain a repository, there’s no screen output. Inside a directory containing a repository, we see the name of the current branch.

We could just let the git branch command write the name of the current branch to the terminal window as is, but by wrapping it in an echo command, we can include a leading space. The space will separate the branch name from the directory name in the command prompt, making it easier to see at a glance.


Adding the String to Your Bash Prompt

We’ve acquired the name of the current Git branch, and we’ve got a way to output that to the screen, formatted with a leading space. Now we’ll incorporate that into the command prompt.

We’ll create a function to hold the commands we’ve created so far, and call that function from within the definition of the command prompt. Whenever the command prompt is displayed, our function will be called.

In Bash, the definition of the command prompt is usually (but not always) in the .bashrc file. This is a hidden system file, located in your home directory.

Use your favorite editor to edit the .bashrc file.

The .bashrc file in an editor, with the command prompt definition highlighted.

Look for the line that defines your command prompt. It usually starts with:

PS1= 

On this Ubuntu computer, the line we want is:


PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' 

There’s another ‘PS1=’ line below this one, but it is much shorter. If you read what’s happening in the if-then block, you’ll see one prompt is for color displays and one’s for monochrome. If you’re using Git, you’re a coder. You’ll soon be able to figure out which prompt is the one you need to work with.

We’ll add our function above the if-then block. I’m calling it get_git_branch.

The get_git_branch function added to the .bashrc file.

It contains the same commands that we used on the command line. We now need to determine where to call the function from, in the command prompt definition.

This is the end of the prompt definition.


\w\[\033[00m\]\$ ' 

This is how it’s made up.

\w 

This is a token that gets replaced with the working directory when the command prompt is displayed.

\[\033[00m\] 

This is an ASCII color code. It sets the text color back to white, so that the dollar sign ‘$’ in the prompt is displayed in white text.

\$ ' 

This displays the dollar sign, a space, then closes the command prompt definition with a single quote mark.

We’ll call our function immediately after the ‘\w’ token. We’ll also insert a color code to display the Git current branch in yellow, to make it stand out. This is the text we’re going to insert.

\[\033[01;33m\]$(get_git_branch) 

Note there are no spaces in our inserted text. This is what the complete command prompt looks like with our additions.

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;33m\]$(get_git_branch)\[\033[00m\]\$ ' 
The ASCII color code and function call inserted into the command prompt definition.


Save your changes and close the editor. Close your terminal windows, and open a new terminal window. You’ll see a normal command prompt. Navigate to a directory containing a Git repository.

pwd
cd taf
Checking the command prompt outside and inside a Git repository.

The name of the current Git branch is displayed in yellow. If you check out a different branch, your command prompt immediately changes.

git checkout timed-events 
Checking out a Git branch to verify that the command prompt reflects the new current branch.

It’s Not Just for Git

You could use this method to display any sort of information you can gather with command line tools.


For example, if you create this function and call it from your prompt instead of calling get_git_branch, you’ll see the time in your prompt.

get_time() {
  echo " $(date +%r)"
}
The time incorporated into a command prompt.



Source link

Previous articleBrazilian bank suspends native crypto token trading following 97% price drop