zoukankan      html  css  js  c++  java
  • 版本控制系统-GIT学习文档

    安装运用版本控制系统Git

    一、安装git

    1.在linux系统下安装git:

    yum -y install git

    2.指定用户名和Email地址

    git config --global user.name “Your Name”

    git config --global user.email email@example.com

    二、创建版本库

    1.找一个合适的地方创建一个空目录或者也可以不用创建空目录,选择一个目录作为git的版本库,但是最好新建目录,便于管理

    mkdir git

    cd git

    2.通过git --bare init 命令将这个目录变成Git可以管理的仓库

     [root@ceshi git]# git --bare init

     Initialized empty Git repository in /Users/michael/learngit/.git/

    执行git config --global color.ui true,让输出的文件名称显示颜色

    3.查看该目录下会新生成一个.git目录,这个目录是Git用来跟踪管理版本库的

    [root@ceshi git]# ls -an

    drwxr-xr-x. 8 0 0 4096 11月 24 11:20 .git

    这样Git仓库就已经创建好了

    4.在该目录下随便创建一个文件,文件内容可以自定义

    [root@ceshi git]# cat ceshi.txt

      1.word

    5.用git add命令告诉git将文件添加到仓库

    git add ceshi.txt

    6.git commit 命令将该文件提交到git仓库

    [root@ceshi git]# git commit -m "wrote the first file"

    [master (root-commit) f53ca5a] wrote the first file

     1 files changed, 1 insertions(+), 0 deletions(-)  (返回值告诉你一个文件被改动,插入了一行内容,在ceshi.txt里有一行内容)

     create mode 100644 ceshi.txt

    注意:该命令中的-m是只本次提交的说明,可以自定义说明文字,但是需要有意义,这样可以在历史记录找到改动的记录

    三、查看工作状态和查看修改的内容

    1.刚才我们已经成功的添加并且提交了一个ceshi.txt文件,接下来我们改动该文件的内容

    [root@ceshi git]# cat ceshi.txt

      2.good

    2.现在我们运行git status查看工作状态

    [root@ceshi git]# git status

    # On branch master

    # Changed but not updated:

    #   (use "git add <file>..." to update what will be committed)

    #   (use "git checkout -- <file>..." to discard changes in working directory)

    #

    # modified:   ceshi.txt

    #

    no changes added to commit (use "git add" and/or "git commit -a")

    上面的返回告诉我们,ceshi.txt文件被修改过了,但是还没有提交

    3.我们虽然把ceshi.txt文件修改过了,但是如果我们忘记了修改的内容,也是可以查看到的

    [root@ceshi git]# git diff

    diff --git a/ceshi.txt b/ceshi.txt

    index b05d8c7..bdbace1 100644

    --- a/ceshi.txt

    +++ b/ceshi.txt

    @@ -1 +1 @@

    -1.word

    +2.good

    上面的返回告诉我们将1.word修改成了2.good

    4.接下来我们需要将ceshi.txt文件重新添加到git仓库

    [root@ceshi git]# git add ceshi.txt

    5.然后我们再次查看下工作状态

    [root@ceshi git]# git status

    # On branch master

    # Changes to be committed:(将被提交的更改)

    #   (use "git reset HEAD <file>..." to unstage)

    #

    # modified:   ceshi.txt

    上面返回告诉我们被提交的修改包括ceshi.txt

    6.这下我们可以放心提交该文件了

    [root@ceshi git]# git commit -m "worte the second file"

    [master 4b291e1] worte the second file

     1 files changed, 1 insertions(+), 1 deletions(-)

    返回提示我们一个文件被更改,一行被插入

    7.提交完成之后我们再次查看工作状态

    [root@ceshi git]# git status

    # On branch master

    nothing to commit (working directory clean)

    返回提示我们没有需要提交的修改,工作目录为是干净的

    四、版本回退(回档)

    1.我们再次修改文件内容进行提交

    [root@ceshi git]# cat ceshi.txt

      3.bad

    [root@ceshi git]# git add ceshi.txt

    [root@ceshi git]# git commit -m "wrote the third file"

    [master 2a2747f] wrote the third file

     1 files changed, 1 insertions(+), 1 deletions(-)

    [root@ceshi git]#

    2.查看历史修改记录可用git log进行查看

    [root@ceshi git]# git log

    commit 2a2747f9d2b8c2a114362f6e1858cae3e606e33d

    Author: “wenbinbin” <“wenbinbin@zhanchengkeji.com”>

    Date:   Thu Nov 24 11:56:57 2016 +0800

     

        wrote the third file

     

    commit 4b291e14e0b6cb3f30fd0d9473b07eef14135075

    Author: “wenbinbin” <“wenbinbin@zhanchengkeji.com”>

    Date:   Thu Nov 24 11:52:32 2016 +0800

     

        worte the second file

     

    commit f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1

    Author: “wenbinbin” <“wenbinbin@zhanchengkeji.com”>

    Date:   Thu Nov 24 11:35:13 2016 +0800

     

        wrote the first file

    You have new mail in /var/spool/mail/root

    [root@ceshi git]#

    以上显示的是从最近到最远的提交日志

    最近的提交是wrote the third file,最远的提交是wrote the first file

    3.或者我们可以用最简洁的查看方式

    [root@ceshi git]# git log --pretty=oneline

    2a2747f9d2b8c2a114362f6e1858cae3e606e33d wrote the third file

    4b291e14e0b6cb3f30fd0d9473b07eef14135075 worte the second file

    f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1 wrote the first file

    [root@ceshi git]#

    以上返回前面的是commit id后面的是提交说明

    4.版本回退

    a:回退到上一个版本

    我们在用git log查看到当前版本是write the third file,现在我们回退到上一个版本

    [root@ceshi git]# git reset --hard HEAD^

    HEAD is now at 4b291e1 worte the second file

    提示中说明已经回退到上个版本,我们可以查看下文件内容

    [root@ceshi git]# cat ceshi.txt

    2.good

    [root@ceshi git]#

    确实已经回退到上个版本

    我们用git log再看看现在的版本库状态

    [root@ceshi git]# git log

    commit 4b291e14e0b6cb3f30fd0d9473b07eef14135075

    Author: “wenbinbin” <“wenbinbin@zhanchengkeji.com”>

    Date:   Thu Nov 24 11:52:32 2016 +0800

     

        worte the second file

     

    commit f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1

    Author: “wenbinbin” <“wenbinbin@zhanchengkeji.com”>

    Date:   Thu Nov 24 11:35:13 2016 +0800

     

        wrote the first file

    You have new mail in /var/spool/mail/root

    [root@ceshi git]#

    提示说明当前版本是wrote the second file,但是我们最新的那个版本(wrote the third file)看不见了,我们可以通过刚才查看到的commit id进行回退到刚才的新版本

    b:恢复刚才的回退操作

    我们可以取刚才用git log --pretty=oneline命令查看到的commit id进行恢复

    [root@ceshi git]# git log --pretty=oneline

    2a2747f9d2b8c2a114362f6e1858cae3e606e33d wrote the third file

    4b291e14e0b6cb3f30fd0d9473b07eef14135075 worte the second file

    f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1 wrote the first file

    恢复时只取commit id的前几个数据即可

    [root@ceshi git]# git reset --hard 2a2747f9

    HEAD is now at 2a2747f wrote the third file

    [root@ceshi git]#

    提示告诉我们现在的版本是wrote the third file,恢复成功我们可以查看下ceshi.txt文件内容

    [root@ceshi git]# cat ceshi.txt

     3.bad

    [root@ceshi git]#

    C:回退到上上个版本

    [root@ceshi git]# git reset --hard HEAD^^

    HEAD is now at f53ca5a wrote the first file

    [root@ceshi git]#

    提示告诉我们现在回退到的版本是wrote the fist file,查看ceshi.txt验证

    [root@ceshi git]# cat ceshi.txt

      1.word

    [root@ceshi git]#

    确实已经回退

    [root@ceshi git]# git log

    commit f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1

    Author: “wenbinbin” <“wenbinbin@zhanchengkeji.com”>

    Date:   Thu Nov 24 11:35:13 2016 +0800

     

        wrote the first file

    [root@ceshi git]#

    日志中显示当前版本是wrote the fist file

    d:回退到上上上个版本用git reset --hard HEAD^^^

    e:回退到第100个版本用git reset --hard HEAD~100

    5.如果我们回退到了第一个版本又想恢复怎么办,在git中有个git reflog命令会记录你的每一次操作命令

    [root@ceshi git]# git reflog

    f53ca5a HEAD@{0}: HEAD^^: updating HEAD

    2a2747f HEAD@{1}: 2a2747f9: updating HEAD

    4b291e1 HEAD@{2}: HEAD^: updating HEAD

    2a2747f HEAD@{3}: commit: wrote the third file

    4b291e1 HEAD@{4}: commit: worte the second file

    [root@ceshi git]#

    可以查看到commit id进行回退操作

    五、误操作恢复

    1.当你在ceshi.txt文件中添加了一行,猛然发现你添加的这行代码是错的,这个时候你可以手动删除添加的那一行内容,然后执行git checkout -- ceshi.txt来舍弃之前的误操作

    [root@ceshi git]# git checkout -- ceshi.txt

    [root@ceshi git]# git status

    # On branch master

    nothing to commit (working directory clean)

    [root@ceshi git]#

    2.如果你在修改完之后还做了git add。想要撤销需要执行下面的命令

    [root@ceshi git]# git reset HEAD ceshi.txt

    Unstaged changes after reset:

    M ceshi.txt

    [root@ceshi git]# git status

    # On branch master

    # Changed but not updated:

    #   (use "git add <file>..." to update what will be committed)

    #   (use "git checkout -- <file>..." to discard changes in working directory)

    #

    # modified:   ceshi.txt

    #

    no changes added to commit (use "git add" and/or "git commit -a")

    [root@ceshi git]# git checkout -- ceshi.txt

    [root@ceshi git]# git status

    # On branch master

    nothing to commit (working directory clean)

    [root@ceshi git]#

    六、删除文件

    1.我们新添加一个文件test.txt,并提交

    [root@ceshi git]# touch test.txt

    [root@ceshi git]# git add test.txt

    [root@ceshi git]# git commit -m "add test.txt"

    [master 1a4d9dc] add test.txt

     0 files changed, 0 insertions(+), 0 deletions(-)

     create mode 100644 test.txt

    [root@ceshi git]#

    2.一般情况我们要删除文件是直接执行rm就可以

    但是我们在git目录下将test.txt删除之后,在git版本库中这个文件还在

    [root@ceshi git]# rm -rf test.txt

    [root@ceshi git]# git status

    # On branch master

    # Changed but not updated:

    #   (use "git add/rm <file>..." to update what will be committed)

    #   (use "git checkout -- <file>..." to discard changes in working directory)

    #

    # deleted:    test.txt

    #

    no changes added to commit (use "git add" and/or "git commit -a")

    [root@ceshi git]#

    Git告诉我们删除了一个test.txt文件但是版本库里的这个文件没有删除

    a:如果你是不小心删除了这个文件可以进行恢复

    [root@ceshi git]# git checkout -- test.txt

    You have new mail in /var/spool/mail/root

    [root@ceshi git]# ls

    ceshi.txt  test.txt

    [root@ceshi git]#

    b:如果你需要在版本库里也删除的话,可以执行

    [root@ceshi git]# git rm test.txt

    rm 'test.txt'

    [root@ceshi git]# git commit -m "remove test.txt"

    [master 941e92c] remove test.txt

     0 files changed, 0 insertions(+), 0 deletions(-)

     delete mode 100644 test.txt

    [root@ceshi git]# ls

    ceshi.txt

    [root@ceshi git]#

    七、配置别名

    1.将git status用git st来代替,命令如下

    git config --global alias.st status

    这样我们既可以用git st 也可以用git status,其他的命令也可以自定义进行修改

    2.显示最后一次提交信息需要执行

    [root@ceshi git]# git config --global alias.last 'log -1'

    [root@ceshi git]# git last

    commit 941e92cc6807f67e5ba184dd796132626a94ae02

    Author: “wenbinbin” <“wenbinbin@zhanchengkeji.com”>

    Date:   Thu Nov 24 15:14:56 2016 +0800

     

        remove test.txt

    [root@ceshi git]#

    博主github地址:https://github.com/bazingafraser/cv 本文章为Bazingafraser作者原创,转载请注明出处,违权必究:http://www.cnblogs.com/bazingafraser/
  • 相关阅读:
    软工课设第一周周五报告
    软工课设第一周周四报告
    软工课设第一周周三报告
    软工课设第一周周二报告
    软工课设第一周周一报告
    团队项目记录4
    团队项目记录3
    团队项目记录2
    jQuery 打气球小游戏 点击气球爆炸效果
    计网第二章:物理层
  • 原文地址:https://www.cnblogs.com/bazingafraser/p/8483764.html
Copyright © 2011-2022 走看看