Accurate git commits

2019-07-31 23:56发布

If I have some code like

/**
 * blah blah blah
 */
...some codes...
/**
 * yadda yadda

And then I add

/**
 * blah blah blah
 */
...some codes...
/**
 * blah blah blah
 */
...some codes...

before the "yadda yadda" commanet, the git diff will show that I added:

 +  * blah blah blah
 +  */
 +  ...some codes...
 + /**

Is there a way to tell git, "hey, that's not right. Try again."? I know of --patience but that seems to just be for git diff and for the life of me it never works correctly. I know it's no super important, but it makes diffs and commit logs, especially on GitHub, much more clean.

1条回答
贪生不怕死
2楼-- · 2019-08-01 00:39

The way Git stores files in the repository is on a whole-file basis. When you ask for a difference between two versions, Git retrieves both complete files from the repository1, and runs the diff algorithm between them. Unlike some other source control systems, Git does not calculate or store the differences between files at the time of commit.

This method has the advantage of easily changing the behaviour of the diff at the time you ask for it. For example, you have seen the --patience flag that uses a different diff algorithm to determine a (possibly) better patch between two files.

Because of the way files are stored in the Git repository, it is not possible to tell Git to somehow keep track of a custom diff alignment.

  1. Deep down in the Git repository, inside .pack files, Git uses various delta encoding methods to store differences between similar objects and thus reduce the size of the repository. However, this is on a lower level than the file storage and are not actual diffs between source files.
查看更多
登录 后发表回答