Find the starting point of a local branch
Hi, list
How can I find out what's the staring reference point (a commit number or tag name) of a locally created branch? I can use gitk to find out it but this method is slow, I think there might be a command line to do it quickly.
Thanks in advance.
Signature · 2 lines
woody I can't go back to yesterday - because I was a different person then.
In message <20121224035825.GA17203@zuhnb712>, Woody Wu writes:
How can I find out what's the staring reference point (a commit number or tag name) of a locally created branch? I can use gitk to find out it but this method is slow, I think there might be a command line to do it quickly.
The answer is more complex than you probably suspected.
Technically, git log --oneline mybranch | tail -n 1 will tell you the starting point of any branch. But...I'm sure that isn't what you want to know.
You want to know "what commit was I at when I typed git branch mybranch"? The problem is git doesn't record this information and doesn't have the slightest clue.
But, you say, I can use gitk and see it. See? Right there. That isn't (necessarily) the "starting point" of the branch, it is the place where your branch diverged from some other branch. Git is actually quite able to tell you when the last time your branch diverged from some other branch. git merge-base mybranch master will tell you this, and is probably the answer you were looking for. Note that this is the *last* divergence. If your branch diverged and merged previously that will not be reported. Even worse, if you did a fast-forward merge (I recommend against them in general) then it is impossible to discover about what the independent pre-merge history was really like.
-Seth Robertson
Quoted 8 lines · Seth Robertson
In message <20121224035825.GA17203@zuhnb712>, Woody Wu writes:
How can I find out what's the staring reference point (a commit number or tag name) of a locally created branch? I can use gitk to find out it but this method is slow, I think there might be a command line to do it quickly.
Quoted 8 lines
The answer is more complex than you probably suspected.
Technically,
git log --oneline mybranch | tail -n 1will tell you the starting point of any branch. But...I'm sure that isn't what you want to know.You want to know "what commit was I at when I typed
git branch mybranch"?
Yes, this is exactly I want to know.
Quoted 9 lines
The problem is git doesn't record this information and doesn't have the slightest clue.
But, you say, I can use
gitkand see it. See? Right there. That isn't (necessarily) the "starting point" of the branch, it is the place where your branch diverged from some other branch. Git is actually quite able to tell you when the last time your branch diverged from some other branch.git merge-base mybranch masterwill tell you this, and is probably the answer you were looking for.
This is not working to me since I have more than one local branch that diverged from the master, and in fact, the branch I have in question was diverged from another local branch. With the method of 'git merge-base', I have to remember a branch tree in my brain.
But thanks anyway, I see you guys's discussions and it's a little hard to understand to me at the moment. Currently, I still have to use gitk with narrowed outputs.
Quoted 7 lines
Note that this is the *last* divergence. If your branch diverged and merged previously that will not be reported. Even worse, if you did a fast-forward merge (I recommend against them in general) then it is impossible to discover about what the independent pre-merge history was really like.
-Seth Robertson
Signature · 2 lines
woody I can't go back to yesterday - because I was a different person then.
Quoted 30 lines · Woody Wu
Quoted 8 lines · Seth Robertson
In message <20121224035825.GA17203@zuhnb712>, Woody Wu writes:
How can I find out what's the staring reference point (a commit number or tag name) of a locally created branch? I can use gitk to find out it but this method is slow, I think there might be a command line to do it quickly.
Quoted 8 lines
The answer is more complex than you probably suspected.
Technically,
git log --oneline mybranch | tail -n 1will tell you the starting point of any branch. But...I'm sure that isn't what you want to know.You want to know "what commit was I at when I typed
git branch mybranch"?Yes, this is exactly I want to know.
Quoted 9 lines
The problem is git doesn't record this information and doesn't have the slightest clue.
But, you say, I can use
gitkand see it. See? Right there. That isn't (necessarily) the "starting point" of the branch, it is the place where your branch diverged from some other branch. Git is actually quite able to tell you when the last time your branch diverged from some other branch.git merge-base mybranch masterwill tell you this, and is probably the answer you were looking for.
Quoted 3 lines
This is not working to me since I have more than one local branch that diverged from the master, and in fact, the branch I have in question was diverged from another local branch.
As Jeff mentions in a later message, "git pull --rebase" would probably do what you want. It works with local branches too.
I once tried to add the same cleverness that "git pull --rebase" directly in "git rebase" [1], but there were several issues with those patches, one of was regarding the performance ("git pull --rebase" can be equally slow, but since it often involves network, users probably rarely notice). I think it would be nice to at least add it as an option to "git rebase" some day. Until then, "git pull --rebase" works fine.
[1] http://thread.gmane.org/gmane.comp.version-control.git/166710
Quoted 31 lines · Martin von Zweigbergk
Quoted 30 lines · Woody Wu
Quoted 8 lines · Seth Robertson
In message <20121224035825.GA17203@zuhnb712>, Woody Wu writes:
How can I find out what's the staring reference point (a commit number or tag name) of a locally created branch? I can use gitk to find out it but this method is slow, I think there might be a command line to do it quickly.
Quoted 8 lines
The answer is more complex than you probably suspected.
Technically,
git log --oneline mybranch | tail -n 1will tell you the starting point of any branch. But...I'm sure that isn't what you want to know.You want to know "what commit was I at when I typed
git branch mybranch"?Yes, this is exactly I want to know.
Quoted 9 lines
The problem is git doesn't record this information and doesn't have the slightest clue.
But, you say, I can use
gitkand see it. See? Right there. That isn't (necessarily) the "starting point" of the branch, it is the place where your branch diverged from some other branch. Git is actually quite able to tell you when the last time your branch diverged from some other branch.git merge-base mybranch masterwill tell you this, and is probably the answer you were looking for.
Quoted 7 lines
Quoted 3 lines
This is not working to me since I have more than one local branch that diverged from the master, and in fact, the branch I have in question was diverged from another local branch.
As Jeff mentions in a later message, "git pull --rebase" would probably do what you want. It works with local branches too.
I think what 'git pull --rebase' would do is to fetch from the origin and do a 'git rebase'. On one hand, I don't understand 'git rebase' so much from the manual, ont the other hand, I did not get the point why 'git rebase' has something to do with the thing I want to do (what I want is just query some kind of history information).
I know, my knowledge about git is still so limit. I will keep study from the man pages.
Quoted 9 lines
I once tried to add the same cleverness that "git pull --rebase" directly in "git rebase" [1], but there were several issues with those patches, one of was regarding the performance ("git pull --rebase" can be equally slow, but since it often involves network, users probably rarely notice). I think it would be nice to at least add it as an option to "git rebase" some day. Until then, "git pull --rebase" works fine.
[1] http://thread.gmane.org/gmane.comp.version-control.git/166710
Signature · 2 lines
woody I can't go back to yesterday - because I was a different person then.
Quoted 7 lines · Woody Wu
Quoted 6 lines · Martin von Zweigbergk
Quoted 4 lines · Woody Wu
This is not working to me since I have more than one local branch that diverged from the master, and in fact, the branch I have in question was diverged from another local branch.
Quoted 6 lines
Quoted 3 lines
As Jeff mentions in a later message, "git pull --rebase" would probably do what you want. It works with local branches too.
I think what 'git pull --rebase' would do is to fetch from the origin and do a 'git rebase'.
Not if the configured upstream is a local branch (see the "branch.<name>.*" configuration variables). In that case it will just rebase the local branch onto the new position of its upstream. If the upstream is not configured, I believe you can still do "git pull --rebase . <upstream branch>".
Quoted 4 lines
On one hand, I don't understand 'git rebase' so
much from the manual, ont the other hand, I did not get the point why 'git rebase' has something to do with the thing I want to do (what I want is just query some kind of history information).
I may have misunderstood or assumed things incorrectly that you wanted to rebase the commits on your branch. So why do you want to know? (Please ignore me if this was answered elsewhere in the thread that I might have missed.)
Anyway, to answer your question, you could use a method similar to what "git pull --rebase" uses internally to figure out the branch point:
git merge-base $(git rev-parse <branch>) $(git rev-list -g <upstream branch>)
Hope that helps
Quoted 3 lines · Woody Wu
But thanks anyway, I see you guys's discussions and it's a little hard to understand to me at the moment. Currently, I still have to use gitk with narrowed outputs.
Each commit refers to it's parent. If you take a branch, and keep following the first parent of the commits, you will finally reach the root commit (the first commit made). You don't see any branches, it's just one straight line. That's why git can't tell you where a branch started, because it all looks like a single branch to git.
With merge-base, you basically give git two branches, and git finds the first ancestor that is common to both branches.
See [1] for an example. If you follow the arrows from branch a, you get from E to A (note that commit C doesn't know about commit F pointing to it). So it looks like there is only a single branch.
If you do git merge-base a b, git sees both branches have commit C in common, and reports that as the merge-base.
Quoted 10 lines · Seth Robertson
In message <20121224035825.GA17203@zuhnb712>, Woody Wu writes:
How can I find out what's the staring reference point (a commit number or tag name) of a locally created branch? I can use gitk to find out it but this method is slow, I think there might be a command line to do it quickly.
The answer is more complex than you probably suspected.
Quoted 7 lines
Technically,
git log --oneline mybranch | tail -n 1will tell you the starting point of any branch. But...I'm sure that isn't what you want to know.You want to know "what commit was I at when I typed
git branch mybranch"? The problem is git doesn't record this information and doesn't have the slightest clue.
Maybe we should store this information. reflog is a perfect place for this, I think. If this information is reliably available, git rebase can be told to "rebase my whole branch" instead of my choosing the base commit for it.
Signature · 1 line
Duy
Quoted 15 lines · Nguyen Thai Ngoc Duy
Quoted 14 lines · Seth Robertson
In message <20121224035825.GA17203@zuhnb712>, Woody Wu writes:
How can I find out what's the staring reference point (a commit number or tag name) of a locally created branch? I can use gitk to find out it but this method is slow, I think there might be a command line to do it quickly.
The answer is more complex than you probably suspected.
Technically,
git log --oneline mybranch | tail -n 1will tell you the starting point of any branch. But...I'm sure that isn't what you want to know.
Quoted 8 lines
Quoted 3 lines
You want to know "what commit was I at when I typed
git branch mybranch"? The problem is git doesn't record this information and doesn't have the slightest clue.Maybe we should store this information. reflog is a perfect place for this, I think. If this information is reliably available, git rebase can be told to "rebase my whole branch" instead of my choosing the base commit for it.
What's the starting point of the branch if I type: git branch foo <commit-ish>?
Quoted 6 lines · Tomas Carnecky
Quoted 4 lines
Maybe we should store this information. reflog is a perfect place for this, I think. If this information is reliably available, git rebase can be told to "rebase my whole branch" instead of my choosing the base commit for it.
What's the starting point of the branch if I type: git branch foo <commit-ish>?
You start working off <commit-ish> so I think the starting point would be <commit-ish>.
Signature · 1 line
Duy
Quoted 11 lines · Nguyen Thai Ngoc Duy
Quoted 6 lines · Tomas Carnecky
Quoted 4 lines
Maybe we should store this information. reflog is a perfect place for this, I think. If this information is reliably available, git rebase can be told to "rebase my whole branch" instead of my choosing the base commit for it.
What's the starting point of the branch if I type: git branch foo <commit-ish>?
You start working off <commit-ish> so I think the starting point would be <commit-ish>.
Yeah, that sounds sensible. Don't we already have it in the reflog, though?
What is trickier is when you later transplant it to some other base (perhaps prepare a topic on 'master', realize it should better apply to 'maint' and move it there). If the user did the transplanting by hand, reflog would probably not have enough information, e.g. after
$ git checkout maint^0
$ git log --oneline master..topic
$ git cherry-pick $one_of_the_commit_names_you_see_in_the_above
$ git cherry-pick $another_commit_name_you_see_in_the_above
...
$ git branch -f topicno reflog other than HEAD@{} will tell you that you were at maint^0, so the reflog of topic wouldn't know it "forked" from there, either.
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Nguyen Thai Ngoc Duy
Quoted 5 lines · Tomas Carnecky
Quoted 4 lines
Maybe we should store this information. reflog is a perfect place for this, I think. If this information is reliably available, git rebase can be told to "rebase my whole branch" instead of my choosing the base commit for it.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
What's the starting point of the branch if I type: git branch foo <commit-ish>?
You start working off <commit-ish> so I think the starting point would be <commit-ish>.
Yeah, that sounds sensible. Don't we already have it in the reflog, though?
I looked briefly at reflog before writing my previous mail and noticed that when I create a new branch (usually using "git checkout -b branch ref") it does not record the base commit.
Quoted 12 lines
What is trickier is when you later transplant it to some other base (perhaps prepare a topic on 'master', realize it should better apply to 'maint' and move it there). If the user did the transplanting by hand, reflog would probably not have enough information, e.g. after
$ git checkout maint^0 $ git log --oneline master..topic $ git cherry-pick $one_of_the_commit_names_you_see_in_the_above $ git cherry-pick $another_commit_name_you_see_in_the_above ... $ git branch -f topic
Quoted 2 lines
no reflog other than HEAD@{} will tell you that you were at maint^0, so the reflog of topic wouldn't know it "forked" from there, either.
We could at least invalidate the recorded base in reflog and let user define a new one (I hope).
Signature · 1 line
Duy
Quoted 15 lines · Nguyen Thai Ngoc Duy
Quoted 14 lines · Junio C Hamano
Quoted 11 lines · Nguyen Thai Ngoc Duy
Quoted 6 lines · Tomas Carnecky
Quoted 4 lines
Maybe we should store this information. reflog is a perfect place for this, I think. If this information is reliably available, git rebase can be told to "rebase my whole branch" instead of my choosing the base commit for it.
What's the starting point of the branch if I type: git branch foo <commit-ish>?
You start working off <commit-ish> so I think the starting point would be <commit-ish>.
Quoted 6 lines
Quoted 2 lines
Yeah, that sounds sensible. Don't we already have it in the reflog, though?
I looked briefly at reflog before writing my previous mail and noticed that when I create a new branch (usually using "git checkout -b branch ref") it does not record the base commit.
Hmph. Perhaps you are referring to something different than what I think "the base commit" with that word.
$ git reflog mz/pick-unborn | tail -n 1
b3cf6f3 mz/pick-unborn@{3}: branch: Created from ko/masterQuoted 12 lines
Quoted 12 lines
What is trickier is when you later transplant it to some other base (perhaps prepare a topic on 'master', realize it should better apply to 'maint' and move it there). If the user did the transplanting by hand, reflog would probably not have enough information, e.g. after
$ git checkout maint^0 $ git log --oneline master..topic $ git cherry-pick $one_of_the_commit_names_you_see_in_the_above $ git cherry-pick $another_commit_name_you_see_in_the_above ... $ git branch -f topic
Quoted 5 lines
Quoted 2 lines
no reflog other than HEAD@{} will tell you that you were at maint^0, so the reflog of topic wouldn't know it "forked" from there, either.
We could at least invalidate the recorded base in reflog and let user define a new one (I hope).
Please do not even think about going back and rewrite to lose information. If the records have full information, you should be able to reconstruct what you want from it without rewriting.
Even more importantly, wish to "invalidate" indicates that you know at a newer point that you have more authoritative information than the older reflog entries, so you should be able to do the moral equivalent by writing the event as establishing a new base at that point (e.g. "checkout -B"), and stopping at that point in the reflog when reading, without losing the older reflog entries.
Quoted 9 lines · Junio C Hamano
Quoted 3 lines
I looked briefly at reflog before writing my previous mail and noticed that when I create a new branch (usually using "git checkout -b branch ref") it does not record the base commit.
Hmph. Perhaps you are referring to something different than what I think "the base commit" with that word.
$ git reflog mz/pick-unborn | tail -n 1 b3cf6f3 mz/pick-unborn@{3}: branch: Created from ko/master
No you're right. My reflogs must be pruned. Creating a new branch does produce that entry.
Quoted 7 lines
Quoted 2 lines
We could at least invalidate the recorded base in reflog and let user define a new one (I hope).
Please do not even think about going back and rewrite to lose information. If the records have full information, you should be able to reconstruct what you want from it without rewriting.
Quoted 6 lines
Even more importantly, wish to "invalidate" indicates that you know at a newer point that you have more authoritative information than the older reflog entries, so you should be able to do the moral equivalent by writing the event as establishing a new base at that point (e.g. "checkout -B"), and stopping at that point in the reflog when reading, without losing the older reflog entries.
Exactly. And when we prune the reflog, we could add the base back so the base is always in reflog.
Signature · 1 line
Duy
Quoted 8 lines · Nguyen Thai Ngoc Duy
Quoted 3 lines
You want to know "what commit was I at when I typed
git branch mybranch"? The problem is git doesn't record this information and doesn't have the slightest clue.Maybe we should store this information. reflog is a perfect place for this, I think. If this information is reliably available, git rebase can be told to "rebase my whole branch" instead of my choosing the base commit for it.
Is that what you really want, though? We record the "upstream" branch already, and you can calculate the merge base with that branch to see which commits are unique to your branch. In simple cases, that is the same as "where did I start the branch". In more complex cases, it may not be (e.g., if you merged some of the early commits in the branch already). But in that latter case, would you really want to rebase those commits that had been merged?
The reason that git does not bother storing "where did I start this branch" is that it is usually not useful. The right question is usually "what is the merge base". There are exceptions, of course (e.g., if you are asking something like "what work did I do while checked out on the 'foo' branch"). But for merging and rebasing, I think the computed merge-base is much more likely to do what people want.
-Peff
Quoted 19 lines · Jeff King
Quoted 8 lines · Nguyen Thai Ngoc Duy
Quoted 3 lines
You want to know "what commit was I at when I typed
git branch mybranch"? The problem is git doesn't record this information and doesn't have the slightest clue.Maybe we should store this information. reflog is a perfect place for this, I think. If this information is reliably available, git rebase can be told to "rebase my whole branch" instead of my choosing the base commit for it.
Is that what you really want, though? We record the "upstream" branch already, and you can calculate the merge base with that branch to see which commits are unique to your branch. In simple cases, that is the same as "where did I start the branch". In more complex cases, it may not be (e.g., if you merged some of the early commits in the branch already). But in that latter case, would you really want to rebase those commits that had been merged?
Quoted 6 lines
The reason that git does not bother storing "where did I start this branch" is that it is usually not useful. The right question is usually "what is the merge base". There are exceptions, of course (e.g., if you are asking something like "what work did I do while checked out on the 'foo' branch"). But for merging and rebasing, I think the computed merge-base is much more likely to do what people want.
Rebasing is exactly why I want this. Merge base works most of the time until you rewrite upstream (which I do sometimes). There are also cases when I create a branch without upstream, or when upstream is renamed. Still, making "rebase -i --topic" == "rebase -i $(git merge-base HEAD @{upstream})" would be cool.
Signature · 1 line
Duy
Quoted 9 lines · Nguyen Thai Ngoc Duy
Quoted 6 lines
The reason that git does not bother storing "where did I start this branch" is that it is usually not useful. The right question is usually "what is the merge base". There are exceptions, of course (e.g., if you are asking something like "what work did I do while checked out on the 'foo' branch"). But for merging and rebasing, I think the computed merge-base is much more likely to do what people want.
Rebasing is exactly why I want this. Merge base works most of the time until you rewrite upstream (which I do sometimes).
True, although wouldn't you generally want to rebase it on top of the rewritten upstream in that case (which is what "pull --rebase" will do, by scanning the reflog for the last version of the upstream that you actually built on).
Quoted 3 lines
There are also cases when I create a branch without upstream, or when upstream is renamed. Still, making "rebase -i --topic" == "rebase -i $(git merge-base HEAD @{upstream})" would be cool.
Yeah. I usually just do "rebase -i @{upstream}" which picks the same commits, but moves to the updated version of upstream (IOW, I both rewrite and move forward at the same time). But there is value in rewriting without moving forward in many workflows. That seems like a sensible feature to me.
-Peff