That the reason to make harder work with anonymous branches in git (so named lost commits)?

StackOverflow https://stackoverflow.com/questions/20634488

  •  18-09-2022
  •  | 
  •  

Question

I read http://stevelosh.com/blog/2009/08/a-guide-to-branching-in-mercurial/ where comparing with git for Anonymous branches shown:

Git has no real way to handle this. Sure, it lets you update and
commit, but if you don’t create a (named) ref to that new commit
you’re never going to find it again once you switch to another
one. Well, unless you feel like grep’ing through a bunch of log
output.

Oh, and hopefully it doesn’t get garbage collected.

Sometimes you might not want to think up a name for a quick-fix
branch. With git you have to name it if you want to really do
anything with it, with Mercurial you don’t.

So can I switch to intermediate project state (by git reset --hard 6aa32cfecf4 or something) and commit sequence of changes without taking name to branch?

hg heads shown all heads including anonymous branches. Is this possible from CLI find all heads from these anonymous branches with git?

Was it helpful?

Solution

You can indeed switch to an intermediate project state, although you want to use git checkout 6aa32cf instead of git reset --hard; git reset will move the HEAD branch to that state, which is not what you want. git will warn you that you're in a "detached HEAD" state, which means that it doesn't know which branch to advance when you commit. You can certainly still work and commit, though.

If you do make commits, and then checkout another branch without naming the branch you're on, git will warn you:

ash@autumn ~/code/peril ((9fae92d...)) $ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  9fae92d Commit to find later.

If you want to keep them by creating a new branch, this may be a good time
to do so with:

 git branch new_branch_name 9fae92d5d5698096f0c1ddc11773ffded1194af2

Switched to branch 'master'

Even if you don't follow its instructions, you can still find the commit later with git reflog show HEAD and grep if you remember the commit message you're looking for:

35f4222 HEAD@{0}: checkout: moving from 9fae92d5d5698096f0c1ddc11773ffded1194af2 to master
9fae92d HEAD@{1}: commit: Commit to find later.
0d4b27b HEAD@{2}: checkout: moving from master to 0d4b27b379

Or, even later, with git fsck --unreachable --no-reflogs and some scripting.

OTHER TIPS

I don’t understand the grammar of your main question.

But I’ld like to add:

  • “lost” git branches might be garbage collected after 30 days (default value) and not before
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top