user9368716
user9368716

Reputation:

Do not show a file as modified if only comment is changed

I have a list of files that git says are changed because there is a change in a comment. How can I configure Git to ignore files (not show as modified), when only comment is changed. For example The original file contains;

def foo():
    # Return
    return bar

Now the file is changed to (Comment is changed to # Return bar)

def foo():
    # Return bar
    return bar

Now when I run git status, the above file is shows as modified. I want this change to be ignored. How can I configure Git to ignore these files.

EDIT: The date and time are written as comments in my .py file. When only these comments change I do not want to commit this file with my other files (where the change needs to be committed).

Upvotes: 0

Views: 309

Answers (1)

Xetius
Xetius

Reputation: 46874

As many of the commentors have said, you are fixing the wrong problem.

There is not really a way to do this in git, as it uses the entire file contents to create a SHA. This SHA is compared to the previous SHA. If the SHA changes, this means the contents of the file has changed.

I can't think of a way of doing this, which would leave the file contents consistent across all versions. If the comments (date/time) are not important enough to be stored in git, then don't add them there in the first place. If it is your editor or IDE that is doing it, then either stop it doing it or change the editor/IDE

Your comment regarding this being a company requirement was hidden initially for me. As this is a requirement for the company, then you need to get them to change policy (This is duplicating information stored in the git metadata for the commit) or accept it and carry on.

git blame will give you better information on who is making changes and when, as it identifies who has made each change to the file, and when.

Edit: As mentioned below by MarkAdelsberger below, git status does use the file's stat information, but as stated, this still leads to the same answer. Here are a technical doc and a blog post which explain some of the details.

Upvotes: 1

Related Questions