I want to change the tracking/comparison of the upstream branch

133 views Asked by At

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!

1

There are 1 answers

0
torek On

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/or git 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 of origin's feature-branch) to the name upstream/branch-A(your Git's memory ofupstream's branch-A`). You can do:

git rev-list --count upstream/branch-A..origin/feature-branch

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 from upstream/branch-A. You can use this construct with many Git commands, including git log:

git log upstream/branch-A..origin/feature-branch

Here, we're using it with git rev-list, which is a lot like git log—they're kind of sister commands—except that rev-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 from origin/feature-branch, but not reachable from upstream/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 from origin/feature-branch. We can get this with:

git rev-list --count origin/feature-branch..upstream/branch-A

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.