Determine the Git Branch You're On

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 sub-command 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        

And the output will be something like this:

  aerabi/add-readme
  aerabi/add-github-actions
* master
  the-hotfix-branch

        

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        

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")        

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$         

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        

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\]\$ "        

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]$        

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:

要查看或添加评论,请登录

Mohammad-Ali A'R?BI的更多文章

  • 7 Noob Git Tips

    7 Noob Git Tips

    We will start 2025 one month into the year with seven basic git tips. This issue is based on a Twitter series that I…

  • 7 Basic Git Commands

    7 Basic Git Commands

    In this Git Weekly issue, we'll cover what basic commands are used when committing directly to the main branch. As a…

  • Git Submodule Update

    Git Submodule Update

    Halloween is here and we're back with Git Weekly #8, another piece on the spine-chilling git submodules. ?? Submodules…

  • Git Submodules

    Git Submodules

    Welcome to Git Weekly #7. This week, we will discuss having git repos within git repos.

  • Git Get One File From Another Repo

    Git Get One File From Another Repo

    Welcome to Issue #6 of Git Weekly! This week we're going to talk file and moving them around. Let's assume you have a…

  • How to Squash Git Commits Manually

    How to Squash Git Commits Manually

    Welcome to Git Weekly! Each week, we explore advanced git features to help you fine-tune your workflow. Today’s topic…

  • Git Merge vs Rebase and Where to Use Them

    Git Merge vs Rebase and Where to Use Them

    There are two workflows for merging a feature branch into the master branch, namely 'rebase and fast-forward' and…

  • Git Merge vs Rebase: The Three Types of Merge

    Git Merge vs Rebase: The Three Types of Merge

    Merging is adding the work done in one branch to another. There are three ways one could merge one branch into another:…

    2 条评论
  • Change a Commit's Author on Git

    Change a Commit's Author on Git

    I usually commit to my work and to my private git projects, and I use two different email addresses for them. But it…

    4 条评论

社区洞察

其他会员也浏览了