Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KumarManoj-S/64fd49eb27c644d3261cf51699ea788e to your computer and use it in GitHub Desktop.
Save KumarManoj-S/64fd49eb27c644d3261cf51699ea788e to your computer and use it in GitHub Desktop.
Move one git repository into another as a subdirectory without losing the commit histories.
# Let's say we have two repositories repo_A and repo_B.
# And following are the directory structure of both repositories,
# repo_A
# | - backend
# | - index.js
# repo_B
# | - components
# | - js
# | - index.js
# Now the requirement is to move repo_B into repo_A as a subdirectory without losing the commit histories.
# First, clone both the repos if you don't have it already in your local.
git clone repo_A
git clone repo_B
# Go to repo_B and create a subdirectory as you want it in repo_A (let's call it frontend).
cd repo_B
mkdir frontend
# Move all the files and folders into the subdirectory created.
git mv components frontend/
git mv js frontend/
git mv index.js frontend/
# Commit the changes made.
git commit -m "Move all the files and directories into subdirectory"
# Now, the directory structure of repo_B looks as follows,
# repo_B
# | - frontend
# | - components
# | - js
# | - index.js
# Now, go to repo_A and add a new remote to repo_B
cd ../repo_A
git remote add repo_B ../repo_B
git fetch repo_B
# Check if the new remote is added properly using the following command,
git remote -v
# Merge the remote created.
git merge repo_B/master --allow-unrelated-histories
# Now, you can remove the remote created.
git remote rm repo_B
# After the merge, the directory structure of repo_A will look like,
# repo_A
# | - backend
# | - frontend
# | - components
# | - js
# | - index.js
# | - index.js
# Thanks :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment