开发模式介绍
master为生产环境分支
trunk为测试环境分支
开发分支由程序员自己取名
比如来一个新项目之后,下面步骤都是在本地操作
1.从本地获取远程master最新代码,保证本地master是最新的代码
2.根据master新建一个新的分支
3.开发完成以后,获取远程新的trunk分支代码,保证合并之前本地trunk是最新的代码,把开发分支合并到trunk分支进行测试
4.测试通过以后,获取远程master最新代码,保证合并之前本地master是最新的代码,开发分支合并的master
ps:如果是线上紧急bug,有时候可以在master更改并更新,我们是不允许把trunk分支合并到master分支
gitlab和git操作
一名开发者使用git的步骤
首先告诉ops(一般公司都是运维管理GItLab)要对哪个项目进行开发,ops确认后开通gitlab账号并授权项目到开发者账号,然后安装git客户端
这里以windows安装git bash 2.7的客户端演示一些常用的操作命令,安装过程很简单和装qq一样傻瓜式安装:
现在Windows上面安装git bash 2.7,安装完成后打开如下图:
在E盘上面创建了gittest目录,然后cd到此目录下
要和GitLab通信需要使用秘钥,客户端生成秘钥
$ ssh-keygen.exe -t rsa -C "xiewenming@qq.com" Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/lenovo/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/lenovo/.ssh/id_rsa. Your public key has been saved in /c/Users/lenovo/.ssh/id_rsa.pub. The key fingerprint is: SHA256:YgzUnNdCsLYNOZXc5FiT6aDZq9Q0emugr0IBT5Z8PwI xiewenming@qq.com The key's randomart image is: +---[RSA 2048]----+ | . ..o.=o=+o | |. E.. ++*+=. | | = o..*=.+. | | o .+==+ . | | . .==So | | . .+.+ | | . o + . | | . . . o | | ..o.. | +----[SHA256]-----+ warren@lenovo-PC MINGW32 /e/gittest (master)
查看公钥
$ cat /c/Users/lenovo/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfrhsM3P/uuzDt6akRTJTfZMESiyfqZO6MOrs1ORalu/8X30e+g8/VCMFUviHfXV9GlbiDALDyVlaVxm10Yp205CafWEnlsjjnLTaFyp9JfBdagbF7HJ0horwpJfPlh6Byu/S9dbI5xj9wOFEclzrSHJzJBmLGfVdC4zkZn4AWkaDAbC7LiOAhHDUG1pFNa/uWBCJwR/POlON6v8BYpbUSOi91a6FIskAO5jiX8Ok0YFTtbUs+s1cQbcfxC4C2Lc/Nj8PyXXGh+JpQxp6m2Zxa26kSRgMc0NaPIhNZqcdPjGwS6ChYW6q6hywf9XvUsYYguV8Hgedo1eAn3rYpbHpsqT xiewenming@qq.com warren@lenovo-PC MINGW32 /e/gittest (master)
登陆到GitLab把公钥拷贝到自己的账号里面
git客户端初始化操作
$ git config --global user.name "xiewenming"
warren@lenovo-PC MINGW32 /e/gittest (master)
$ git config --global user.email xiewenming@qq.com
warren@lenovo-PC MINGW32 /e/gittest (master)
$ git clone git@192.168.x.x:ops/scripts.git
#帮助命令git help
查看分支
$ git branch
* master
创建新分支并切换到新分支
$ git checkout -b test M GitTongJi.sh Switched to a new branch 'test'
创建开发分支,不会切换到分支新分支
$ git branch xiewenming
切换到开发分支
$ git checkout xiewenming
修改文件,增加新文件并查看修改状态
warren@lenovo-PC MINGW32 /e/gittest/scripts (xiewenming) $ vi test1.log $ vi LogTongji.sh warren@lenovo-PC MINGW32 /e/gittest/scripts (xiewenming) $ git add . #增加新文件需要add,add只是把文件添加到本地缓存区,还需要使用commit进行提交 warren@lenovo-PC MINGW32 /e/gittest/scripts (xiewenming) $ git status On branch xiewenming Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: LogTongji.sh new file: test1.log warren@lenovo-PC MINGW32 /e/gittest/scripts (xiewenming) $
删除add添加的缓存区文件
$ touch 1234 warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git rm 1234 #没有add 可以直接用os命令删除
fatal: pathspec '1234' did not match any files warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git add . warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git rm 1234 #add以后需要用 git rm -f 强制删除 error: the following file has changes staged in the index: 1234 (use --cached to keep the file, or -f to force removal) warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git rm -f 1234 rm '1234'
提交更新并加注释
$ git commit -m "test" [xiewenming 67c298a] test 3 files changed, 137 insertions(+), 135 deletions(-) create mode 100644 test.log create mode 100644 test1.log warren@lenovo-PC MINGW32 /e/gittest/scripts (xiewenming)
把本地分支推到gitlab上面
$ git push origin xiewenming:xiewenming Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 373 bytes | 0 bytes/s, done. Total 5 (delta 2), reused 0 (delta 0) To git@192.168.x.x:ops/scripts.git 9e553ab..67c298a xiewenming -> xiewenming warren@lenovo-PC MINGW32 /e/gittest/scripts (xiewenming)
切换到master分支,把xiewenming分支合并到当前分支
$ git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'. warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git merge xiewenming Updating 9e553ab..67c298a Fast-forward LogTongji.sh | 2 +- test.log | 1 + test1.log | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 test.log create mode 100644 test1.log warren@lenovo-PC MINGW32 /e/gittest/scripts (master)
删除名称为xiewenming的分支 小d也可以
$ git branch -D xiewenming Deleted branch xiewenming (was 67c298a). warren@lenovo-PC MINGW32 /e/gittest/scripts (master)
删除远程分支为xiewenming的分支
$ git push origin :xiewenming To git@192.168.x.x:ops/scripts.git - [deleted] xiewenming warren@lenovo-PC MINGW32 /e/gittest/scripts (master)
查看当前分支的提前日志
$ git log commit 9e553abcd3582a0481a2884fd0babc3e13b9732a Author: root <root@192-168-x-55.xxx.com> Date: Mon Jun 12 11:22:07 2017 +0800 vi ShangXian.sh 添加删除bak分支 commit f7dc4cd80b0a7550217aac17cd17094a4917831d Author: root <root@192-168-x-55.xxx.com> Date: Thu Jun 1 10:20:27 2017 +0800 add commit 43e4c138499fe478cc397f256de4841c714d8315 Author: root <root@192-168-x-55.xxx.com> Date: Tue May 23 18:56:55 2017 +0800 add sh
根据commit id回滚,commit id 可以通过git log查看
$ git reset --hard 9e553abcd3582a0481a2884fd0babc3e13b9732a HEAD is now at 9e553ab vi ShangXian.sh 添加删除bak分支 warren@lenovo-PC MINGW32 /e/gittest/scripts (master)
查看本地和远程分支
$ git branch -a master * xiewenming remotes/origin/HEAD -> origin/master remotes/origin/master remotes/origin/xiewenming
从创建文件到提交远程master完整的步骤
$ touch 123 warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git add . warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: 123 warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git commit -m "test" [master f904e95] test 2 files changed, 151 insertions(+), 151 deletions(-) create mode 100644 123 warren@lenovo-PC MINGW32 /e/gittest/scripts (master) $ git push origin master Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 1.11 KiB | 0 bytes/s, done. Total 3 (delta 2), reused 0 (delta 0) To git@192.168.x.x:ops/scripts.git 9e553ab..f904e95 master -> master warren@lenovo-PC MINGW32 /e/gittest/scripts (master)
更新远程master分支代码到本地
$ git pull origin master remote: Counting objects: 2, done. remote: Compressing objects: 100% (2/2), done. remote: Total 2 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (2/2), done. From 192.168.x.x:ops/scripts * branch master -> FETCH_HEAD f904e95..75c9baf master -> origin/master Updating f904e95..75c9baf Fast-forward 123 | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 123