Steve
Steve

Reputation: 4097

Pushing Only Recent History to a New Git Repo

I have a legacy repository that has been corrupted through some misdirection of LFS and files that are no longer accessible. The result is that it requires access to files that no longer exist to push to a new repository or major surgery on the history that seems too difficult/risky that this time. I would like to attempt to create a new repository and push only recent history to avoid the old and bad commits. Say, all commits since a certain date or from a certain commit. Is this possible?

enter image description here

Upvotes: 4

Views: 230

Answers (2)

matt
matt

Reputation: 535201

Why not just delete everything before the first "good" commit?

One easy way:

git rebase -i --root

You are shown a list of all commits in reverse order from the start, e.g.

pick aa48e9c start
pick e10b625 middle
pick 6691860 finished up

Let's say we want to get rid of "start" and "middle" but keep "finished up". So just delete those two lines and save.

pick 6691860 finished up

An artificial empty first commit will be created, but the bad commits will be gone. There might be some conflicts to work out but it shouldn't be difficult.

Upvotes: 2

Shell Code
Shell Code

Reputation: 692

Perhaps you could try git rebase --onto <sha1> <sha2> <sha3>(see this link) functionality where

  • sha1: The new base
  • sha2: The parent of the first commit that will be copied (sha2 will not be copied itself)
  • sha3: The last, most recent commit which will be copied

So all in all something like this:

  1. Setup a new local repository named newRepo
mkdir newRepo
cd newRepo
git init
  1. Create an initial commit in newRepo just to initialize master branch
touch temp_file_for_initial_commit
git add .
git commit -m "Initial commit of new corruption-free repository"
  1. Get all the history from the oldRepo
git add origin "/path/to/oldRepo" # notice it could just be a path to the local repo
git fetch origin
  1. Use git rebase --onto to copy old range of valid commits to new repo
git rebase --onto master <sha of parent of first commit> <sha of latest commit>
  1. Make a branch for new history
git checkout -b latest-history-on-new-repo

  1. Clean up reference to old repo
git remote remove origin

Upvotes: 0

Related Questions