Thank to @jthill from this question: Convert or copy a folder from a git branch with all its history to be a new git branch
I used this Git commands to slice a folder from a GitHub Source Branch into another branch and maintain the commit history of the folder in the Source Branch:
git clone <repo-line>
git switch Source-Branch
a=$( git commit-tree -p heads/Source-Branch -m "Slice folder from a branch." heads/Source-Branch:Source-Folder )
git update-ref refs/heads/New-Branch $a
git status
git switch New-Branch
git status
git push -u origin New-Branch
How to use the same method, but this time I want to slice a folder from a GitHub branch to another folder in the same branch and maintain the commit history?
Thank to @jthill from this question: Convert or copy a folder from a git branch with all its history to be a new git branch
I used this Git commands to slice a folder from a GitHub Source Branch into another branch and maintain the commit history of the folder in the Source Branch:
git clone <repo-line>
git switch Source-Branch
a=$( git commit-tree -p heads/Source-Branch -m "Slice folder from a branch." heads/Source-Branch:Source-Folder )
git update-ref refs/heads/New-Branch $a
git status
git switch New-Branch
git status
git push -u origin New-Branch
How to use the same method, but this time I want to slice a folder from a GitHub branch to another folder in the same branch and maintain the commit history?
Share Improve this question asked Feb 2 at 1:32 tarekahftarekahf 1,0025 gold badges25 silver badges60 bronze badges1 Answer
Reset to default 0I found how to copy a folder from a branch to the same branch in a new folder and maintain the commit history:
git clone <url>
git switch source-branch
a=$(git commit-tree -p heads/source-branch -m "Slice folder to prepare for copy with commit history" heads/source-branch:source-folder)
git update-ref refs/heads/temp-branch $a
git branch -vv
mkdir new-folder
touch new-folder/readme.md
git add .
commit -m "Commit new folder to prep for copy."
git merge --allow-unrelated-histories --no-commit -Xsubtree=new-folder temp-branch
git status
git add .
git commit -m "Commit a copy of source-folder with commit history."
git log --oneline
git push origin
With slight modification of the above script, you can easily do the same, and instead, you can copy to a new branch and new folder instead of the same branch.