threads / discuss / 48308

git-checkout -B resets branch not showing last revision

Subject: git-checkout -B resets branch not showing last revision

## tl;dr

2 messages between Apr 17, 2018 and Apr 17, 2018.

replies: 1people: 2as markdown or json

Haralan Dobrev· Apr 17, 2018, 08:31 UTC · lore
Hi,
I recently had the following case:
1. I have a local branch called `foo` which I haven't checkout recently.
2. Due to a bad alias I ran the following command:
    git-checkout -B foo

This checked out and reset the foo branch. Which is according to the documentation. However, the output of the command does not show the previous state of the foo branch unlike what `git-reset --hard` does for some time.

The previous revision is not present in the `reflog` either as I haven't checked it out.

I think it would be beneficial to Git users to show the previous revision in all operations resulting in reseting a branch.

Thanks, Harry

Junio C Hamano· Apr 17, 2018, 08:45 UTC · re: Haralan Dobrev · lore

Re: git-checkout -B resets branch not showing last revision

Haralan Dobrev <harry@hkdobrev.com> writes:
Show 15 quoted lines
> I recently had the following case:
>
> 1. I have a local branch called `foo` which I haven't checkout recently.
> 2. Due to a bad alias I ran the following command:
>
>     git-checkout -B foo
>
> This checked out and reset the foo branch. Which is according to the
> documentation.
> However, the output of the command does not show the previous state of
> the foo branch
> unlike what `git-reset --hard` does for some time.
>
> The previous revision is not present in the `reflog` either as I
> haven't checked it out.

A reflog entry is a record of Old and New object names, with timestamp of the transition and a comment left by the tool. You may see only one record in "git reflog foo@{now}" output, but that record actually knows both the new commit (i.e. the HEAD when you ran "checkout -B") and the old one (i.e. the one you are asking to know). It is only the stupid "git reflog" output that only shows the "new" side of the entry.

So if you do
	head -n1 .git/logs/refs/heads/foo
you'll find the "Old" state at the leftmost column.

I personally do not think it is a good remedy to add more cruft to the output from checkout etc. like we do for "reset --hard" (if anything, I think we should actually remove it from the latter, and show it only asked for --verbose, or something). These ephemeral output messages can easily be missed and not get noticed until they scroll away out of the terminal window.

Teaching "git reflog" to show one extra "fake" entry using the "Old" side of the oldest reflog entry would be a much better solution.

← back to recent threads