0

I have a folder on ~/Documents/Share that contains classes that I use a lot and that work together. I want to transform that folder in a git submodule and then import that on Xcode 5. My idea is to use that git on a lot of projects.

I am struggling with this for the last 3 hours. I have tried a dozen "easy" instructions on the web, none worked.

How do I do that? All I want is to create a local submodule to work on a local project.

Please do not give exoteric ectoplasmic explanations... explain me like I'm five years old (because I never used git before).

Yes, I am familiar with terminal.


when I try to do git push as suggested, I see this error:

Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 292 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
Duck
  • 34,902
  • 47
  • 248
  • 470

1 Answers1

5

I'm not sure if and how it would work with a local submodule, but we do something similar with our bitbucket account.

To transform your folder into a git repository, commit all files and push them onto a server:

cd yourdirectory
git init (1)
git add . (2)
git commit -m "this is my commit message" (3)
git remote add origin <adress-of-your-remote-git-repository> (4)
git push (5)
  1. Transforms your directory into a local git repository.
  2. Stages all files in the directory to your git staging.
  3. Commits your staged files.
  4. Adds a remote origin to your local repository.
  5. Pushes your local repository onto the remote reference.

You now have a remote repository with all your files. To use it as a submodule in any git enabled project:

cd yourprojectdirectory
git submodule add <adress-of-your-remote-git-repository> (1)
git submodule update --init --recursive (2)
  1. Adds the remote repository as a new git submodule.
  2. Initializes the submodule so it has actual content in it.

All thats left now is to open the folder of the added submodule in the finder and drag that folder into your Xcode project.

To update your files with some changes made in the local project, you would make your changes in the project and then you would do the following:

cd yourprojectdirectory/yoursubmoduledirectory
git add changedfile.m
git commit -m "your commit message"
git push

Of course you would then have to synch those changes to your other projects:

cd yourotherprojectdirectory/yoursubmoduledirectory
git pull
Florian
  • 501
  • 4
  • 7
  • OK, thanks for that explanation! As I understand this will make a copy of the git inside the project, but suppose I change something on one of these files inside the project, that have their equivalents on the original git. How do I push the changes to the original git? I am not seeing how. Commit will just update the project copy. Remember that I am not working remote. All my files are local, the git and the clones. – Duck Oct 20 '13 at 13:59
  • I cannot put formatted code in this comment, so I'll add it to my answer! – Florian Oct 20 '13 at 14:06
  • Almost there. When I do git push, I see an error... I have updated the question because I cannot put formatted text here. – Duck Oct 20 '13 at 14:14
  • I have updated my question with the error message. One question: do I really need to have a local copy inside the project? Isn't possible to work directly with the local repository? – Duck Oct 20 '13 at 14:17
  • Hm... I never got this error message, so I'm not sure what the solution for that problem would be... Regarding your question: As far as I know you need to have a local copy inside your project. That's just the way git submodules work. However, you could just put all your source files into a repository and link then with your project. This would be fine if you are working on your own, but if you have co-workers this leads to problems. E.g. everyone of you would have to have your source files in the same directory etc. Submodules save you from a lot of headaches then. – Florian Oct 20 '13 at 14:27
  • by *you could just put all your source files into a repository and link then with your project* you mean simply drag my original folder that I have transformed into a git into xcode project? (yes, I am working alone) – Duck Oct 20 '13 at 14:33
  • Yes, if you are working alone you could just save your source files into a repository and then just drag the source folder into your Xcode project. In fact you would not even need a git repository then. The repository would then just be for backup purposes (meaning everytime you change something you could commit your source files and would have the possibility to just jump back and forth between commits and would minimize the chance to lose your source files). – Florian Oct 20 '13 at 14:39
  • 1
    However I would still give git submodules a try, since they are really easy to work with once you have set them up and they are the preferred way to work. – Florian Oct 20 '13 at 14:41
  • OK, but I see a lot of procedures to follow to propagate the changes thru other projects using it... lot of steps mean more chances to forget something... THANKS! – Duck Oct 20 '13 at 14:42
  • Well, after having set up your submodule the only steps you would have would be to do a git pull if you want your project to have the newest source files or a git push if you changed something. The big advantage of working with a submodule is you can leave a specific commit in your project if you introduced breaking changes to your source files. If you just reference them all from a folder, you would need to update every project of yours after introducing such breaking changes. With git submodules you can defer this until you decide to update your project. – Florian Oct 20 '13 at 14:46
  • I don't get this: " If you just reference them all from a folder, you would need to update every project of yours after introducing such breaking changes." If I drag the git directly to N projects without cloning and from one of these projects I change the git, wouldn't every project be instantly updated? – Duck Oct 20 '13 at 14:52
  • Yes, and thats exactly the problem. Image you introduce a breaking change to one of your files (e.g. rename a method), you won't be able to build any of your projects which use that method. With git submodules however, you don't just have a file or folder referenced, but rather a specific commit in a repository. So all of your projects are still buildable, until you decide to pull the latest version of your repository with your source files. I would encourage you to read through that link to fully grasp the concept of submodules: http://git-scm.com/book/en/Git-Tools-Submodules – Florian Oct 20 '13 at 15:07