blob: c6ae4acf1053642b56cd61ef26967ada44949522 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# Git
My personal cheat sheet of commands for various tools and workflows
- [Git](#git)
- [Split out subfolder into new repository](#split-out-subfolder-into-new-repository)
- [(Optional) Migrate original subdir to submodule](#optional-migrate-original-subdir-to-submodule)
## Split out subfolder into new repository
*Be sure you are inside the original repo*
```sh
cd <orignal_repository>/<subdir-to-split>
```
Set local variables for use
```sh
username="$(git config user.username)"
subdir="$(git rev-parse --show-prefix)"
newrepo="$(basename $subdir)"
oldrepo="$(git rev-parse --show-toplevel)"
cd $oldrepo
```
Create a new branch containing only the sub-directory using `git subtree`
```sh
git subtree split -P $subdir -b $newrepo
```
Create a temp git repo and pull in the newly created branch
```sh
cd $(mktemp -d)
git init && git pull $oldrepo $newrepo
```
Copy over the git artifacts from original repo's root directory
```sh
cp -rt ./ $oldrepo/.gitignore $oldrepo/.gitattributes $oldrepo/.vscode
```
Commit changes
```sh
git add -A && git commit -m "split out $newrepo into submodule"
```
Create a new remote repository using something like [gh](https://github.com/cli/cli)
```sh
gh repo create $username/$newrepo
```
(Optional) You can also just create the new remote repository manually and set the new local repository's remote with
```sh
git remote add origin https://github.com/$username/$newrepo
```
Push newly created repository to remote
```sh
git push -u origin master
```
### (Optional) Migrate original subdir to submodule
Switch back into the original repository
```sh
cd $oldrepo
```
Remove the `subdir` from git and the filesystem
```sh
git rm -rf $subdir
rm -rf $subdir
```
Add the newly created remote repository as a submodule at the `subdir`'s path
```sh
git submodule add git@github.com:$username/$newrepo $subdir
git submodule update --init --recursive
```
Commit the changes to the original repository and push to remote
```sh
git commit -m "split out $newrepo into submodule"
git push -u origin master
```
|