Saturday, April 24, 2021

How do we get the "diff" or the patch for the very first commit in Git?

This is a summary of several Stack Overflow discusses about how to get the "diff" or the patch for the very first commit in Git.

  1. To get the diff from the very first commit to the empty tree, we can use the following command.
    git diff \
        4b825dc642cb6eb9a060e54bf8d69288fbee4904 \
        $(git log --reverse | head -1 | cut -d' ' -f2)
      
    where 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the SHA of the empty tree and $(git log --reverse | head -1 | cut -d' ' -f2) retrieves the SHA of the very first commit. Alternatively, we can rewrite the above in a way that perhaps is easier to read.
  2. To get the diff from the very first commit to the empty tree, we can use the following command.
    git diff \
        $(printf '' | git hash-object -t tree --stdin) \
        $(git log --reverse | head -1 | cut -d' ' -f2)
      
  3. To get the patch from the very first commit to the empty tree, we can use the following command.
    git format-patch \
        --root --stdout \
        $(git log --reverse | head -1 | cut -d' ' -f2)
        

Reference 

  1. How to get Git diff of the first commit?  
  2. How to use git format-patch on initial commit

No comments:

Post a Comment