Tuesday, November 26, 2024

How to Copy Git Repository Without History

There are several methods to do this using git clone, git push or using git archive. But I personally prefer the one using git clone.

Objective is to copy repo 1 which is source repo to a new Repo which is NewRemote repo with out commit history.

Precautions before you proceed with this:

  1. Ensure you have write access to the repository.
  2. Backup any important local changes before proceeding.
  3. This will permanently remove the old commit history.
  4. Collaborators will need to re-clone the repository.

Here are step by step git examples for this specific repo

# 1. Clone the source repository
git clone https://github.com/inagasai/SourceRepo.App.git

# 2. Enter the cloned repository directory
cd vGlence.App

# 3. Verify current branches
git branch -a

# 4. Checkout master branch
git checkout master

# 5. Create a new branch without history
git checkout --orphan clean-main

# 6. Add all files to the new branch
git add .

# 7. Commit the files with a new initial commit
git commit -m "Initial commit - reset repository history"

# 8. Delete the old main branch (if it exists)
git branch -D main 2>/dev/null

# 9. Rename current branch to main
git branch -m main

# 10. Remove the original remote
git remote remove origin

# 11. Add the original repository as a new remote
git remote add origin https://github.com/inagasai/NewRemote.App.git

# 12. Force push to overwrite the remote repository
git push -f origin main
  

Detailed Breakdown of the outcome:

  1. This process creates a new branch with no commit history.
  2. It adds all existing files to a new initial commit.
  3. Force pushes to overwrite the remote repository.
  4. Removes all previous commit history.

Hope this helps.

No comments:

Post a Comment