Skip to content

Instantly share code, notes, and snippets.

@christoph-jerolimov
Last active December 3, 2021 20:41
Show Gist options
  • Save christoph-jerolimov/5881319 to your computer and use it in GitHub Desktop.
Save christoph-jerolimov/5881319 to your computer and use it in GitHub Desktop.
Initialize a git repository afterwards with separate commits based on the file modified timestamp. Simple run git init && git afterwards
#!/bin/bash
IFS=$(echo -en "\n\b")
# Ensure we are in a git repo with gitignore.
if [ ! -d .git ]; then echo "Current working directory is not a git repository."; exit -1; fi
#if [ ! -f .gitignore ]; then echo "Current working directory has no .gitignore file."; exit -2; fi
# Sort (by date) and save new files
for file in `git status --porcelain --untracked-files=all | grep -v '.log$' | grep '^?? ' | sed 's/^?? //g'`
do
echo `stat -f '%m' "$file"` $file
done | sort > .gitinit.log
# Add and commit the files
while read line
do
date=`echo $line | sed 's/ .*//g'`
file=`echo $line | sed 's/^[0-9]\{10\} //g'`
git add "$file"
GIT_AUTHOR_DATE=$date GIT_COMMITTER_DATE=$date git commit -m "$file"
done < .gitinit.log
rm .gitinit.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment