Skip to content

Instantly share code, notes, and snippets.

@MichaelPaulukonis
Created March 14, 2025 15:01
Show Gist options
  • Select an option

  • Save MichaelPaulukonis/ef1038d6ca7a0447e18765e238c9f576 to your computer and use it in GitHub Desktop.

Select an option

Save MichaelPaulukonis/ef1038d6ca7a0447e18765e238c9f576 to your computer and use it in GitHub Desktop.
Cross-repo replacement while preserving both histories and not doing a lot of manual stuff
# Navigate to your existing repository
cd /path/to/existing/repo

# Add the new repository as a remote
git remote add new-repo /path/to/new/repo

# Fetch the branch from the new repository
git fetch new-repo branch-name

# Create a new branch in your existing repository
git checkout -b new-branch

# Remove all tracked files while respecting .gitignore
git ls-files -z | xargs -0 git rm -r --cached

# Commit the removal of all files
git commit -m "Remove all old files"

# Merge the branch from the new repository
git merge new-repo/branch-name --allow-unrelated-histories

# Resolve any conflicts if they arise

# Commit the merge
git commit -m "Merged branch from new repository"

# Push the new branch to the existing repository's remote
git push origin new-branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment