1

I have an account on Dreamhost and they have instructions on using Git to track files in a custom WordPress theme. Their instructions, at, https://help.dreamhost.com/hc/en-us/articles/227816388-Using-Git-with-DreamPress, are the same as many other websites that suggest same. For example, http://git-memo.readthedocs.io/en/latest/deploy.html

You put in an executable script in a bare repo's hooks/post-receive directory and it calls a git "checkout -f". Here is the example script

#!/bin/sh
GIT_WORK_TREE=/home/user/theme-directory git checkout -f  

Here is what I don't understand. Why "checkout -f"? That only changes the name of the branch being tracked, it does not bring it up to date. Shouldn't there be a pull (or fetch) the new content? Does checkout have more power than I undersand?

pauljohn32
  • 2,079
  • 21
  • 28

1 Answers1

1

post-receive is a server-side hook which will be executed on git push.

So no need for a pull: the client pushes some content, which is then checked out on the server.

Why "checkout -f"?

First, "checkout -f" is actually checkout -f @, or checkout -f HEAD: it checks out whatever HEAD is now (after the push).

Second, the --force option ensures that switching branch succeeds even if the index or working tree differs from HEAD (which it will, since the push just changed said HEAD)

But I would try instead

GIT_WORK_TREE=/home/user/theme-directory git checkout -f  -- .

That is: specifying a pathspec, which will overwrite paths in the working tree by replacing with the contents in the index or in the (most often a commit, here: HEAD, which just changed after the push).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Sorry, you are saying same words that I couldn't understand from beginning. On the server there are 2 folders, the bare repo and the cloned folder (where in past we checked out master, presumably). In the post push script, how does the colder folder come up to date? The checkout run in the cloned folder just designates which branch is used, in effect it does nothing so far as I can tell. – pauljohn32 Apr 23 '18 at 17:49
  • @pauljohn32 OK. I have edited my answer accordingly. – VonC Apr 23 '18 at 18:42
  • You are +1 here, thanks for effort. I think you are saying "checkout -f HEAD" is not only for changing branches. It has effect of pulling newest commit into the cloned repo. I'm going to test that and see. – pauljohn32 Apr 24 '18 at 21:22
  • No it only modifies the index and working tree to reflect a branch or a commit – VonC Apr 24 '18 at 21:24
  • If that is true, how does the update that is pushed to the bare/server repo find its way over to the checked out project directory. Which was my original question. – pauljohn32 May 03 '18 at 20:44
  • @pauljohn32 Because what is pushed will change HEAD (of the pushed branch). And HEAD is what is checked out in the project directory. – VonC May 03 '18 at 21:52