I'm new to forking and trying to wrap my head around the process. I have an original repo "Main Repo" which I've forked and cloned. There's a branch "branch-A" on Main Repo that I want to make contributions to. So far these are the steps I've run:
git clone <forked-repo>
git remote add upstream <original-repo>
git checkout -b feature-branch
git fetch upstream
git merge upstream/branch-A
*make some codes changes*
*commit changes*
git push origin feature-branch
When I go back to Github and select the branch I just committed, it shows "This branch is x commits ahead of master" but I want it to show how many commits ahead/behind I am off branch-A. What commands do I need to run to do that? Thanks!
You may not be able to get GitHub to show you what you want, but it is easy enough in command-line Git. Just remember that the command line works on your repository, not on any repository over on GitHub, so you'll need to be sure to have your repository synchronized with the GitHub-side ones (via
git fetch
and/orgit push
as needed).Suppose that you'd like to compare the name
origin/feature-branch
(as seen in your Git repository, representing your Git's memory oforigin
'sfeature-branch) to the name
upstream/branch-A(your Git's memory of
upstream's
branch-A`). You can do:The two-dot notation here means: Find commits that are reachable by starting with whichever commit
origin/feature-branch
names, and look at those commits, but stop looking when you get to a commit reachable fromupstream/branch-A
. You can use this construct with many Git commands, includinggit log
:Here, we're using it with
git rev-list
, which is a lot likegit log
—they're kind of sister commands—except thatrev-list
simply lists the commit hash IDs (this is intended for feeding to other Git programs). With the--count
option, instead of listing the hash IDs, it counts them. So this gives a count of commits that are reachable fromorigin/feature-branch
, but not reachable fromupstream/branch-A
, and that's your "ahead of" count.Additional information
There's also a count of commits reachable from
upstream/branch-A
but not reachable fromorigin/feature-branch
. We can get this with:Note that the placement of the two names around the
..
special-character-sequence is swapped. Given what you've done recently, you can typically expect this count to be zero: that is, there are no such commits.You can get both counts at once using
git rev-list --left-right
and the three-dot syntax,upstream/branch-A...origin/feature-branch
for instance, but we'll leave that for later.For (much) more on the concept of reachable commits, see Think Like (a) Git.