问题
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?
回答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).
来源:https://stackoverflow.com/questions/49838135/why-is-checkout-f-the-right-thing-to-put-in-a-post-receive-hook