2016-12-30 44 views
2

我正在写一个Lua脚本,它创建一个目录,在其中创建一些文件并初始化git,将这些文件添加到它并最终提交所有内容。然而,从Lua内部没有办法使用cd(你可以,但它不会有效果),所以我想知道是否有可能git init一个目录,git add某些文件,最后是git commit -a -m "message",所有的工作目录都是上面的目录所需的目录。编辑:-C作品,谢谢大家。对于任何人想知道,在呼叫os.execute结束后,在Lua,cd“重置”。因此,os.execute("cd mydir"); os.execute("git init");将无法​​按预期工作。要使其起作用,请使用os.execute("cd mydir; git init;");git init,添加,从不同的目录提交

+0

所以混帐回购协议是在LUA脚本的位置的子目录。那是对的吗? –

+0

@代码学徒准确。 – user6245072

+0

[这个问题](http://stackoverflow.com/questions/3769137/use-git-log-command-in-another-folder)特定于'git log',但看看' - git-dir '标志适用于其他命令。 –

回答

3

通过关于-CI评论下面的提示做:

git init newStuff 
Initialized empty Git repository in c:/fw/git/initTest/newStuff/.git/ 

作出的dir Newstuff文件git的回购协议(我已经创建)

然后我说两个文件Newstuff文件,并从它的父母使用-C

git -C newStuff/ status 
On branch master 

Initial commit 

Untracked files: 
    (use "git add <file>..." to include in what will be committed) 

     new1 
     new2 

nothing added to commit but untracked files present (use "git add" to track) 

我看到新的文件。上壳

git -C newStuff/ add . 
git -C newStuff/ status 
On branch master 

Initial commit 

Changes to be committed: 
    (use "git rm --cached <file>..." to unstage) 

     new file: new1 
     new file: new2 

git -C newStuff/ commit -m"initial" 
[master (root-commit) bfe387b] initial 
2 files changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 new1 
create mode 100644 new2 
2

例:新增并提交它们

#!/bin/sh 
dirPath=some/dir/path 
mkdir -p $dirPath 
touch $dirPath/newFile 
git init $dirPath 
git -C $dirPath add . 
git -C $dirPath commit -a -m "Initial commit" 
git -C $dirPath log