git 命令不在git 仓库内执行的办法

假设工作区所在目录为 /home/linyihai/workspace
本地git仓库为/home/linyihai/atc-testcase
那么可以在工作区目录执行:
GIT_DIR=home/linyihai/atc-testcase/.git git pull

注意:git add 如果使用这种方式,git clone远端仓库时,会出现这样的问题:
假设远端仓库名称为:atc-testcase
使用git clone命令克隆远端仓库到本地路径下,克隆下来的本地仓库
会变成 atc-testcase/atc-testcase,即出现双层仓库的路径,外面的仓库时真正的git 仓库,内部atc-testcase只是包了仓库文件的文件夹
所以必须真正切换到git 仓库中执行git add命令才不会出问题


在python 工程中,可以这样执行:
 1 def run_suprocess_cmd(cmd):
 2   p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
 3   for line in p.stdout.readlines():
 4     logger.debug(line)
 5 
 6 git_prefix = 'GIT_DIR=' +'/home/linyihai/atc-testcase' + '/.git '
 7 git_add = git_prefix + "git add --all "
 8 current_work_path = os.path.dirname(os.path.abspath(__file__)) #先保存当前工作目录
 9 os.chdir('/home/linyihai/atc-testcase') #切换到git 仓库路径去 
10 run_suprocess_cmd(git_add) #执行git命令,使用模块subprocess.Popen执行git add命令
11 os.chdir(current_work_path) #执行完后切换回当前工作目录
 
posted @ 2016-07-22 19:48  yihailin  阅读(1846)  评论(0)    收藏  举报