Skip to content

Instantly share code, notes, and snippets.

@peteretelej
Last active April 30, 2025 21:33
Show Gist options
  • Save peteretelej/dba6428dfbc3e4d0d89feb8e418e4a7a to your computer and use it in GitHub Desktop.
Save peteretelej/dba6428dfbc3e4d0d89feb8e418e4a7a to your computer and use it in GitHub Desktop.
Add old code project to github with correct timestamps

How to add an Old Project to GitHub While Retaining Timestamps for old changes

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)

Steps

1. Initialize the Repository

First, navigate to your project’s directory and initialize a new Git repository.

cd /path/to/your/project
git init

2. Gather File Modification Timestamps

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

3. Create a Script to Commit Files Based on Modification Time

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

4. Make the Script Executable

chmod +x commit_files_by_day.sh

5. Run the Script

Execute the script to create the commits.

./commit_files_by_day.sh

6. Optional Step: Automatically Update the Most Recent Commit Timestamp

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:

  1. 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)
  2. 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"
  3. Force Push the Amended Commit

    Run the command below to force push the changes to the remote repository:

    git push -f origin main

7. Push the Repository to GitHub

  1. Create a new repository on GitHub. Do not initialize it with a README, .gitignore, or license.

  2. 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

8. Review Commit History

You can use git log to review the commit history and ensure that the timestamps and commit messages are correct.

git log --stat

Conclusion

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment