Skip to content

Instantly share code, notes, and snippets.

@samredai
Created January 9, 2020 00:43
Show Gist options
  • Save samredai/adecd53effc5aa87e59a93f2a1c41d35 to your computer and use it in GitHub Desktop.
Save samredai/adecd53effc5aa87e59a93f2a1c41d35 to your computer and use it in GitHub Desktop.
Git: Copy One File From One Repo To Another Repo While Preserving Commit History [Alternative Solution For Large Repositories]
# For this example, assume REPO_A has one file that you want to move into REPO_B along with it's commit history
# Clone REPO_A
git clone REPO_A
# Extract only the commit hashes for your target file and place them in a text file
# See this gist for more info on this line: https://gist.github.com/samsetegne/7768efe9394714c7fb5085de14f03516
git log -p path/to/target/file/filename.py | grep -n "^commit" | grep -Eo "[^ ]+$" > ~/commit_hashes.txt
# Clone REPO_B
git clone REPO_B
# Fetch REPO_A into the clone of REPO_B
git fetch REPO_A
# Loop through the commit hashes (in reverse to start with the earliest commit first)
# Do a cherry pick -> add -> commit sequence for each commit hash
tac ~/commit_hashes.txt | while read line; do git cherry-pick $line && git add . && git commit -m "Cherries"; done
# Push changes to REPO_B
git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment