GIT:修改文件并使用相同的日期/时间进行提交

时间:2015-08-10 06:47:42

标签: c# git libgit2sharp

似乎不容易提交具有不同内容但具有相同日期/时间的文件。

以下情况:

  • 创建一个git存储库
  • 使用定义的日期/时间fpr创建/ lastwrite添加文件“foo.bar”(例如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.
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

即使没有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

相关问题