GIT: Modifying Files and Committing with Same Date / Time

It seems that it is not easy to write a file with different content, but with the same date / time.

In the following situation:

  • Create a git repository
  • Add file "foo.bar" with specific creation date / time fred / lastwrite (eg 2015-01-01 00:00:00)
  • Commit this file
  • Change the content of "foo.bar" and set the date / time back to one value
  • call "git status" => do not commit anything, working directory is inactive
  • call "git commit" => No changes; do nothing

How can I force commit !?

Here is the playback code with 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.
      }
    }
  }
}

      

+3


source to share


1 answer


I could reproduce it even without libgit2sharp

(using TortoiseGit and msysgit).

This is a known issue:
https://github.com/msysgit/git/issues/312
https://groups.google.com/forum/#!topic/git-users/Uo9TDppHTSI



I managed to get it to detect the changes by running: git read-tree HEAD

in the console. If your library allows you to run these (or arbitrary) commands, it might help as well.

Anyway, this is what git is deliberately fighting against, so I would advise against manually changing ModifiedDate

it if possible.

+2


source







All Articles