Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git clone changes file modification time

Tags:

git

clone

When I clone a Git repository using the "git clone ..." command, all cloned files in my local repository have the same modification time with date and time as when the git clone command was issued.

Is there a way to clone a remote Git repository with the actual modification time for each file?

like image 761
user3302761 Avatar asked Feb 12 '14 17:02

user3302761


People also ask

Does Git save file modification time?

Git stores the last modification time for each file, based on its commit history.

Can you clone a repository multiple times?

Simply clone your project's repo twice (or even more often). When your work on a feature branch is done, simply push that branch and check it out on your 2nd copy to run tests there. You can pick up a new story and work on that on your "main" project directory.

How do I get the last modification time in Git?

This is an operating system issue, not a Git issue. You can get the time of the last modification from git log -n1 -- file; that is what git is for. I do not quite understand the statement "this is what git is for".

Why does git show the same time every time a file changes?

As a result, the times displayed times match the last commit that changed each file. If the file had a different timestamp on disk at the time the original commit was made, it was not ever stored anywhere in the Git repository and as such it cannot ever be restored without an external data source.

How to undo changes that haven't been committed in Git?

Changes that haven't been committed to the local repository are called "local" changes in Git. They exist in your Working Copy, but you haven't wrapped them in a commit, yet. If you want to discard this type of changes, you can use the git restore command: git restore index.html. This will undo all uncommitted local changes in the specified file.

How do I discard local changes in Git?

Discarding Local Changes in a File Changes that haven't been committed to the local repository are called "local" changes in Git. They exist in your Working Copy, but you haven't wrapped them in a commit, yet. If you want to discard this type of changes, you can use the git restore command:


2 Answers

Git does not record timestamp for the files, since it is a Distributed VCS (meaning the time on your computer can be different from mine: there is no "central" notion of time and date)

The official argument for not recording that metadata is explained in this answer.

But you can find scripts which will attempt to restore a meaningful date, like this one (or a simpler version of the same idea).

like image 102
VonC Avatar answered Oct 01 '22 11:10

VonC


You can retrieve the last modification date of all files in a Git repository (last commit time). See How to retrieve the last modification date of all files in a Git repository.

Then use the touch command change the modification date:

git ls-tree -r --name-only HEAD | while read filename; do   unixtime=$(git log -1 --format="%at" -- "${filename}")   touchtime=$(date -d @$unixtime +'%Y%m%d%H%M.%S')   touch -t ${touchtime} "${filename}" done 

Also see my gist here.

like image 41
ERU Avatar answered Oct 01 '22 11:10

ERU