Skip to content

Instantly share code, notes, and snippets.

@izharaazmi
Created November 27, 2023 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izharaazmi/513a29c903d172cb0e35f3e4533cfb69 to your computer and use it in GitHub Desktop.
Save izharaazmi/513a29c903d172cb0e35f3e4533cfb69 to your computer and use it in GitHub Desktop.
Extract a branch from a repo into a separate Repo when the branch is unrelated to the original repo with zero common files. This preservers original author name and commit date and removes any conflicting files completely.
cd /Users/izhar/Sites/my-source-repo || exit
echo "Reset current branch to prevent checkout errors"
git reset --hard
echo "Checkout the branch to extract, assumed 'dev'"
git checkout dev
echo "Delete destination branch if exists, assumed 'dex'"
git branch -D dex
echo "Recreate an orphan branch for destination"
git checkout --orphan dex
echo "Reset destination branch to avoid any leftover untracked or ignored files"
git reset --hard
echo "Make an empty commit as git requires a parent commit for cherry pick"
GIT_AUTHOR_DATE="2023-05-23 00:00:00" GIT_COMMITTER_DATE="2023-05-23 00:00:00" git commit --allow-empty -m "Initial commit"
echo "Start picking commits"
# Change the commit ID (bf7242c23) below from where the cherry picking should start
git rev-list --reverse bf7242c23^..dev | while read -r commit; do
echo "---------------------------------------------------------------------------------------------------"
commit_date=$(git log -1 --pretty=format:%ci "$commit")
commit_message=$(git log -1 --pretty=%B "$commit")
author_name=$(git log -1 --pretty=format:%an "$commit")
author_email=$(git log -1 --pretty=format:%ae "$commit")
echo "Picking $commit $commit_message"
echo "Date: $commit_date"
echo "Author: $author_name <$author_email>"
GIT_AUTHOR_DATE="$commit_date" GIT_COMMITTER_DATE="$commit_date" git cherry-pick "$commit" || {
git diff --name-only --diff-filter=U | xargs git rm
git cherry-pick --continue || git cherry-pick --skip || exit
}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment