似乎提交一个内容不同但日期/时间相同的文件并不容易。
以下情况:
创建git存储库
添加一个文件“foo.bar”,其中包含定义的日期/时间fpr creating/lastwrite(例如2015-01-01 00:00:00)
提交此文件
修改“foo.bar”内容并再次将日期/时间设置为相同的值
call“git status”=>无需提交,工作目录清除
call“git commit”=>无更改;无需提交
我怎么能强迫你这么做!是吗?
以下是libgit2sharp的重发代码:
using System.IO;
using LibGit2Sharp;
using System;
namespace GitWorkingUpdateProblem01
{
class Program
{
static void Main(string[] args)
{
const string repoDir = @".\git-Test";
Repository.Init(repoDir);
using (var repo = new Repository(repoDir))
{
string fileName = Path.Combine(repo.Info.WorkingDirectory, "foo.bar");
var dt = new DateTime(2015, 01, 01, 00, 00, 00);
using (var sw = new StreamWriter(fileName, false))
{
sw.WriteLine("UNIQUE-TEXT-1234");
}
File.SetLastWriteTime(fileName, dt); File.SetCreationTime(fileName, dt);
repo.Stage(fileName); repo.Commit("1");
using (var sw = new StreamWriter(fileName, false))
{
sw.WriteLine("UNIQUE-TEXT-4321");
}
File.SetLastWriteTime(fileName, dt); File.SetCreationTime(fileName, dt);
repo.Stage(fileName); repo.Commit("2"); // ==> THROWS: No changes; nothing to commit.
}
}
}
}
最佳答案
我可以复制它,即使没有libgit2sharp
(使用tortoisegit&msysgit)。
这是一个众所周知的问题:
https://github.com/msysgit/git/issues/312
https://groups.google.com/forum/#!topic/git-users/Uo9TDppHTSI
我可以通过运行以下命令来检测更改:git read-tree HEAD
在控制台中。如果您的库允许您运行此(或任意)命令,那么它也可能有帮助。
无论如何,这是故意与git抗争的,因此如果可能的话,我建议不要手动更改ModifiedDate
。