Skip to content

Instantly share code, notes, and snippets.

@eeowaa
Last active November 3, 2021 21:51
Show Gist options
  • Save eeowaa/6dbb22d9bc27108355912f496a1ba36a to your computer and use it in GitHub Desktop.
Save eeowaa/6dbb22d9bc27108355912f496a1ba36a to your computer and use it in GitHub Desktop.
Replace a branch head with an imported tree from another git repo
# Checkout a new feature branch off of main
git checkout -b feature/import-tree main
# Clear the worktree, but keep the index
cd "`git rev-parse --show-toplevel`"
git rm -rf .
git reset
# Replace the contents of the worktree with that of another git repo's HEAD
git -C ../other-repo archive --format=tar HEAD | tar xf -
# Update the index with added, updated, and deleted content
git add -A
# Commit your sweeping changes that you want to override the main branch with
git commit -m 'Import tree (<hash>) from another repo (<url>)'
# Start a merge operation from feature/import-tree into main
# - Do not update the index or worktree (-s ours)
# - Stop short of creating a new commit (--no-commit)
git checkout main
git merge -s ours --no-commit feature/import-tree
# Update the index and worktree with the tree of feature/import-tree
git read-tree -um HEAD feature/import-tree
# Complete the merge operation to create a merge commit
git commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment