Home > Net >  How can I add new files to git based on their last modification date?
How can I add new files to git based on their last modification date?

Time:07-15

I work locally under Windows. I have a local git repo and already included some files. The folder (and subfolders) where I am working in includes many other files that are not yet tracked and not known to git. Some of these files have not been changed for many months. I need to add some, but not all of these files. The criterium for inclusion should be the date of last change (e.g. I want to "git add" only files that have been modified within the last month).

Is there a way to do this? Searching help files and threads here only showed me solutions when the files are already tracked.

CodePudding user response:

Not possible with git only, but if you have "git bash" or WSL available, you should be able to run something like

find . -type f -mtime -31 -not -path '*/.git/*' -exec git add -v -- {}  

to add files modified within the last 31 days.

Notes:

  • If you only want to include certain folders, specify their names instead of .
  • To list which files are being matched by the specified age,
    run the command without the -exec part:
    find . -type f -mtime -31 -not -path '*/.git/*'
  • To list which files would be added without actually adding them,
    use git add's --dry-run option (short: -n):
    find . -type f -mtime -31 -not -path '*/.git/*' -exec git add -v -n -- {}

Edit history:

  • Instead of find's -print expression use git add's -v option (a.k.a. --verbose). This way only the files that were actually added are listed, not all files that were passed to the git add command.
  • Add dry-run suggestion to notes
  • Related