如何动态/自动添加文件到git?

时间:2016-01-09 21:37:28

标签: git bash

我使用push在Linux服务器上部署策略。

效果很好。

我遇到的问题是用户可以添加文件 - 用户创建的文件 我需要将这些文件添加到repo中。

这样做的策略是什么。

我希望在hooks/post-receive

中部署代码
#!/bin/sh
git --work-tree=~/public_html --git-dir=~/root.git checkout -f
echo ""
echo ""
echo ""
echo "success-"
echo ""
echo ""
echo ""

当用户添加文件时,它位于public_html中,并且绝不会被git跟踪。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您应该先在钩子中添加和提交用户文件:

git --work-tree=~/public_html --git-dir=~/root.git add -- path/to/userfile
git commit -m "add user file"

此处,path/to/userfile是相对于~/public_html的相对路径,git -c 'core.bare=false' --work-tree=~/public_html --git-dir=~/root.git checkout -f 是工作树的根文件夹。

然后你可以继续检查所有回购:

-c 'core.bare=false'

core.bare允许覆盖裸仓库的配置git add,以允许~/public_html继续进行,将git --git-dir=~/root.git config core.bare false git --work-tree=~/public_html --git-dir=~/root.git add -- path/to/userfile git --git-dir=~/root.git config core.bare true 视为其工作树。

如果' -c'选项不起作用(因为OP使用git 1.7.1 - 在2010年12月发布!),首先尝试(如果升级git不是一个选项)来更改设置,然后恢复它:

{{1}}

请注意,这是一种奇怪的做法,因为它会在带有额外提交的分支中生成git push结果。在git push之后不要忘记git pull,以便获得新的提交。

答案 1 :(得分:0)

如果您想添加所有文件,只需使用:

# `git add -A` is equivalent to  `git add . && git add -u`.
git add -A

它会将所有修改后的+重命名文件添加到索引中,然后您可以提交。

# on a single line it should be: 
git add . -A && git commit

此命令将添加您的文件,除非它们在您的.gitignore中或标有assume-unchanged标记

相关问题