Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rebase: rebased commit still in index?

Tags:

git

git-rebase

When I read about git-rebase, I understood the rebased commits should get lost. I say should because I noticed that, knowing the rebased commit sha, I can recall it.
Suppose I have the following three commits

A -> B -> C

where C's sha is cshaid. Then, if I interactively rebase fixing-up C into B with git rebase -i HEAD~2 and then I check the result with git log, I obtain the expected result, meaning

A -> B'

where B''s sha is different from B's sha.
However, running git log cshaid shows again

A -> B -> C

Questions: is this a known behavior? I tried reading git rebase --help but couldn't find related info. Why rebased commits are not simply forgot? I mean, rebase is kind of a dangerous operation to be performed only if you know what you are doing and you can do it, which is the point in having a dirty index (or wherever these useless commits are kept)? Am I missing something?
Step to reproduce (and to better understand my doubts). If you are willing to reproduce the situation, try with:

  1. mkdir sampledir && cd sampledir && git init
  2. touch file && git add -A . && git commit -m "Initial"
  3. edit file, then git commit -am "First modification"
  4. edit file, then git commit -am "Second modification"
  5. git log, you will see three commits, remember the sha for Second modification
  6. git rebase -i HEAD~2, the fixup Second modification into First modification
  7. git log, you will see two commits, where the sha for First modification is now different than in step 5
  8. however, git log sha-for-"Second modification" will show the exact same tree as point 5 in this list
like image 711
ThanksForAllTheFish Avatar asked Jun 11 '26 12:06

ThanksForAllTheFish


1 Answers

Yes, this is the expected behavior. Unreferenced commits will eventually be garbage collected and thereby purged from disk. They're kept around for a number of days (by default 14), but before that 14-day timer even starts ticking the objects must have expired from the reflog as well (unreachable objects by default expire after 30 days).

Related StackOverflow questions:

  • git reflog expire and git fsck --unreachable
  • Listing and deleting Git commits that are under no branch (dangling?)
like image 80
Magnus Bäck Avatar answered Jun 14 '26 04:06

Magnus Bäck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!