分布式版本控制工具

分布式版本控制工具,第1张

Git分布式版本控制实战


Git是一个免费开源的分布式版本控制工具,也是目前最流行的分布式版本控制系统,可以减轻开发者和开源项目在管理分支代码上的压力。在这篇文章里,我会贴一张自己前期整理的笔记指导图,也会在后面的例题练习中讲解每个 *** 作的具体含义和图解,方便大家和自己参考。如有不正确之处,欢迎拍砖。



1。基本概念和术语:

  • Workspace:表示当前工作目录

  • 临时存储区:存储在stage区(gitaddfile/test.py)

    版本库:它存在于主代码库中(gitcommit)



    m的含义:

    第一列中的m表示“缓冲区”已更改。

    第二列中的m表示“工作区”已经更改。


    2。Git安装和环境准备

    junluobj@junluobj:~$ sudo install git  #安装git软件包 junluobj@junluobj:~$ mkdir git junlubbj@junluobj:~$ cd git ; mkdir project #创建两个目录方便后面示例说明 junluobj@junluobj:~/git/project$ ls -lh -rw-rw-r-- 1 junluobj junluobj  5 Aug  2 07:18 README.txt -rw-rw-r-- 1 junluobj junluobj 99 Aug  2 07:21 test.py


    4。Git全局配置

    许多开发人员会在项目或开发团队中提交代码。为了让代码以后可以查询,知道是谁提交的,强烈建议在项目组自己的工作环境中配置自己的git全局配置,这样可以查询自己代码提交的任何功能代码的记录,以及提交的时间,也可以知道团队成员提交的代码。请参见以下示例:

    junluobj@junluobj:~/git/project$ git config --global user.name Rocky  # 配置用户名 junluobj@junluobj:~/git/project$ git config --global user.email [email protected] #配置用户邮箱 junluobj@junluobj:~/git/project$ git config --global color.ui true   #输出信息可颜色显示 junluobj@junluobj:~/git/project$ git config --list  #列出所有的配置 user.name=Rocky [email protected] color.ui=true

    git全局配置设置信息将保存在。默认为当前主目录的gitconfig文件,也可以通过修改这个文件直接保存,修改后的信息可以通过gitconfig-list立即看到。

    junluobj@junluobj:~$ cat .gitconfig [user]         name = Rocky         email = [email protected] [color]         ui = true

    此外,如果您想要删除诸如user.name之类的设置,您可以按照以下说明进行 *** 作;您还可以修改。gitconfig文件手动

    junluobj@junluobj:~/git/project$ git config --unset --global user.name

    5。创建存储库

    创建存储库有两种方式,一种是本地初始化,另一种是远程克隆,如示例所示(可以从上图查询)。

    #在本地创建一个空的repo,也将在project 目录中创建一个.git 目录 junluobj@junluobj:~/git/project$ git init    Initialized empty Git repository in /home/junluobj/git/project/.git/ junluobj@junluobj:~/git/project$ ls -A .git/ branches  config  description  HEAD  hooks  info  objects  refs #通过远程克隆一个repo junluobj@junluobj:~/git$ git clone https://github.com/openstack-dev/devstack.git

    6。添加和提交文档

    project中有一个test.py和README.txt文件。现在我修改test.py,然后通过gitstatus检查存储库状态;标记的部分表示没有被git命令提交或注册,注意这里的状态。

    #添加文件,这里也可以通过git add --all 一次性添加文件 junluobj@junluobj:~/git/project$ git add test.py junluobj@junluobj:~/git/project$ git add README.txt junluobj@junluobj:~/git/project$ git status

    #提交文件 junluobj@junluobj:~/git/project$ git commit -m "first commit code" junluobj@junluobj:~/git/project$ git status # On branch master nothing to commit (working directory clean) # working directory 见上面的图解分析

    7。检查git状态

    在检查git的状态之前,我先用一个简单的例子来描述一下工作区:它表示当前的工作目录临时区域:它存储在stage区域(gitaddfile/test.py)版本库;它存储在主代码库中(gitcommit)。它是用来做什么的?如下图


    通常,我们在本地工作目录中修改或添加新代码。我们每次修改代码,都可以通过gitaddFile将文件添加到stagingarea后提交。我们可以通过gitcommitFile-m将文件提交给gitrepository,这是我们最终放置代码的地方。通过图示,更容易理解三个区域之间的关系

    下面的例子解释了git状态的细节:

    junluobj@junluobj:~/git/project$ git status # 查看git状态 # On branch master nothing to commit (working directory clean)

    我们可以看到git的当前状态,工作directroyclean,也就是说Stagingarea、工作目录、gitrepository的内容都是一样的

    同时,为了更好的区分其状态的细节,在工作目录下增加新的文件和修改代码,再次检查状态:


    test.py的第一列是空,第二列有标记为m的标志。

    testfile的第一列是A,第二列是空。这些是什么意思?

    通常,每个文件有两个标志位;test.py的第一个标志位是空,表示staging区域没有变化;第二个m表示工作目录已经改变;testfile的第一个标志A表示stage区域增加了新的变化,而后面的空表示工作目录没有变化,所以是空白色;

    再次修改test.py后,再次检查状态,发现test.py后有一个标志位M;这意味着工作目录已经改变。第一个意思是集结地变了。将新修改的文件再次添加到临时区域,并提交代码。

    使用gitstatus-ss再次检查git状态。没有显示表明三个区域的内容是一致的,而-s选项只是简单地显示git状态信息。

    8。检查文件差异

    您可以从下图
    中看到当前test.py和git存储库文件之间的差异

    9。撤消 *** 作:

    当我们在 *** 作过程中不小心提交了不正确的代码,想要取消它的 *** 作,有三种方法可以取消。

    第一种类型:gitresettest.py(从git存储库中取出以覆盖其暂存区中的文件)

    第二:gitcheckouttest.py(从临时区域覆盖当前工作目录中的文件)

    类型3:gitcheckoutHEADtest.py(从git存储库中覆盖当前工作目录中的文件)


    10.创建和删除分支机构

    从上图可以看出:

    junluobj@junluobj:~/git/project$ git branch # 列出当前所有的branch junluobj@junluobj:~/git/project$ git branch test-branch # 创建test-branch junluobj@junluobj:~/git/project$ git checkout test-branch #切换到新建的branch M       test.py Switched to branch 'test-branch' #切换到master 分支, 删除新建的分支 junluobj@junluobj:~/git/project$ git checkout master M       test.py Switched to branch 'master' junluobj@junluobj:~/git/project$ git branch -d test-branch Deleted branch test-branch (was 20b0d2e).


    11.检查git提交的记录:

    Git也有很多常用的Git命令,比如gitmerg、rm、mv等。这里就不截图画图一一展示了。只要查一下它的帮助信息,里面的命令大部分都会在工作中用到。


    12.Git常用命令

    #常用命令 junluobj@junluobj:~$ git --help    add        Add file contents to the index    bisect     Find by binary search the change that introduced a bug    branch     List, create, or delete branches    checkout   Checkout a branch or paths to the working tree    clone      Clone a repository into a new directory    commit     Record changes to the repository    diff       Show changes between commits, commit and working tree, etc    fetch      Download objects and refs from another repository    grep       Print lines matching a pattern    init       Create an empty git repository or reinitialize an existing one    log        Show commit logs    merge      Join two or more development histories together    mv         Move or rename a file, a directory, or a symlink    pull       Fetch from and merge with another repository or a local branch    push       Update remote refs along with associated objects    rebase     Forward-port local commits to the updated upstream head    reset      Reset current HEAD to the specified state    rm         Remove files from the working tree and from the index    show       Show various types of objects    status     Show the working tree status    tag        Create, list, delete or verify a tag object signed with GPG


    13.项目团队的Git-git工作流程:


    #1.从远程服务器(上游)克隆代码库: $ git clone -b project-code ssh://username@HOST/IP/git/ofm.git project-code #2.进入到当前的工作目录,并根据项目的组件或功能或bug 来创建本地的brach $ cd project-code $ git checkout -b story-123 #创建并切换到新建的branch #3.确保你是在当前新建的branch中,在其下面修改代码 $ git status or git status -s $ vim some-code / vim files #4.添加代码 $ git add some-code/files #5. 提交代码 $ git commit -m "coments xxxxx" #6.如果你的代码需要他人帮你review, 可创建一个path 发送给他 $ git format-patch -1 0001-second-commit.patch #7.在将你的代码推向到你的项目组的代码库中,你需要确保代码跟同事之间没有冲突 $ git checkout project-code $ git pull $ git rebase project-code story-123 # 若有冲突,解决其冲突的部分,并在此添加冲突的文件 $ git add conf/conflict.file $ git rebase --continue #8.切换到项目的分支并自建的分支内容 $ git checkout project-code $ git merg story-123 #9. 将代码推向到项目组代码库中 $ git push #10.最后删除自建的分支 $ git branch -d story-123


    14.Git引用:

    http://www.Git-SCM.com/book
    http://Gitref.org/
    http://en.Wikipedia.org/wiki/Git_%28software%29
    https://github.com/

    欢迎分享,转载请注明来源:内存溢出

    原文地址: http://outofmemory.cn/zz/784427.html

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-05-04
    下一篇 2022-05-04

    发表评论

    登录后才能评论

    评论列表(0条)

    保存