如何在 git hook post-receive 后同步 stage(index) 和工作目录?

How to sync the stage(index) and working directory after git hook post-receive?

在我的另一个问题中,, 我可以使用 git 挂钩来调用 post-receive 以便工作目录与 git 裸存储库相同。

unset GIT_INDEX_FILE
git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f

推送后修改了工作目录,但索引没有更新。 例如,在我的本地机器上,我添加了一行“???”在 hello.txt。我 added/commited/push 更改为自动更新服务器工作 git 目录中的 hello.txt。

diff --git a/hello.txt b/hello.txt
index 3ab3c7a..b63ad40 100644
--- a/hello.txt
+++ b/hello.txt
@@ -4,3 +4,5 @@ Again, helloooo, world?
 Hello, world? Automatically?

 How about this?
+
+????

我尝试从服务器的工作目录 add/commit/push 得到这个错误。

ubuntu@ip-172-31-63-19 dumb> git push
To /home/ubuntu/git/dumb.git/
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '/home/ubuntu/git/dumb.git/'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

可能出了什么问题?如何同步stage(index)和工作目录?

参考:https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks

编辑

What's the difference between "git reset" and "git checkout"? 开始,我可以在工作目录中 运行 git reset --hard 进行同步,但推送的更改会被覆盖。然后,使用 git push,我可以获得更新版本,但我需要合并。

这是来自服务器工作目录的 git log --pretty=oneline

86461a03f7d46bbc90d1ef47ae3a21774848407f Merge branch 'master' of /home/ubuntu/git/dumb
049111edb0e612ec3bf364ea01423cdea8575560 1234
f506be37c3cd6d24ec727ebfb551c9d5dbd780b5 ?*
ca5a0f5efb90788c3e1669d192d3fb333fdbcd72 ?

这是来自本地目录

049111edb0e612ec3bf364ea01423cdea8575560 1234
f506be37c3cd6d24ec727ebfb551c9d5dbd780b5 ?*
ca5a0f5efb90788c3e1669d192d3fb333fdbcd72 ?

问题在于,即使工作目录包含服务器的最新更新,本地 git 目录中的索引和历史记录也不会更新。

我认为这不是最佳解决方案,但在服务器的工作目录中,我可以同步到最新更新。

  1. git 隐藏
  2. git拉
  3. git 藏品掉落

它存储工作目录和index/history中的更改以防止冲突,然后拉动同步并丢弃存储。现在,我可以自动从我的本地目录同步到服务器的工作目录,并且我可以不时更新更改。

虽然我期待一个完整的解决方案,而不是这个临时解决方案。