Skip to content

Instantly share code, notes, and snippets.

@dietmar
Created January 8, 2025 11:15
Show Gist options
  • Save dietmar/137369f776e83e7d23308f9bbbe5a8df to your computer and use it in GitHub Desktop.
Save dietmar/137369f776e83e7d23308f9bbbe5a8df to your computer and use it in GitHub Desktop.
Split a file into two preserving Git line history

Also see https://devblogs.microsoft.com/oldnewthing/20190916-00/?p=102892

# create new branch, rename the file, commit
git checkout -b split1
git mv original_file.txt part1_file.txt
git commit -m "rename original_file"
# remove some lines from part1_file.txt
git add part1_file.txt
git commit -m "remove lines from part1_file.txt
# go back to main branch
git checkout main
# create new branch, rename the file, commit
git checkout -b split2
git mv original_file.txt part2_file.txt
git commit -m "rename original_file"
# remove some lines from part2_file.txt
git add part2_file.txt
git commit -m "remove lines from part2_file.txt
# go back to main branch
git checkout main
# now we can octopus merge the two branches into main
git merge split1 split2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment