0

I have been working on a series of exercises from a popular GitHub repository. But unfortunately I have committed my answers to the main branch of their repository. I should have cloned their repository first and then committed my changes to the cloned repository. Both of these are on GitHub.

Do I have a way to do this?: I don't want to loose my progress and redo the exercises, but more importantly I want to preserve the timestamps associated with my commits. I want to commit the answers to my clone (which is to be created) and then push the already committed commits to it.

All of guides/solutions to similar problems started with the clone first and then

Here is what I have done:

$ git clone repo_of_exercises.git

< Solved many exercises and I have a commit message for each exercise >
$ git add <solution to an exercise>
$ git commit -m "<Appropriate commit message>"
< Note that I have not pushed these changes, it's all on my computer >

What are the next steps that I should take to copy all of commits to a clone that I will create soon?

torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    Does this help? https://stackoverflow.com/a/5181968/686023 – L01NL Feb 10 '22 at 10:53
  • It kind of does, all my commits are there but the problem is that I cannot save the timestamps associated with each commit - every commit has the same timestamp, the time I pushed them to the repo – dope_centipede Feb 10 '22 at 10:58
  • 2
    @dope_centipede Unless you create new commits with something like "rebase" or "cherry-pick", the commit timestamp is part of the immutable content of the commit, and is not affected by pushing or pulling it between repos. – IMSoP Feb 10 '22 at 11:24

1 Answers1

0

Here is how I solved my problem:

I just had to add my repository as a remote.

git remote add main link_to_my_repo.git
< I could have removed the origin, which points to the original public repository
 - read the documentation linked below >

git push main
< to push all the commits to my private repository >

With these commands I will preserve the timestamp of my commits.

With my first attempt I had rebased the local repository to correct some commit messages and because of this all the commit messages were updated with a new timestamp.

I could have removed the original remote of the public repository but I chose not to thinking that it if I kept it then it may be recognised as a fork of the public repository.

But I was wrong and it does not recognise it as a fork.

git remote documentation

a tutorial that I followed