Git 分布式版本控制 实战

Git 是一个免费开源的分布式版本控制工具,也是目前最流行的分布式版本控制系统,可以很好的减轻开发人员及开源项目对于管理分支代码的压力。本篇将贴出前期自己整理的笔记导图,也会在后面示例演练中说明每个操作的具体含义以及图解,方便大家及自我查阅, 有不正确的地方欢迎拍砖。

1. 基本概念及术语:

M的含义:

第一列的M表示“缓存区“有改动

第二列的M表示“工作区”有改动

2. Git 安装及环境准备

junluobj@junluobj:~$sudoinstallgit#安装git软件包junluobj@junluobj:~$mkdirgitjunlubbj@junluobj:~$cdgit;mkdirproject#创建两个目录方便后面示例说明junluobj@junluobj:~/git/project$ls-lh-rw-rw-r–1junluobjjunluobj5Aug207:18README.txt-rw-rw-r–1junluobjjunluobj99Aug207:21test.py

4. Git 全局配置

在项目或开发小组中都会有很多开发人员提交代码, 为了让代码日后查询以及知道是谁提交的,强烈建议在项目组中在自己的工作环境下配置自己的git全局配置,这样自己的代码提交了什么功能性的代码, 什么时候提交的,都可以从记录中查询, 也知道team成员提交的代码情况, 好了下见下面示例:

junluobj@junluobj:~/git/project$gitconfig–globaluser.nameRocky#配置用户名junluobj@junluobj:~/git/project$gitconfig–globaluser.emailRocky@test.com#配置用户邮箱junluobj@junluobj:~/git/project$gitconfig–globalcolor.uitrue#输出信息可颜色显示junluobj@junluobj:~/git/project$gitconfig–list#列出所有的配置user.name=Rockyuser.email=Rocky@cn.ibm.comcolor.ui=true

git 全局配置设置信息默认会保存在当前家目录的.gitconfig 文件中, 也可以直接的通过对此文件进行修改保存, 同时通过 git config –list 可以立即看到修改的信息。

junluobj@junluobj:~$cat.gitconfig[user]name=Rockyemail=Rocky@test.com[color]ui=true

5. 创建repository

创建repository 有两种方式, 一种是本地初始化,一种是从远程克隆,见示例(可从上导图查询)

#在本地创建一个空的repo,也将在project目录中创建一个.git目录junluobj@junluobj:~/git/project$gitinitInitializedemptyGitrepositoryin/home/junluobj/git/project/.git/junluobj@junluobj:~/git/project$ls-A.git/branchesconfigdescriptionHEADhooksinfoobjectsrefs#通过远程克隆一个repojunluobj@junluobj:~/git$gitclonehttps://github.com/openstack-dev/devstack.git

6. 添加及提交文件

在project 有一个test.py 和 README.txt 文件,现在我对test.py 进行修改,然后通过git status 查看repository 状态; 红色标记部分表示还未提交或未通过git命令进行登记, 同时注意此处的状态。

#添加文件,这里也可以通过gitadd–all一次性添加文件junluobj@junluobj:~/git/project$gitaddtest.pyjunluobj@junluobj:~/git/project$gitaddREADME.txtjunluobj@junluobj:~/git/project$gitstatus

#提交文件junluobj@junluobj:~/git/project$gitcommit-m”firstcommitcode”junluobj@junluobj:~/git/project$gitstatus#Onbranchmasternothingtocommit(workingdirectoryclean)#workingdirectory见上面的图解分析

7. 查看git状态

在查看git状态前,我先用个简单的示例说下 工作区: 表示当前的工作目录 暂存区:存放到stage area(git add file/test.py) 版本库:存在master代码库中(git commit )都是用来做什么的: 如下图

通常我们会在自己本地working Directory中修改或新增代码,每当我们修改完代码后, 通过git add File 将文件添加到staging area后就可以对文件进行提交了,通过git commit File -m 就可将文件提交到git repository 中,也就是我们最终存放的代码的地方。经过图解,更加容易理解三个区层的关系

下面通过示例来进一部阐述git 状态的细节:

junluobj@junluobj:~/git/project$gitstatus#查看git状态#Onbranchmasternothingtocommit(workingdirectoryclean)

我们可以看到当前的git 状态 working directroy clean, 也就表示 Staging area (缓存区)、Working Directory及git repository的内容都是一样的;

同时,为了更好区别其状态的细节,在working directory 下增加新的文件及修改代码,再次查看状态:

从上图中可以看到:

junluobj@junluobj:~/git/project$gitbranch#列出当前所有的branchjunluobj@junluobj:~/git/project$gitbranchtest-branch#创建test-branchjunluobj@junluobj:~/git/project$gitcheckouttest-branch#切换到新建的branchMtest.pySwitchedtobranch’test-branch’#切换到master分支,删除新建的分支junluobj@junluobj:~/git/project$gitcheckoutmasterMtest.pySwitchedtobranch’master’junluobj@junluobj:~/git/project$gitbranch-dtest-branchDeletedbranchtest-branch(was20b0d2e).

11. 查看git 提交的记录:

Git 还有很多git 常用命令,如git merg ,rm 、mv 等等 这里就不一一去截图画图来显示了,直接查看它的帮助信息即可,里面大部分的命令在工作都会使用到。

12. Git 常用命令

#常用命令junluobj@junluobj:~$git–helpaddAddfilecontentstotheindexbisectFindbybinarysearchthechangethatintroducedabugbranchList,create,ordeletebranchescheckoutCheckoutabranchorpathstotheworkingtreecloneClonearepositoryintoanewdirectorycommitRecordchangestotherepositorydiffShowchangesbetweencommits,commitandworkingtree,etcfetchDownloadobjectsandrefsfromanotherrepositorygrepPrintlinesmatchingapatterninitCreateanemptygitrepositoryorreinitializeanexistingonelogShowcommitlogsmergeJointwoormoredevelopmenthistoriestogethermvMoveorrenameafile,adirectory,orasymlinkpullFetchfromandmergewithanotherrepositoryoralocalbranchpushUpdateremoterefsalongwithassociatedobjectsrebaseForward-portlocalcommitstotheupdatedupstreamheadresetResetcurrentHEADtothespecifiedstatermRemovefilesfromtheworkingtreeandfromtheindexshowShowvarioustypesofobjectsstatusShowtheworkingtreestatustagCreate,list,deleteorverifyatagobjectsignedwithGPG

13. Git-项目组git工作流程:

少一点预设的期待,那份对人的关怀会更自在

Git 分布式版本控制 实战

相关文章:

你感兴趣的文章:

标签云: