This guide will help you create a new Git repository from an existing project while preserving the original file modification timestamps. This way, your commit history will reflect the chronological changes made to the project.
Follow these steps: (needs bash & git)
First, navigate to your project’s directory and initialize a new Git repository.
cd /path/to/your/project
git init
Use the find
and stat
commands to gather the last modified timestamps of all files in the project and sort them by modification time.
find . -type f -exec stat -c '%Y:%n' {} + | sort -n > file_mod_times.txt
Create a script named commit_files_by_day.sh
to read the file_mod_times.txt
file, group files by day, and commit files as per their last modified timestamps.
nano commit_files_by_day.sh
Paste the following script into the file:
#!/bin/bash
current_day=""
files_to_commit=()
# Read the file_mod_times.txt file
while IFS=: read -r timestamp filename; do
day=$(date -d "@$timestamp" '+%Y-%m-%d')
if [[ "$day" != "$current_day" ]]; then
if [[ "${#files_to_commit[@]}" -gt 0 ]]; then
# Convert timestamp to a human-readable date
commit_date=$(date -d "$current_day 00:00:00" --rfc-2822)
# Add files to Git and commit them
git add "${files_to_commit[@]}"
GIT_AUTHOR_DATE="$commit_date" GIT_COMMITTER_DATE="$commit_date" git commit -m "Changes on $current_day"
# Reset files array
files_to_commit=()
fi
current_day="$day"
fi
files_to_commit+=("$filename")
done < file_mod_times.txt
# Last commit
if [[ "${#files_to_commit[@]}" -gt 0 ]]; then
commit_date=$(date -d "$current_day 00:00:00" --rfc-2822)
git add "${files_to_commit[@]}"
GIT_AUTHOR_DATE="$commit_date" GIT_COMMITTER_DATE="$commit_date" git commit -m "Changes on $current_day"
fi
chmod +x commit_files_by_day.sh
Execute the script to create the commits.
./commit_files_by_day.sh
When moving the old code around, the latest commit may have received an incorrect newer date. To ensure the most recent commit reflects the previous time:
-
Identify the Previous Commit Date Automatically
Run the following command to get the previous commit's timestamp:
previous_timestamp=$(git log -2 --format=%ci | tail -n 1)
-
Amend the Last Commit
Use the retrieved timestamp to amend the last commit automatically:
GIT_AUTHOR_DATE="$previous_timestamp" GIT_COMMITTER_DATE="$previous_timestamp" git commit --amend --no-edit --date "$previous_timestamp"
-
Force Push the Amended Commit
Run the command below to force push the changes to the remote repository:
git push -f origin main
-
Create a new repository on GitHub. Do not initialize it with a README, .gitignore, or license.
-
Back in your terminal, set the remote URL for your repository and push the changes.
git remote add origin https://github.com/yourusername/your-repository-name.git
git branch -M main
git push -u origin main
You can use git log
to review the commit history and ensure that the timestamps and commit messages are correct.
git log --stat
You now have a Git repository that faithfully reflects the historical changes to your project, with commits organized by the last modified timestamps of the files. This method helps to create a more meaningful commit history for projects where Git was not originally used.
I hope this guide helps you successfully migrate your old project to GitHub while preserving the commit timestamps. If you have any further questions or run into issues, feel free to ask!