简体   繁体   中英

How to change the latest commit date to oldest?

So I'm new to github. I have committed my readme.md file 3 days ago, but I found out that there was a mistake on my readme.md file. I want to edit the file, but if I edit it, the commit date will change to the current timestamp. Is there any way that I can edit the file without changing the commit date to the current timestamp? I'd like to keep the first commit date as I commit changes.

So I'm new to github. I have committed my readme.md file 3 days ago, but I found out that there was a mistake on my readme.md file. I want to edit the file, but if I edit it, the commit date will change to the current timestamp.

That's sort of right, but also sort of wrong. Most importantly, it suggests that you're thinking of Git as storing files—but Git doesn't store files. Git stores commits .

The thing that has a date is not the file, it's the commit . The commit then contains files , but every commit contains every file . So if you make a new commit now with the old readme.md file in it, that new commit also has the current time as its timestamp. It's just that the new copy of readme.md is the old copy of readme.md . Those two files have the same contents, so Git, which de-duplicates files based on content, stores the content only once.

If you assign the commit dates to the file, this means the file now has two separate dates. In fact, of course, the commit dates are assigned to the commits . There are two different commits, with two different dates. The two different commits may have the same (shared) readme.md , or may have two different readme.md files.

Is there any way that I can edit the file without changing the commit date to the current timestamp?

Whenever you make a new commit, it gets a new commit timestamp. In fact, though, it gets two new commit timestamps. One of these two is very easy to control because you can use --date= date to give it a particular date-and-time-stamp. The other is harder to control: it always defaults to "now".

So, if you like, you can easily set one of the two dates to any value you want. The one you can set so easily, using a command-line argument, is the author date . The other date is the one that Git calls the committer date and there are reasons to avoid setting it out of order, 1 but Git will allow you to override it if you are sufficiently determined. Simply set the environment variable GIT_COMMITTER_DATE before running git commit —it takes the same string values as the --date= option—and Git will believe that your computer's clock is set to that value and will therefore use that as the commit timestamp.

There is rarely a good reason to do this kind of date manipulation. The fact is that files evolve, including README-type files, and if you fix one, it's just like fixing any other bug. The file was wrong in the old commits, and is corrected in this commit and each subsequent commit that also contains the file. Tools like git blame will find the commit that fixed the specific fixed line(s), and will keep looking back in history for the author of previous lines.


1 One reason is that it will confuse humans. A more technical reason is less describable and usually not all that interesting anyway: for particularly large commit graphs, Git is growing some optimizations these days that involve adding auxiliary graph data, where out-of-order committer dates make things more complicated and potentially slower.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM