Open sourcing code into a separate git repository
source link: https://www.netmeister.org/blog/git-second-remote.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Open sourcing code into a separate git repository
March 19th, 2023
Note: the approach here will still push your private stuff into the public repository. I'll have to update this with whatever the right way is to avoid that, but first I have to make dinner (homemade pizza!).
Source: Renato Augusto Martins; CC BY-SA 4.0
Suppose you have a code repository that you want to open source. You go ahead and add all your license, contributor agreement, README, and any other files, but you also have a few changes that you know will remain internal. That is, you know that your open source'd code will not be the authoritative copy, but a fork of your closed copy that you will have to track separately.
Now git is supposed to help you with this, and you know you want a local branch as well as a second remote repository, but git is notoriously unintuitive at times, so here's the quick set up for this scenario:
We start out with a simple git repository with just a single remote and our main branch:
$ ls Makefile README.md doc private src test $ git branch * main $ git remote -v origin ssh://[email protected]:/~jschauma/whatever.git (fetch) origin ssh://[email protected]:/~jschauma/whatever.git (push) $
Now let's create a new branch for open sourcing this code. This will become the authoritative open source upstream, so we want to remove the private data and add our open source files:
$ git checkout -b opensource Switched to a new branch 'opensource' $ git rm -fr private/ rm 'private/file' $ $EDITOR LICENSE CONTRIBUTING $ git add LICENSE CONTRIBUTING $ git commit -m 'ready for open source' [opensource 611db4c] ready for open source 3 files changed, 2 insertions(+) create mode 100644 CONTRIBUTING create mode 100644 LICENSE delete mode 100644 private/file $
I said: Note: the approach here will still push your private stuff into the public repository. I'll have to update this with whatever the right way is to avoid that, but first I have to make dinner (homemade pizza!).
Next, we initialize the new remote repository. E.g., on GitHub, you might create a new (empty) repository. This will be the repository that we push our open source branch into. We then add the new repository as a second remote and push our branch there as the main branch:
$ git branch main * opensource $ git remote add github [email protected]:jschauma/second-remote-example.git $ git remote -v github [email protected]:jschauma/second-remote-example.git (fetch) github [email protected]:jschauma/second-remote-example.git (push) origin ssh://[email protected]:/~jschauma/whatever.git (fetch) origin ssh://[email protected]:/~jschauma/whatever.git (push) $ git push github opensource:main # "main" here is the remote "main" Enumerating objects: 25, done. Counting objects: 100% (25/25), done. Compressing objects: 100% (20/20), done. Writing objects: 100% (25/25), 9.32 KiB | 1.86 MiB/s, done. Total 25 (delta 3), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (3/3), done. To github.com:jschauma/second-remote-example.git * [new branch] opensource -> main $
And there you go, we're all set. Your main branch remains untouched, you can sync changes from there into your opensource branch or pull in changes from others from GitHub.
March 19th, 2023
This page mainly exists because I forget the order and syntax of the right git commands. :-)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK