7

To get to know how far ahead and/or behind a branch is compared to its remote, there's the git status command.

But git status only shows the status of the current branch.

Looks like to get to know the status of the other branches in the same directory, I need to switch to each branch and run git status again.

However, switching branches is not always possible, as the current branch may not be clean.

Is there any way to get to know the status of the other branches without switching to the other branches?

phd
  • 82,685
  • 13
  • 120
  • 165
lyle
  • 757
  • 1
  • 7
  • 18

3 Answers3

6

To directly answer the question that you posed in the title: you can't run git status in other branches. git status shows you two key pieces of information: how you have changed the index (how you have staged changes) that are different from the most recent commit on your branch, and how you have changed the working directory and made changes that you haven't staged.

In other words, status shows you a combined diff of the HEAD to the index with a diff of the index to the working directory. So it only makes sense to get the equivalent of git status on the current branch, since it takes the index and working directory into account.

With that caveat out of the way, it sounds like you're only interested in ahead/behind count, and you want the ahead/behind count for some other branch compared to its upstream.

Even though ahead/behind count is one of the things git status shows as somewhat ancillary information, it is the easiest way to find the ahead/behind count for the current branch, as you noted.

To show ahead/behind for all branches, you can use the helpful script in this answer.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
2

You should be able to do the below to temporarily set the remote tracking branch

git branch -u <remote>/<branch>

And then git status will display what you need.

Edit: You might want something like this instead (see the script in the first answer) Show git ahead and behind info for all branches, including remotes

bhavicp
  • 336
  • 1
  • 4
  • 15
-3
git status --branch yourLocalBranch

For detailed explanation: https://git-scm.com/docs/git-status

Community
  • 1
  • 1
Carlos UCR
  • 299
  • 3
  • 6
  • The `--branch` option does not do that, it only shows the current branch and tracking info when using `--short` – kaios Sep 14 '22 at 13:21