2

I want to use a hook on my server in git to check my code when a push occurs.

I have my code checking solution ready to go. The problem with git's hooks is that it's hard to check out the latest code from the server's git repository when an update occurs.

How can I checkout directly from a remote repository in a latest git environment?

I found an example code on stackoverflow, but it's 6 years old. I don't know why, but commands like '--work-tree' are not available in my environment. I need another approach. Git server hooks, check if code is formated

bzImage
  • 21
  • 5

2 Answers2

0

A simpler version (than the one I mentioned 6 years ago) would be a post-receive hook which would set both the GIT_DIR (your current bare repository) and the WORK_TREE (the folder where you want to check out your repository)

git --git-dir="$GIT_DIR" --work-tree=. -C "$TARGET" restore -- .

Replace $TARGET by the path where you want your files to be checked out.

Replace git restore with git checkout if the Git on your server is older than Git 2.23 (Q3 2019).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Oops. --work-tree is valid command. The problem is elsewhere.

git --work-tree="<somewhere temporary path>" checkout HEAD

It works with gitea repository on Windows.
When I use <path> = "D:\check-file\\" it is not working.
But when I use <path> = "D:\check-file" or "D:\check-file\\\\" it is working fine.

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
bzImage
  • 21
  • 5
  • 1
    `--work-tree` is more accurately a valid *option* (option to the [`git` command](https://git-scm.com/docs/git#Documentation/git.txt---work-treeltpathgt) itself) – VonC Mar 28 '23 at 06:44