Forking is the one thing I have never used in git. Yesterday I came to know how can we use it.
Why we use a fork?
Open source is a great thing, someone writes a library, so that others can use it, others can add up to it. Let's assume you are using an open-source library in your project, the library has everything you need except that the one thing you need for your project. That's where forking comes to help. Basically forking means you are creating a copy of the open-source library, making appropriate changes, committing back or submitting a pull request to the author of the open-source library.
How to fork an open-source library
Click on the Fork button on the right top corner of open-source GitHub repository
Then clone the forked library inside your local directory
git clone "forked lib URL"
Make appropriate changes to the library and commit it
git add .
git commit -m “commit message”
git push origin master
Now all the changes you want to make in the open-source library are there, the only thing remaining is to use this forked library as a dependency in your react native project.
How to use GitHub repository as an npm dependency in a react-native?
We make changes in open source libraries because we want the library to behave different for our project. Now consider a scenario
You went to node_modules/open_source_library
You made the changes
You are done here but in the future, there will be a case where we need to delete node_modules in order to resolve Android Studio’s linking with react-native libraries.
Now you will run npm install in order to install the open-source library. All your changes now has override by the original library. This means you need to make those changes again.
That’s why we fork the library and make changes so that we can use it as an npm dependency. Only question here is "How can we so that?”
It's very simple, You just need to run following command.
- You should be in your project directory
- npm install --save git+https://git@your fork repository URL without https://.git
You will notice that inside package.json in the dependency section, Instead of the open-source library version you will see your fork library.
That’s it. I hope this blog will help you in any way possible.
If you want to read more Do visit my website. Cheers.
Top comments (9)
You could also use it from your local directory and install it as
npm install --save path/to/dependency
true. I shared the way so people working with you on project can also make change in fork lib.
👍🏻
If you find any error or anything which is not correct please write here, I'm happy to talk about it.
bro can u help me with yarn example.....it asking version
it asking version ? Didn't get you.
Could you please give an example of step 2. npm install --save git+git@your fork repository URL without https://.git
Looks like everyone can just misguide its so easy while the truth is that most libraries will not work this way .. Am I wrong ?
Seems like it. I have used many open source project as npm package by forking it for my projects.