116

This is my first time forking a GitHub project, and I'm not too competent with CocoaPods either, so please bear with me.

Basically, I forked a project on GitHub using the following in my Podfile:

pod 'REActivityViewController', '~> 1.6.7', :git => 'https://github.com/<username>/REActivityViewController.git'

I then made some changes to the fork, and of course when I did a pod install to install another pod it reinstalled the original REActivityViewController and erased my changes.

I'm realizing I need to push my changes to my fork before another pod install, but how do I know it is the fork being installed, considering that this is a repo installed by CocoaPods? I looked in the REActivityViewController folder installed under the Pods folder and there aren't any git files.

Do I need to work on my fork outside of my project and then use CocoaPods to install the changes? That's too cumbersome of a workflow.

Or do I need to do something with submodules?

Michael Voccola
  • 1,827
  • 6
  • 20
  • 46
OdieO
  • 6,836
  • 7
  • 56
  • 88

3 Answers3

201

I will answer this question using an example. I have a fork of TTTAttributedLabel with some extra functionality I added here:

https://github.com/getaaron/TTTAttributedLabel

In order to use this in a Cocoapods project, I:

  1. Push my changes to my fork
  2. Configure my Podfile to get the changes & update

Once you've pushed your changes to your fork, get the SHA of your last commit. You can do this using git rev-parse origin/master | pbcopy or on the GitHub commits page for your project: Screenshot of copying a commit's SHA on GitHub

Then, you can specify the specific commit on your fork in your Podfile like this:

pod 'TTTAttributedLabel', :git => 'https://github.com/getaaron/TTTAttributedLabel.git', :commit => 'd358791c7f593d6ea7d6f8c2cac2cf8fae582bc1'

After that, pod update will update this particular commit from your fork. If you want, you can also make a podspec for your fork, but I find this approach simpler and I don't make changes frequently enough to justify a new workflow.

Do I need to work on my fork outside of my project and then use Cocoapods to install the changes? That's way to cumbersome of a workflow.

You can do it this way, but I usually:

  1. Edit the code inside my project and make sure it works
  2. Copy the changes over to my fork, by
    • exporting a patch, or
    • copying over the entire source code file
  3. Commit & push to GitHub
  4. Update the Podfile with the new SHA
  5. Run pod update.

Or do I need to do something with submodules?

No, you don't need to.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • 1
    Follow-up question: So updating the Podfile with the new SHA is really necessary? `pod install` won't just automatically clone the most up-to-date version with the latest commit? – OdieO Jan 06 '14 at 04:47
  • If you rename your project to something different and make your own podspec file, you can point it at your own repo and use `pod 'MyForkName', :head` instead. – Aaron Brager Jan 06 '14 at 04:51
  • 1
    `:head:` points to the newest commit, but you can't use `:git` and `:head` in the same line. – Aaron Brager Jan 06 '14 at 04:52
  • You can commit your new Podspec at https://github.com/CocoaPods/Specs and cocoapods will automatically find it. – Aaron Brager Jan 06 '14 at 04:53
  • Finally, if you want to keep it private, you can just register your custom Podspec for private use here: http://guides.cocoapods.org/making/private-cocoapods.html – Aaron Brager Jan 06 '14 at 04:54
  • 5
    This is a perfect fix for using a custom fork of a public project. In my case, I forked and modified a project, and had an open PR for the maintainer to merge those changes, but wanted to update my project's Podfile to use those changes immediately. This worked great! – cbowns Jun 15 '15 at 21:41
  • @AaronBrager - Can I add the same repo in my Dependency file ? – Arpan May 17 '16 at 08:26
  • Instead of using the commit name, you can use the branch name `pod 'Eureka', :git => 'https://github.com/slammer8/Eureka.git', :branch => 'Swift3'` – slammer Jul 09 '16 at 16:04
  • This works but I used `pod install` instead of `pod update` – Okhan Okbay Feb 12 '18 at 20:58
  • 2
    @AaronBrager it's showing "Unable to find a specification for '' " do you know how to resolve it? – Susim Samanta Mar 15 '18 at 01:50
  • 2
    @AaronBrager if the original branch is updated with some bug fixes after i forked the repository how do i pull those new changes from original repository to my forked repository ? – Mihir Mehta Dec 28 '18 at 06:44
  • Nice answer, but is it just me who thinks Submodules is better than Pods in terms of source code management? There are many situations where we make changes to the dependent project (source code) and we would want to push directly to the forked project, this can be easily facilitated in submodules. Pods is not friendly in this matter, isn't it? – Raj Pawan Gumdal Apr 16 '19 at 11:49
  • Submodules are simpler for this one thing, but generally Cocoapods / Carthage solve many problems that submodules don’t – Aaron Brager Apr 17 '19 at 21:43
  • The best thing instead of pointing to a specific commit is to create a new release in github and change the podspec with the latest version. In the podfile the you only have to point to your git repo without specifying any commit, it will always get the latest commit. – Matteo Gobbi Apr 26 '20 at 02:57
  • 1
    @MihirMehta, any new about "after i forked the repository how do i pull..."? – Frade Feb 24 '21 at 12:45
36

Another option is to have your project reference the pod directly and not via github. This way you don't have to keep committing your fork or copying/pasting code just to test your changes. You can work with two different Xcode projects simultaneously and commit separately into their respective projects.

pod 'AFNetworking', :path => '~/Documents/AFNetworking'

CocoaPods Documentation: http://guides.cocoapods.org/using/the-podfile.html#using-the-files-from-a-folder-local-to-the-machine

enter image description here

Oren
  • 5,055
  • 3
  • 34
  • 52
  • 1
    How does this solve the problem? The Pod will pull these sources and place them as Pods project in the source project. Any changes to these (now included through Pods) files are not tracked by Git of the '~/Documents/AFNetworking', isn't it? – Raj Pawan Gumdal Apr 16 '19 at 11:56
  • @RajPawanGumdal The local folder can still be checked into a local or remote repo, the Cocoapods aspect and the Git aspect are separate issues. – MandisaW Nov 04 '22 at 20:56
0

CocoaPods and GitHub fork

You have two variants to work with fork

  1. [Podfile specific branch]
  2. [Podspec specific branch]

The difference is in the first variant you must push changes into remote/origin branch, while the second variant allows you just commit changes in a local branch

yoAlex5
  • 29,217
  • 8
  • 193
  • 205