1

Sorry for such an easy question but how do I make a git repository I've added locally viewable in my master?

Details:

  1. the repo, in this case "stripe-php", is actually inside another repo, my application.
  2. visualizing the problem: the "stripe-php" hidden repo is colored gray-black instead of light-blue on github's website.

So this is hidden:

hidden repo

this is viewable:

viewable repo

tim peterson
  • 23,653
  • 59
  • 177
  • 299
  • 2
    Do you have a .gitmodules file in your repo? It looks like `stripe-php` is a submodule (http://stackoverflow.com/q/16301338/6309) not yet initialized. – VonC Sep 22 '13 at 20:54
  • @VonC was just reading about submodules. I know absolutely nothing about this. [Some argue against using them](http://codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/). Would you mind offering your view? – tim peterson Sep 22 '13 at 21:16
  • @VonC ok added as submodule but now I think its only a link to the repo and not the repo itself. See this picture http://imgur.com/5NLxTqT – tim peterson Sep 22 '13 at 23:00
  • 1
    It is indeed a link. I detail that notion in my answer below. – VonC Sep 23 '13 at 05:44

1 Answers1

1

Submodule is a good way to reference a fixed point in another repo history.
See "True nature of submodules".

Adding a submodule isn't enough, you must initialize it and update it:

git submodule update --init

You can also declare a submodule in order to follow a certain branch of ots upstream repo.
See "git submodule tracking latest".

If you have already declared a submodule without taking advantage of that option, see "How to make an existing submodule track a branch".


If you pull from GitHub, a simple git submodule update --init on your server in your live repo is enough to update your submodules.

Actually, the full command would be:

git submodule update --init --recursive --force

If you push directly to your server, to see a submodule updated in a live server, you need to have:

  • a bare repo (you can clone, on your server, your current repo which represents your live files, but which doesn't yet display the submodule content, with the --bare option, and push to that bare repo from your client)

  • a post-receive hook similar to what I describe in "Git submodule on remote bare".

That would be:

cd /path/to/your/bare/repo.git
$ cat > hooks/post-receive

#!/bin/sh
GIT_DIR=/path/to/live/repo/.git
GIT_WORK_TREE=/path/to/live/repo
cd /path/to/live/repo
git pull /path/to/your/bare/repo.git
git submodule update

$ chmod +x hooks/post-receive
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Can submodules be actual directories and not just links? Even after `init` and `update` the subdomain files don't exist in the remote repo where I've `pull`ed the main repo. – tim peterson Sep 23 '13 at 11:18
  • @timpeterson they are a directory, but represented in git index as a special entry (http://stackoverflow.com/a/2227598/6309). You won't see the files in a submodule as represented in GitHub in your remote repo. But `git submodule update --init` would fill that same submodule folder in your local clone. – VonC Sep 23 '13 at 11:21
  • @timpeterson See for instance my own project at https://github.com/VonC/compileEverything/tree/master/gitolite. It does include gitolite, but you have to click on the '2f48a3e' link to access gitolite repo content. – VonC Sep 23 '13 at 11:22
  • -@VonC thanks for your help! So maybe submodules are not what I need. I need the files b/c my remote repo is my live website and without the files the website doesn't function. Thoughts on what might be more appropriate? – tim peterson Sep 23 '13 at 11:26
  • @timpeterson yes that can work too: you need to push to a bare repo (that includes the special entry of your submodule), and have a post-receive hook which will: a/ cd to your live repo, git pull what you have received in the bare repo into the live repo, b/ git submodule update. See for instance http://stackoverflow.com/a/11178531/6309. – VonC Sep 23 '13 at 11:28
  • -@VonC Thanks can you outline that code? So on my live site server I need to create a new repo or can I make my existing one "bare" and then do the `git checkout` and `git submodule update --init`? – tim peterson Sep 23 '13 at 11:32
  • Can the live and the bare repo be the same directory, `/var/www/` ? – tim peterson Sep 23 '13 at 11:49
  • @timpeterson sure: yu can setup your bare repo wherever you want. As long as the bare repo and the live repo are in two separate folders, and as long as your bare repo is in a folder ending with (naming convention) a `.git` extension: `myBareRepo.git/`, you are ok. – VonC Sep 23 '13 at 11:51
  • hmm, maybe it is simpler to delete my existing live repo and just create a new bare repo? One repo sounds better than two, right? – tim peterson Sep 23 '13 at 12:08
  • @timpeterson no, you need two: to create the bare repo, simply `git clone --bare /path/to/live/repo` – VonC Sep 23 '13 at 12:10
  • -@VonC Ok sorry for the delay, I did `git clone --bare ` and executed `hooks/post-receive` but this gave me `Already up-to-date.`. So nothing changed in my live repo. This has to be a trivial problem, right? – tim peterson Sep 23 '13 at 13:30
  • @timpeterson the `hooks/post-receive` is supposed to be executed automatically every time you are pushing from your client machine to your bare repo on the server. – VonC Sep 23 '13 at 13:44
  • Ahh, i see. I've just been doing `git pull origin master` in my live repo, so I need to instead be issuing that command from my bare repo `/var/www/www.git`? – tim peterson Sep 23 '13 at 13:47
  • @timpeterson as you see in the post-receive hook, the `git pull` is indeed done from the live repo: it pulls whatever has been pushed to the bare repo. But if you don't push *first* to the bare repo, a `git pull` from the live repo would return "`Already up-to-date`". – VonC Sep 23 '13 at 13:49
  • @timpeterson note also that the script doesn't use '`origin`' as the name of the remote, but specify directly its path: it doesn't assume what your remotes are referring to. – VonC Sep 23 '13 at 13:50
  • -@VonC Ok, I guess I need to understand what the `push` command looks like. Is this correct: `cd /var/www` -> `git push /var/www /var/www/www.git` ? `/var/www` is my live repo, `/var/www/www.git` is my bare repo. – tim peterson Sep 23 '13 at 13:57
  • @timpeterson the push takes place on your workstation, in your local repo, when you are pushing new commits to your bare repo on your server. It *does not* take place on your server in `/var/www`... – VonC Sep 23 '13 at 13:58
  • Oh dangit! I'm so stupid. Ok, let me repeat all this on my workstation. Just for clarity, my workstation is `.../nginx/html/`. – tim peterson Sep 23 '13 at 14:01
  • @timpeterson by "all this", you mean just the `git push` part, right? Because the `post-receive` hook still on your bare repo on the server: it will change directory to your live repo, set `GIT_DIR` and `GIT_WORK_TREE` properly, do the git pull, and the `submodule update`, still on the server. – VonC Sep 23 '13 at 14:04
  • Also for clarify, my workflow is as follows: 1. make changes in workstation 2. `git push origin master` 3. ssh into live server 4. `git pull origin master`. This results in my live server having the same files as my workstation with the exception of the submodules whose files don't exist in my live server (the problem I'm trying to fix). – tim peterson Sep 23 '13 at 14:06
  • @timpeterson so you mean you don't push to your server, but to Github I presume, and then pull from it? In that case, you wouldn't need the bare repo and the hook: a simple `git submodule update --init` after the pull would suffice, provided you did commit your submodule in your repo before pushing it to GitHub (as you see in https://github.com/VonC/compileEverything/tree/master/gitolite) – VonC Sep 23 '13 at 14:08
  • Yeah I push to Github and then pull from that. Ok let me try `git submodule update --init` after the pull... – tim peterson Sep 23 '13 at 14:09
  • So I need to do the `git submodule update --init` after the `pull` only if those submodules have changed, right? – tim peterson Sep 23 '13 at 14:14
  • @timpeterson I have updated my answer. You can do it every-time: if there is no change, that update won't do anything. – VonC Sep 23 '13 at 14:16
  • wow, thanks for hanging in there with me. I can't thank you enough. I think your updated answer addresses everything. Have a great day! – tim peterson Sep 23 '13 at 14:22