in Education by
I have a Git repository which contains a number of subdirectories. Now I have found that one of the subdirectories is unrelated to the other and should be detached to a separate repository. How can I do this while keeping the history of the files within the subdirectory? I guess I could make a clone and remove the unwanted parts of each clone, but I suppose this would give me the complete tree when checking out an older revision etc. This might be acceptable, but I would prefer to be able to pretend that the two repositories doesn't have a shared history. Just to make it clear, I have the following structure: XYZ/ .git/ XY1/ ABC/ XY2/ But I would like this instead: XYZ/ .git/ XY1/ XY2/ ABC/ .git/ ABC/ JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The Easy Way™ It turns out that this is such a common and useful practice that the overlords of Git made it really easy, but you have to have a newer version of Git (>= 1.7.11 May 2012). See the appendix for how to install the latest Git. Also, there's a real-world example in the walkthrough below. Prepare the old repo cd git subtree split -P -b Note: must NOT contain leading or trailing characters. For instance, the folder named subproject MUST be passed as subproject, NOT ./subproject/ Note for Windows users: When your folder depth is > 1, must have *nix style folder separator (/). For instance, the folder named path1\path2\subproject MUST be passed as path1/path2/subproject Create the new repo mkdir ~/ && cd ~/ git init git pull Link the new repo to GitHub or wherever git remote add origin git push -u origin master Cleanup inside , if desired git rm -rf Note: This leaves all the historical references in the repository. See the Appendix below if you're actually concerned about having committed a password or you need to decreasing the file size of your .git folder. Walkthrough These are the same steps as above, but following my exact steps for my repository instead of using . Here's a project I have for implementing JavaScript browser modules in node: tree ~/node-browser-compat node-browser-compat ├── ArrayBuffer ├── Audio ├── Blob ├── FormData ├── atob ├── btoa ├── location └── navigator I want to split out a single folder, btoa, into a separate Git repository cd ~/node-browser-compat/ git subtree split -P btoa -b btoa-only I now have a new branch, btoa-only, that only has commits for btoa and I want to create a new repository. mkdir ~/btoa/ && cd ~/btoa/ git init git pull ~/node-browser-compat btoa-only Next, I create a new repo on GitHub or Bitbucket, or whatever and add it as the origin git remote add origin [email protected]:node-browser-compat/btoa.git git push -u origin master Happy day! Note: If you created a repo with a README.md, .gitignore and LICENSE, you will need to pull first: git pull origin master git push origin master Lastly, I'll want to remove the folder from the bigger repo git rm -rf btoa Appendix Latest Git on macOS To get the latest version of Git using Homebrew: brew install git Latest Git on Ubuntu sudo apt-get update sudo apt-get install git git --version If that doesn't work (you have a very old version of Ubuntu), try sudo add-apt-repository ppa:git-core/ppa sudo apt-get update sudo apt-get install git If that still doesn't work, try sudo chmod +x /usr/share/doc/git/contrib/subtree/git-subtree.sh sudo ln -s \ /usr/share/doc/git/contrib/subtree/git-subtree.sh \ /usr/lib/git-core/git-subtree Thanks to rui.araujo from the comments. Clearing your history By default removing files from Git doesn't actually remove them, it just commits that they aren't there anymore. If you want to actually remove the historical references (i.e. you committed a password), you need to do this: git filter-branch --prune-empty --tree-filter 'rm -rf ' HEAD After that, you can check that your file or folder no longer shows up in the Git history at all git log -- # should show nothing However, you can't "push" deletes to GitHub and the like. If you try, you'll get an error and you'll have to git pull before you can git push - and then you're back to having everything in your history. So if you want to delete history from the "origin" - meaning to delete it from GitHub, Bitbucket, etc - you'll need to delete the repo and re-push a pruned copy of the repo. But wait - there's more! - if you're really concerned about getting rid of a password or something like that you'll need to prune the backup (see below). Making .git smaller The aforementioned delete history command still leaves behind a bunch of backup files - because Git is all too kind in helping you to not ruin your repo by accident. It will eventually delete orphaned files over the days and months, but it leaves them there for a while in case you realize that you accidentally deleted something you didn't want to. So if you really want to empty the trash to reduce the clone size of a repo immediately you have to do all of this really weird stuff: rm -rf .git/refs/original/ && \ git reflog expire --all && \ git gc --aggressive --prune=now git reflog expire --all --expire-unreachable=0 git repack -A -d git prune That said, I'd recommend not performing these steps unless you know that you need to - just in case you did prune the wrong subdirectory, y'know? The backup files shouldn't get cloned when you push the repo, they'll just be in your local copy. Credit http://psionides.eu/2010/02/04/sharing-code-between-projects-with-git-subtree/ Remove a directory permanently from git http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/ How to remove unreferenced blobs from my git repo

Related questions

0 votes
    Why there is a need to move the existing directory into a new repository? How this can be done in SVN?...
asked Feb 18, 2021 in Technology by JackTerrance
0 votes
    How can we clone a Git repository into a specific folder?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    What is the Git command to move branch pointer to different commit without checkout? A. Git cherrypick B. Git update-ref C. Git reset head...
asked Dec 24, 2022 in Technology by JackTerrance
0 votes
    I started some work on a new feature and after coding for a bit, I decided this feature should be on its ... on the new feature. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    Move the most recent commit(s) to a new branch with Git?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    We have a few databases in Pricing Tier: Basic, S0... like below picture: These databases were created before a ... the Azure portal. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Upgraded Jenkins to 2.332.1 and a few plugins to try to stay up to date. Now getting the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    Microsoft now has support for Git repositories on their Team Foundation Service. I have an account on Team ... to TFService. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some ... my original project checkout. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I pulled a project from GitHub a few days ago. I've since discovered that there are several forks on GitHub, ... those forks I pulled? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    we are using git in Team Foundation Service, and we are trying to find a way to delete a remote ... /DefaultCollection/_git/Xxxxxx' Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    How do I revert from my current state to a snapshot made on a certain commit? If I do git log, then I get ... , i.e. commit 0d1d7fc? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    On adding a subproject using git submodule add, how many files will be added in the main repository? (1)4 (2)1 (3)2 (4)3...
asked Jun 1, 2021 in Technology by JackTerrance
0 votes
    How to find and restore a deleted file in a Git repository?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    Explain How to change the URI (URL) for a remote Git repository?...
asked Jan 7, 2021 in Technology by JackTerrance
...