1

I was trying to split my work into multiple commits. I have done this:

git add --patch my_file
# accepted, rejected, manually edited many hunks
# --->
# now my silly mistake comes
git add my_file
git commit -m "some commit message"

I shouldn't have add that file as my changes are already on the staging index. Can I go back to that state (marked as ---> above)?

Asocia
  • 5,935
  • 2
  • 21
  • 46
  • 1
    Yes, `git reset my_file`, `git add -p my_file` and `git commit --amend`. – jthill Apr 29 '21 at 12:58
  • Do you search for a way to return one commit back and redo your operations (without the 2nd `add`) (in which case jthill's suggestion is perfect)? Or a way *to avoid* redoing the interactive add step? In the latter case, index having been rewritten, I'm afraid you won't be able to do that. – Romain Valeri Apr 29 '21 at 12:58
  • @jthill I know I can redo the patch. However, since it was a bit long process I don't want to do it again. – Asocia Apr 29 '21 at 12:59
  • @RomainValeri I'm trying to avoid the redoing of interactive add step. – Asocia Apr 29 '21 at 13:00
  • 1
    Right, @RomainValeri tagged it. The object is in your repo, digging it out will be annoying but doable. – jthill Apr 29 '21 at 13:01
  • 2
    @jthill Yes, I didn't want to wander into plumbing feats, but you're right, technically the tree OP would like to recover *has* been written to .git/objects after the interactive add... probably interesting for an educational perspective, but clearly less efficient and pragmatic than just redoing the add step. – Romain Valeri Apr 29 '21 at 13:05
  • OK, thank you both. I just wanted to make sure if there is something I can do before proceeding. – Asocia Apr 29 '21 at 13:05
  • 3
    My first try would be to find files in `.git/objects` about the same size and changed about when you did the correct add, show those until you see the one you wanted, then `git update-index --cacheinfo 100644,$thathash,my_file`. `find .git/objects -mmin -30m -mmin +5m` for less than half an hour ago but more than 5 minutes. – jthill Apr 29 '21 at 13:06
  • @jthill I decided to dig this down for just educational purposes. Now I have just 5 different hashes when performed a `find`. Can you tell me what does `100644` mean in the command you wrote? Can I write it directly? – Asocia Apr 29 '21 at 13:24
  • 1
    Oh it means mode. Somehow, I couldn't see it in the man page at the first time. – Asocia Apr 29 '21 at 13:25

0 Answers0