Skip to content

Commit

Permalink
Improved git docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhvelplund authored Oct 21, 2024
1 parent 1bdb66d commit ef00576
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
47 changes: 43 additions & 4 deletions docs/Miscellaneous/Git.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,49 @@

<!-- toc -->

## Move files from one repository to another, preserving git history

_Based on Ayushya Jaiswal's excellent article on [Move files from one repository to another, preserving git
history](https://medium.com/@XanderGrzy/how-to-move-files-from-one-git-repo-to-another-preserving-history-2e81e3e3c8b7)._

In the example below, we want to extract a folder (`code/src/utils`) from a mono-repo (`repo1`) into its own repo (`repo2`),
preserving the git history.

```shell
git clone [email protected]:example/test.git repo1
git clone [email protected]:example/test.git repo2
cd repo2
git remote rm origin
git filter-branch --subdirectory-filter code/src/utils -- --all
git reset --hard
git gc --aggressive
git prune
git clean -xfd
```

Now only the filtered folder is left, and it's at the root of repo2. If it's going into another repo with existing
files, we can move the things to a sub-folder and commit them before proceeding, but in this case we want them in the
root.

Next, we push them to the new remote repo (which was created ahead of time and left empty):

```shell
git remote add origin [email protected]:example/test-extrusion.git
git branch -M main
git push -u origin main
```

Finally, we should clean up the old repo:

```shell
cd ../repo1
rm -rf code/src/utils
git add -A
git commit
```

Obviously, there might be more refactoring needed, but this is the basic idea.

## Summarize changes

```shell
Expand Down Expand Up @@ -33,10 +76,6 @@ UI: [github.com/mhvelplund/git-bash](https://github.com/mhvelplund/git-bash).

Credentials: [Storing Git Credentials with Git Credential Helper](https://techexpertise.medium.com/storing-git-credentials-with-git-credential-helper-33d22a6b5ce7)

## Move files from one repository to another, preserving git history

Excellent article [here](https://medium.com/@ayushya/move-directory-from-one-repository-to-another-preserving-git-history-d210fa049d4b).

## Move a feature branch from one product to another

If, by accident, one has created a feature ``foo`` from the wrong product
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ slay the same dragon twice.

## Notes

- (2024-10-21) [Git](./Miscellaneous/Git.md)
- (2024-10-15) [CLI tools](./DevOps/CLI.md)
- (2024-08-07) [Shell scripting](./DevOps/BashScripting.md)
- (2024-08-07) [Node.js & TypeScript](./Programming/Node.md)
Expand All @@ -16,7 +17,6 @@ slay the same dragon twice.
- (2024-01-19) [Docker](./DevOps/Docker.md)
- (2024-01-07) [Jupyter & Rust](./Programming/JupyterRust.md)
- (2024-01-05) [Misc. Notes](./Miscellaneous/MiscNotes.md)
- (2023-09-15) [Git](./Miscellaneous/Git.md)
- (2023-09-14) [VS Code](./Miscellaneous/VS%20Code.md)
- (2023-09-02) [Obsidian](./Miscellaneous/ObsidianNotes.md)
- (2023-04-06) [Rust](./Programming/Rust.md)
Expand Down

0 comments on commit ef00576

Please sign in to comment.