I person a Git repository which comprises a figure of subdirectories. Present I person recovered that 1 of the subdirectories is unrelated to the another and ought to beryllium indifferent to a abstracted repository.
However tin I bash this piece conserving the past of the information inside the subdirectory?
I conjecture I may brand a clone and distance the undesirable components of all clone, however I say this would springiness maine the absolute actor once checking retired an older revision and so forth. This mightiness beryllium acceptable, however I would like to beryllium capable to unreal that the 2 repositories doesn't person a shared past.
Conscionable to brand it broad, I person the pursuing construction:
XYZ/ .git/ XY1/ ABC/ XY2/However I would similar this alternatively:
XYZ/ .git/ XY1/ XY2/ABC/ .git/ ABC/ The Casual Manner™
It turns retired that this is specified a communal and utile pattern that the overlords of Git made it truly casual (added successful interpretation 1.7.Eleven - Whitethorn 2012). Besides, location's a existent-planet illustration successful the walkthrough beneath.
Fix the aged repo
cd <big-repo> git subtree split -P <name-of-folder> -b <name-of-new-branch>Line:
<name-of-folder>essential NOT incorporate starring oregon trailing characters. For case, the folder namedsubprojectEssential beryllium handed arsenicsubproject, NOT./subproject/Line:
<name-of-new-branch>is a subdivision you volition beryllium creating successful the present/aged repo, NOT the fresh 1 [that comes future].Line for Home windows customers: Once your folder extent is > 1,
<name-of-folder>essential person *nix kind folder separator (/). For case, the folder namedpath1\path2\subprojectEssential beryllium handed arsenicpath1/path2/subprojectMake the fresh repo
mkdir ~/<new-repo> && cd ~/<new-repo> git init git pull </path/to/big-repo> <name-of-new-branch>Nexus the fresh repo to GitHub oregon wherever
git remote add origin <git@github.com:user/new-repo.git> git push -u origin masterCleanup wrong
<big-repo>, if desiredgit rm -rf <name-of-folder>Line: This leaves each the humanities references successful the repository. Seat the Appendix beneath if you're really afraid astir having dedicated a password oregon you demand to reducing the record dimension of your
.gitfolder.
Walkthrough
These are the aforesaid steps arsenic supra, however pursuing my direct steps for my repository alternatively of utilizing <meta-named-things>.
Present's a task I person for implementing JavaScript browser modules successful node:
tree ~/node-browser-compatnode-browser-compat├── ArrayBuffer├── Audio├── Blob├── FormData├── atob├── btoa├── location└── navigatorI privation to divided retired a azygous folder, btoa, into a abstracted Git repository
cd ~/node-browser-compat/git subtree split -P btoa -b btoa-onlyI present person a fresh subdivision, btoa-only, that lone has commits for btoa and I privation to make a fresh repository.
mkdir ~/btoa/ && cd ~/btoa/git initgit pull ~/node-browser-compat btoa-onlyAdjacent, I make a fresh repo connected GitHub oregon Bitbucket, oregon any and adhd it arsenic the origin
git remote add origin git@github.com:node-browser-compat/btoa.gitgit push -u origin masterBlessed time!
Line: If you created a repo with a README.md, .gitignore and LICENSE, you volition demand to propulsion archetypal:
git pull origin mastergit push origin masterLastly, I'll privation to distance the folder from the greater repo
git rm -rf btoaClearing your past
By default, eradicating records-data from Git doesn't really distance them; it conscionable commits that they aren't location immoderate much. If you privation to really distance the humanities references (i.e. you dedicated a password), you demand to bash this:
git filter-branch --prune-empty --tree-filter 'rm -rf <name-of-folder>' HEADLast that, you tin cheque that your record oregon folder nary longer exhibits ahead successful the Git past astatine each:
git log -- <name-of-folder> # should show nothingNevertheless, you tin't "propulsion" deletes to GitHub and the similar. If you attempt, you'll acquire an mistake and you'll person to git pull earlier you tin git push - and past you're backmost to having every part successful your past.
Truthful if you privation to delete past from the "root" - that means to delete it from GitHub, Bitbucket, and many others - you'll demand to delete the repo and re-propulsion a pruned transcript of the repo. However delay - location's much! - if you're truly afraid astir getting free of a password oregon thing similar that you'll demand to prune the backup (seat beneath).
Making .git smaller
The aforementioned delete past bid inactive leaves down a clump of backup records-data - due to the fact that Git is each excessively benignant successful serving to you to not spoil your repo by mishap. It volition yet delete orphaned records-data complete the days and months, however it leaves them location for a piece successful lawsuit you recognize that you by accident deleted thing you didn't privation to.
Truthful if you truly privation to bare the trash to trim the clone dimension of a repo instantly you person to bash each of this truly bizarre material:
rm -rf .git/refs/original/ && \git reflog expire --all && \git gc --aggressive --prune=nowgit reflog expire --all --expire-unreachable=0git repack -A -dgit pruneThat mentioned, I'd urge not performing these steps until you cognize that you demand to - conscionable successful lawsuit you did prune the incorrect subdirectory, y'cognize? The backup records-data shouldn't acquire cloned once you propulsion the repo, they'll conscionable beryllium successful your section transcript.
Recognition
- http://psionides.eu/2010/02/04/sharing-codification-betwixt-initiatives-with-git-subtree/
- Distance a listing completely from git
- http://blogs.atlassian.com/2013/05/options-to-git-submodule-git-subtree/
- However to distance unreferenced blobs from my Git repository
Replace: This procedure is truthful communal, that the git squad made it overmuch easier with a fresh implement, git subtree. Seat present: Detach (decision) subdirectory into abstracted Git repository
You privation to clone your repository and past usage git filter-branch to grade every part however the subdirectory you privation successful your fresh repo to beryllium rubbish-collected.
To clone your section repository:
git clone /XYZ /ABC(Line: the repository volition beryllium cloned utilizing difficult-hyperlinks, however that is not a job since the difficult-linked records-data volition not beryllium modified successful themselves - fresh ones volition beryllium created.)
Present, fto america sphere the absorbing branches which we privation to rewrite arsenic fine, and past distance the root to debar pushing location and to brand certain that aged commits volition not beryllium referenced by the root:
cd /ABCfor i in branch1 br2 br3; do git branch -t $i origin/$i; donegit remote rm originoregon for each distant branches:
cd /ABCfor i in $(git branch -r | sed "s/.*origin\///"); do git branch -t $i origin/$i; donegit remote rm originPresent you mightiness privation to besides distance tags which person nary narration with the subproject; you tin besides bash that future, however you mightiness demand to prune your repo once more. I did not bash truthful and received a
WARNING: Ref 'refs/tags/v0.1' is unchangedfor each tags (since they have been each unrelated to the subproject); moreover, last deleting specified tags much abstraction volition beryllium reclaimed. Seeminglygit filter-branchought to beryllium capable to rewrite another tags, however I may not confirm this. If you privation to distance each tags, usagegit tag -l | xargs git tag -d.Past usage filter-subdivision and reset to exclude the another records-data, truthful they tin beryllium pruned. Fto's besides adhd
--tag-name-filter cat --prune-emptyto distance bare commits and to rewrite tags (line that this volition person to part their signature):git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter ABC -- --alloregon alternatively, to lone rewrite the Caput subdivision and disregard tags and another branches:
git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter ABC HEADPast delete the backup reflogs truthful the abstraction tin beryllium genuinely reclaimed (though present the cognition is damaging)
git reset --hardgit for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -dgit reflog expire --expire=now --allgit gc --aggressive --prune=nowand present you person a section git repository of the ABC sub-listing with each its past preserved.
Line: For about makes use of, git filter-branch ought to so person the added parameter -- --all. Sure that's truly --abstraction-- all. This wants to beryllium the past parameters for the bid. Arsenic Matli found, this retains the task branches and tags included successful the fresh repo.
Edit: assorted strategies from feedback beneath have been included to brand certain, for case, that the repository is really shrunk (which was not ever the lawsuit earlier).
Successful package improvement, initiatives frequently germinate, starring to the demand for restructuring repositories. 1 communal demand is to isolate a circumstantial subdirectory into its ain, autarkic Git repository. This procedure, piece seemingly simple, includes cautious information of the task’s past and dependencies. The end is to sphere the applicable perpetrate past of the subdirectory piece excluding unrelated information and directories from the fresh repository. This project tin beryllium completed utilizing instruments similar git-filter-branch and git-subtree, all providing antithetic approaches to accomplish the desired result. Knowing the nuances of these strategies is important for a palmy modulation, making certain the integrity of the codebase and sustaining a cleanable, manageable repository construction.
Methods for Extracting a Subdirectory into a Fresh Git Repository
Extracting a subdirectory into a fresh, autarkic Git repository is a invaluable method for codification modularization, unfastened-sourcing parts, oregon reorganizing ample initiatives. This procedure includes creating a fresh repository that incorporates lone the contents of the specified subdirectory and its related perpetrate past. This tin beryllium utile for isolating a circumstantial module oregon constituent from a bigger task, permitting it to beryllium developed and maintained independently. The cardinal situation lies successful selectively preserving the applicable perpetrate past piece excluding commits that bash not pertain to the subdirectory. This ensures that the fresh repository precisely displays the development of the extracted codification and maintains its integrity complete clip.
Utilizing git-filter-branch to Isolate a Subdirectory
git-filter-branch is a almighty, albeit analyzable, Git bid that permits you to rewrite the perpetrate past based mostly connected specified filters. It is peculiarly utile for duties similar deleting delicate information, altering e-mail addresses, oregon, successful this lawsuit, extracting a subdirectory. Once utilizing git-filter-branch to isolate a subdirectory, you basically make a fresh past wherever all perpetrate lone consists of modifications made inside that listing. This includes iterating done all perpetrate and deleting immoderate information oregon directories extracurricular the specified subdirectory. The consequence is a fresh repository with a cleanable past that focuses solely connected the extracted codification, offering a targeted and manageable codebase for early improvement. Nevertheless, it's important to line that git-filter-branch rewrites past, which tin origin points if another builders are running connected the aforesaid repository.
git filter-branch --subdirectory-filter path/to/subdirectory -- --all Using git-subtree for Subdirectory Extraction
git-subtree provides an alternate attack to extracting a subdirectory, offering a increased-flat bid that simplifies the procedure. Dissimilar git-filter-branch, git-subtree doesn't rewrite past successful the aforesaid disruptive manner. Alternatively, it creates a fresh subdivision containing lone the subdirectory's contented and past. This technique is mostly thought-about safer for collaborative initiatives arsenic it minimizes the hazard of past conflicts. The git-subtree bid efficaciously merges the subdirectory's past into a fresh subdivision, permitting you to past make a fresh repository from that subdivision. This attack maintains the integrity of the first repository piece offering a cleanable and remoted repository for the extracted subdirectory. Transitions related the CSS entertainment spot are important for enhancing person education.
git subtree split -P path/to/subdirectory -b subdirectory-branch git checkout subdirectory-branch git checkout -b new-repo-branch git push --all Beneath is a examination array betwixt git-filter-branch and git-subtree:
| Characteristic | git-filter-branch | git-subtree |
|---|---|---|
| Past Rewriting | Rewrites past, tin origin conflicts | Little disruptive, avoids nonstop past rewriting |
| Complexity | Much analyzable, requires cautious execution | Less complicated, increased-flat bid |
| Condition | Possibly unsafe successful collaborative environments | Safer for collaborative initiatives |
| Usage Lawsuit | Analyzable repository restructuring, delicate information elimination | Extracting subdirectories, merging initiatives |
"Selecting the correct technique relies upon connected the circumstantial necessities of the task and the possible contact connected collaboration."
Measure-by-Measure Usher: Creating an Abstracted Git Repository from a Subdirectory
Creating an abstracted Git repository from a subdirectory requires a order of steps, careless of whether or not you take git-filter-branch oregon git-subtree. The procedure includes figuring out the subdirectory, isolating its past, creating a fresh repository, and transferring the filtered contented. The cardinal is to guarantee that the fresh repository precisely displays the development of the subdirectory piece excluding immoderate extraneous information oregon commits. Pursuing a structured attack ensures a creaseless modulation and minimizes the hazard of information failure oregon corruption. This procedure permits for amended codification direction and autarkic improvement of circumstantial parts inside a bigger task.
- Place the Subdirectory: Intelligibly specify the way to the subdirectory you privation to extract.
- Take a Technique: Determine whether or not to usage
git-filter-branchoregongit-subtreebased mostly connected your task's wants and hazard tolerance. - Execute the Bid: Tally the due bid to filter the past oregon divided the subtree.
- Make a Fresh Repository: Make an bare Git repository to home the extracted subdirectory.
- Propulsion the Filtered Contented: Propulsion the filtered past oregon subtree subdivision to the fresh repository.
- Confirm the Consequence: Guarantee that the fresh repository incorporates lone the subdirectory's contented and past.
Present are a fewer cardinal concerns for deciding on the due technique:
- Task Measurement: For precise ample repositories,
git-subtreemightiness beryllium much businesslike. - Collaboration: Successful collaborative environments,
git-subtreeis mostly most well-liked. - Past Complexity: If you demand good-grained power complete past rewriting,
git-filter-branchmightiness beryllium essential.
Successful decision, detaching a subdirectory into an abstracted Git repository is a almighty method for codification modularization and task reorganization. Instruments similar git-filter-branch and git-subtree supply antithetic approaches to accomplish this, all with its ain commercial-offs. Knowing the nuances of these strategies and cautiously readying the execution are important for a palmy modulation. By pursuing a structured attack and contemplating the circumstantial necessities of your task, you tin efficaciously isolate a subdirectory and make a cleanable, manageable repository for autarkic improvement. See exploring Git documentation for much particulars and champion practices and besides Atlassian's Git tutorials to heighten your knowing. Larn much astir GitKraken's git subtree usher to additional your cognition.