0

I have a VPS running a Node.js / React app.

I want to update the files in the vps each time I push data to the git(hub).

I found out, using this answer, that I can add some hooks in git, executing commands on "post-receive".

What I didn't quite understand :

  1. Why did he init another git ? Couldn't he have done this in the .git directory and create the hooks/post-receive file?

  2. Why git checkout -f ? If the goal is to update local files, so nodemon / create-react-app restarts the server / app, why not execute a git pull instead ?

  3. Is there a better way of doing this ?

0xRyN
  • 852
  • 2
  • 13

1 Answers1

0
  1. In the recommended answer there, nobody is using GitHub and there is no other Git repository yet. So the answer to your question:

    Couldn't he have done this in the .git directory and create the hooks/post-receive file?

    is: No, there was no .git directory in the first place. The target machine had nothing at all, no Git repository, no working tree, etc. The git init --bare created the Git repository (the ".git directory").

  2. The git checkout -f is a poor-man's / low-quality implementation of push to deploy. A receiving repository is normally "bare", and this one is no exception.

    why not execute a git pull instead ?

    That would require creating a third Git repository. That would have been an option.

  3. "Better" is in the eye of the beholder. There are many ways of doing this, each with its own pluses and minuses. See also Deploy a project using Git push, which notes that since Git 2.3, receive.denyCurrentBranch = updateInstead is available; it was not available prior to 2015 (and in 2015, many people had older versions of Git installed).

Note further that if you're using GitHub as a hosting system, this changes a number of variables. The questions and answers you and I have linked here are aimed at those not using GitHub.

torek
  • 448,244
  • 59
  • 642
  • 775