多语言展示
当前在线:965今日阅读:23今日分享:31

如何使用git快速克隆远程仓库代码

git对于代码版本控制来说非常方便,实用,本文着重介绍如何高效的使用git命令将远程代码克隆至本地。
工具/原料
1

Linux

2

git

3

gerrit

方法/步骤
1

先说明一下通过web浏览器的方法,这个在之前gerrit评审的时候就说过,要根据具体的情况而定,是用SSH还是HTTP,这里选择SSH:

2

然后,将该命令在Linuxshell中执行即可。

3

现在我们克隆代码比如命令为,git clone https://xx.x.x.x/xxxxxx/xxxx,克隆代码需要指明repo的分支名等信息,经常输入的时候比较麻烦,我们需要将该命令相同的部分封装成一个命令,提交者才更有效率的工作。

4

有个python的脚本供大家参考:#!/usr/bin/env python# coding=utf-8 # for git clone helperimport os, sysGERRIT_SERVER='xxx.xx.xx.xx'  ####这里写gerrit服务器IPGERRIT_LISTEN_PORT='29418'  ####这里指定端口GIT_REPO_NAME=''SSH_CLONE_FULL_CMD=''GERRIT_HOOKS_CMD='' WHOAMI=''def hello(): print '\033[0;32;40m' #print 50*('*') print  print '\tGerrit source code clone tools' print  print 50*('*') print '\033[0m'def __exit(x): sys.exit(x) def error_color(): print '\033[0;31;40m' def normal_color(): print '\033[0m' def get_username(): global WHOAMI WHOAMI = os.popen('whoami').read().strip()def prep_git_repo(): global GIT_REPO_NAME GIT_REPO_NAME=raw_input('Please input the dest Git Repository:') if (len(GIT_REPO_NAME) <= 0): error_color() print 'Invalid Git Repository, You entered NULL value!' normal_color() __exit(1) print('The dest Git Repo is %s') % GIT_REPO_NAME def prep_ssh_clone_str(): global SSH_CLONE_FULL_CMD global GIT_REPO_NAME global WHOAMI  global GERRIT_SERVER global GERRIT_LISTEN_PORT SSH_CLONE_FULL_CMD = 'git clone ssh://'+WHOAMI+'@'+GERRIT_SERVER+':'+GERRIT_LISTEN_PORT+'/'+GIT_REPO_NAME print SSH_CLONE_FULL_CMDdef prep_gerrit_hooks(): global WHOAMI global GIT_REPO_NAME global GERRIT_HOOKS_CMD global GERRIT_LISTEN_PORT global GERRIT_SERVER GERRIT_HOOKS_CMD = 'scp -p -P ' + GERRIT_LISTEN_PORT + ' ' + WHOAMI+ '@' + GERRIT_SERVER + ':hooks/commit-msg ' + GIT_REPO_NAME + '/.git/hooks/' print GERRIT_HOOKS_CMDdef do_shell_cmd(CMD): os.system(CMD)def do_git_clone(): global GERRIT_HOOKS_CMD global SSH_CLONE_FULL_CMD get_username() prep_git_repo() prep_ssh_clone_str() prep_gerrit_hooks() do_shell_cmd(SSH_CLONE_FULL_CMD) do_shell_cmd(GERRIT_HOOKS_CMD) print '\033[0;32;40m' print 'Git Clone done!' normal_color()if __name__ == '__main__': hello() do_git_clone() __exit(0)

6

总结:1. 需要指定的Gerrit Server地址2. 设置好PATH

推荐信息