Skip to content

Instantly share code, notes, and snippets.

@yaud
Created December 6, 2022 23:00
Show Gist options
  • Save yaud/779b1776d62e9c9b0625ffd56c65a31d to your computer and use it in GitHub Desktop.
Save yaud/779b1776d62e9c9b0625ffd56c65a31d to your computer and use it in GitHub Desktop.
git tips

Let's say there is repo-source that has sub-directory z under x/y which we want to move to repo-target:

  • go to source repo - cd repo-source
  • extract files from z into a new branch z-only - git subtree split -P x/y/z -b z-only

Now if we checkout to z-only branch all files from x/y/z will be in the root directory.

In order to preserve original directory structure move directories/files into x/y/z manually:

  • re-create x/y/z directory - mkdir -P x/y/z (mkdir x\y\z on Windows)
  • move files into it - git mv <file-to-move> x/y/z
  • remove unneeded files - git rm <file-to-remove>
  • add files to the index - git add .
  • commit changes - git commit -m "Prepare files for moving into repo-target"

Now we need to pull our files from branch z-only in repo-source into repo-target:

  • go to target repo - cd repo-target
  • add repo-source as a remote repo for repo-target - git remote add repo-source <path-to-repo-source>
  • verify it was added - git remote -v
  • checkout to a new branch adding-z-files - git checkout -b adding-z-files
  • pull files from z-only branch in repo-source - git pull repo-source z-only --allow-unrelated-histories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment