从脚本执行git提交

问题描述 投票:0回答:2

我想将一些文件从脚本提交到我拥有的git服务器,这需要自动完成。

我的脚本(位于~/bin)做了类似的事情

rsync -arE --delete --files-from=~/files.list / ~/repo
git --git-dir ~/repo/.git add ~/repo/*
git --git-dir ~/repo/.git commit -m "automatic commit #123"
git --git-dir ~/repo/.git push

我希望它将文件~/files.list中列出的文件复制到~/repo(rsync处理这个),然后将这些文件添加到存储库,提交和推送。

但是,它无法正确地将文件添加到git提交,特别是它总是抓取工作目录中的文件(在本例中为~/bin),所以我需要一种方法来:

  1. 更改git命令的运行目录
  2. 告诉git从工作目录以外的目录中添加文件
git directory rsync
2个回答
2
投票

这听起来像你想要的是Git的-C选项,它在运行之前更改当前目录。您可以编写如下内容:

rsync -arE --delete --files-from=~/files.list / ~/repo
git -C ~/repo/ add .
git -C ~/repo/ commit -m "automatic commit #123"
git -C ~/repo/ push

当然,您也可以将其作为cd和子shell编写,但上面的内容可能会更容易一些:

rsync -arE --delete --files-from=~/files.list / ~/repo
(cd ~/repo/ &&
 git add .
 git commit -m "automatic commit #123"
 git push)

我在这里假设你的rsync命令已经在做你想要的了。


2
投票

无需复制,只需告诉git你的工作文件在哪里。

git -C ~/repo --work-tree="$PWD" add .
git -C ~/repo commit -m etc
git -C ~/repo push

把你的files.list列表放在.gitignore格式中,忽略除了你想让git追捕的文件之外的所有内容,并将它包含在repo的.git/info/exclude中:

# To include only this, and/this, and/that but ignore everything else:
# ignore everything
*
# except
!this
!and/this
!and/that
!etc
# do search inside directories, don't let ignores stop later searches inside
!*/

注意,命令行上的路径是相对于工作树的,如果你还没有在里面,所以如果你在一个仓库并想要从其他地方添加内容,你可以git --work-tree=/path/to/elsewhere add .,它会添加所有。

© www.soinside.com 2019 - 2024. All rights reserved.