A similar question has already been asked here, but I think it got misunderstood (or maybe I misunderstood the question). So let me ask it clearly here. When I do git status
, I get (among other stuff) a line telling me whether or not I am behind of (or forked from) the remote branch I'm tracking. This is great. However, I have more than one remote. I have an 'official' remote (origin), which is shared (and I push there only when stuff is 'ready'), and a 'personal' remote (work), which I use to push stuff from home/work, so that I can keep work when I switch computer. Now, when I do git status
, the local branch is compared to origin/branch_name
(or whatever remote branch I'm tracking). However, I'd like to compare it also to other remotes/branches, but without pulling, simply comparing. Ideally, I'd like to do something like this:
git fetch work
git status work/branch_name
and get a status comparison of my local branch with work/branch_name.
Notice that I do not want to do a diff, that is, I am not interested in what files/paths are different. I just want to see if/when the two branches forked or if one is ahead of the other.
Also notice that I can get this information graphically with gitk --all
, looking where the two branches stay in the tree, but I would like to do this from command line. Also, in case of multiple branches, the tree displayed with gitk --all
can be a mess.
Thanks
Unfortunately, git status
always uses the currently configured upstream, i.e., looks at your current branch (let's call this $branch
) and then checks the configuration values for branch.$branch.remote
and branch.$branch.merge
.
Aside from modifying git status
to take a --pretend-upstream
flag or similar, that leaves you with only two options: reconfigure the upstream (perhaps temporarily), or simulate git status
. Since you only want a count of commits, it turns out that's relatively easy: we just need to know the name of the desired upstream.
To make this fully general you might invent something like branch.$branch.extstatus
or whatever, and loop through git config --get-all
output, but to just hardwire one particular work/branch_name
you can just do:
git rev-list --count work/branch_name..HEAD
and:
git rev-list --count HEAD..work/branch_name
The ..
notation selects commits that are on1 the right-hand-side branch,2 excluding any commits that are on the left-hand side branch, and --count
makes git rev-list
print just the count of commits in the selection; so the first command gets you "commits you have that they don't" (your "ahead" count) and the second gets you "commits they have that you don't" (your "behind" count).
Combine the two with the shell:
echo "vs work/branch_name: ahead $(git rev-list ...), behind $(git rev-list ...)"
(fill in the ...
parts, which I left out to make the line fit). Set that up as a git or shell alias and you're good.
1More precisely, "reachable from": i.e., the commit itself or any of its parents, grandparents, and so on.
2More precisely, anything that identifies a commit, or can be used to locate a commit (e.g., an annotated tag).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With