DEV Community

Cover image for Determine the Git Branch You're On
Mohammad-Ali A'RÂBI
Mohammad-Ali A'RÂBI

Posted on • Originally published at Medium

1

Determine the Git Branch You're On

Branching is one of the most used features of git. People usually change branches all the time and need to determine which branch are they on, constantly.

Use Branch Command

The first git subcommand that deals with branches is the branch command. Just by writing this command down, a list of all your local branches and the branch you are on will be shown. Enter:

git branch
Enter fullscreen mode Exit fullscreen mode

And the output will be something like this:

  aerabi/add-readme
  aerabi/add-github-actions
* master
  the-hotfix-branch
Enter fullscreen mode Exit fullscreen mode

Your current branch is highlighted with an asterisk and a different color.

Use Status Command

The status command is a widely used powerful command that shows your current branch among other things. Enter:

git status
Enter fullscreen mode Exit fullscreen mode

And the output will be something like this:

On branch master 
Your branch is up to date with 'origin/master'. 

Changes not staged for commit: 
  (use "git add <file>..." to update what will be committed) 
  (use "git restore <file>..." to discard changes in working directory) 
        modified:   docker-compose.dev.yml 

Untracked files: 
  (use "git add <file>..." to include in what will be committed) 
        .idea/ 
        Dockerfile 
        docker-compose.override.yml.bak 

no changes added to commit (use "git add" and/or "git commit -a")
Enter fullscreen mode Exit fullscreen mode

The first line shows which branch are you on. The second shows your upstream branch if there are any set.

Use Your Bash Configurations

This part works for Bash on Debian-based Linux distributions like Ubuntu.

I am using bash on Ubuntu. When I’m on a directory, my bash will show me something like this:

mohammad@pc:~/Dev/my-git-project$ 
Enter fullscreen mode Exit fullscreen mode

So, all the time, I know which user account I’m using, on which host, and where on that host. It would be also very useful if your git branch would show up additionally. For it to happen, open your .bashrc file using your editor of choice:

vim ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

And add the following line to the end of the file:

PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\e[91m\]\$(__git_ps1 ' [%s]')\[\e[00m\]\$ "
Enter fullscreen mode Exit fullscreen mode

Save and exit. Now, open a new terminal (or type source ~/.bashrc) and the git branch will show up whenever you’re in a git repository:

mohammad@pc:~/Dev/my-git-project [master]$
Enter fullscreen mode Exit fullscreen mode

Conclusion

Try to configure your shell to show the git branch, it saves you a lot of time.

This article was originally published on Medium. I write weekly on git and monthly on Docker:

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay