0

In short, I want to know the latest commit ID that can also be found on origin. It is possible for me to make many branches, creating a tree from a particular commit ID, but all branches derive from that one commit ID which is also found on origin.

git merge-base asks for two branches. Getting the current branch name is no problem (HEAD), but I do not know the how to get the name of the remote branch. Not just any remote branch, but the one that current branch has derived from most recently. Is this possible, or do I need to go for another approach? Any hint on what I can do?

Forethinker
  • 3,548
  • 4
  • 27
  • 48
  • (Technically `git merge-base` needs two *commits* rather than two branches, but when you need a commit-ID you can name a branch or tag and git will find the commit from there.) Generally the "remote branch" you want is the "upstream" branch that the current branch is "tracking", which can be named with `@{upstream}` (or `@{u}` for short). See http://stackoverflow.com/q/4950725/1256452 for more. – torek Nov 06 '13 at 03:34

1 Answers1

2

If you know which remote branch you are looking for you can just say something like:

git merge-base master origin/master

If you just have an arbitrary local commit and want to know which branch you branched from you can give git merge-base multiple remote branches:

git merge-base HEAD origin/master origin/feature-xy origin/something

If you don't even have a vague idea what remote branch might be the correct one, you have to go on your own. Either generate a list of all remote branches and pass them to git merge-base like above. Or use git rev-list to walk over your local history and use git branch -r --contains to find out if there is some remote branch containing the given commit.


Another way is using:

git log --boundary --decorate --graph HEAD --not --remotes

This will list all commits not in any remote branch and the first commit of the remote (boundary). Use git branch -r --contains $SHA1 on the boundary commit to get an idea which remote branch is involved.


Final solution of Forethinker:

If you want just the SHA-1 of the first commit of the remote:

git rev-list --boundary HEAD --not --remotes | tail -1 | grep -o -E -e "[0-9a-f]{40}"
michas
  • 25,361
  • 15
  • 76
  • 121
  • I do not want to keep track of the remote branch myself. I was hoping that git does this already in one way or another and I could figure out what how to show it. – Forethinker Nov 06 '13 at 03:58
  • Maybe you are talking about tracking upstream branches? You can use `git branch -vv` to show what upstream branch is configured for each local branch. – michas Nov 06 '13 at 04:02
  • Thanks michas. "git log ... " without --graph is pretty close to what I am looking for. I just want the last SHA to print. Is this possible? In the worst case I could use regex to extract it out, but I was just curious. – Forethinker Nov 06 '13 at 04:03
  • What do you mean by *second* SHA? `git log` will potentially list many commits, each having its own SHA. Are you sure you want the second one? If you are only interested in the SHAs use `git rev-list` instead. – michas Nov 06 '13 at 04:07
  • No sorry, I meant the last one. – Forethinker Nov 06 '13 at 04:07
  • `git rev-list ...|tail -1` – michas Nov 06 '13 at 04:11
  • Minor simplification: `--boundary` only adds a single `-` so you can strip this off with `sed`, which you can also use to select the final rev: `git rev-list --boundary HEAD --not --remotes | sed -n '$s/-//p'`. Be aware that, however expressed in code, this will produce somewhat odd results (e.g., nothing at all) if `HEAD` is also unusual (e.g., if you check out a rev earlier than the one shared with `origin`). – torek Nov 06 '13 at 11:08