zoukankan      html  css  js  c++  java
  • 新手学习Git之在本地使用Git

    每个开发人员应该都会一个版本管理工具,在Git和SVN中,我选择以Git,以下是我的一些心得

    什么是 Git

    Git是目前世界上最先进的分布式版本控制系统(没有之一)。

    一、Git安装

    1)、linux下安装:

    Ubuntu Linux,通过一条sudo apt-get install git就可以直接完成Git的安装,非常简单。

    2)、windows下安装:

    Git官网 下载,然后全部选默认,一直点next就可以

    2.首次使用的一些设置

    1)、设置用户名和邮箱,用来区分用户(注:在windows中,安装完成后在开始菜单中有个Git Bash,点击这个就可以打开Git,里边是写Git的命令的)

    $ git config --global user.name "Your Name"
    $ git config --global user.email "email@example.com"
    

    2)、创建版本库

    创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录:

    $ mkdir learngit
    $ cd learngit
    $ pwd 
    D:/JavaWorkSoftware/git/learngit
    

    注:最后这个pwd是用来查看当前目录,如果你使用Windows系统,请确保目录名(包括父目录)不包含中文。

    3)、通过git init命令把这个目录变成Git可以管理的仓库:

    $ git init
    Initialized empty Git repository in D:/JavaWorkSoftware/git/learngit/.git/
    

    好了,你的Git就算是安装完成了。然后就开始使用吧。

    4)、我们写一个文件到仓库中
    创建一个example.txt

    This is an example file,
    This is creat in Git.
    

    (2).添加文件进去,用git add命令

    $ git add example.txt
    warning: LF will be replaced by CRLF in example.txt.
    The file will have its original line endings in your working directory
    

    这个命令一般是不会有任何提示的,但是我出现了如上提示。
    意思大概是:LF(换行,Line Feed)将会被CRLF(回车换行,CarriageReturn)替代
    该文件将在工作目录中具有其原始行尾。
    报这个警告时是由于文件夹远程不存在,但是不影响提交。我们再执行一次就好。

    (3).提交git commit

    $ git commit -m "first example file"
    [master (root-commit) 0cecffa] first example file
    1 file changed, 2 insertions(+)
    create mode 100644 example.txt
    

    这个commit命令的参数 -m 是必不可少的,就是为了添加本次提交文件的日志,相当于做一个标记,以便于我们在版本控制时用到。

    tips:在git中只有存纯文本文件,如.txt .java .md 不能存其他文件,同时,这些纯文本文件要是UTF-8编码
    

    二、版本控制
    以本地仓库为例,讲解版本控制

    1)、我们先讲几个概念
    工作区和暂存区

    工作区(Working Directory),就是是在我们电脑上能看到的文件目录,这里我的那个/learngit就是一个工作区,在工作区中有很多的分支。

    分支(Branching),顾名思义,就是类似于树叶的分支。我们将整个工作区比作主树干,分支的概念就更好地理解了。我们现在就是在Git的主分支(master)上操作,后边会讲到分支管理。

    我们之前用mkdir learngit 命令创建的那个就是一个工作区,在工作区中有一个隐藏目录.git,这个就是Git的版本库(Repository)

    暂存区,就是我们当前正在操作的目录,但是内容还没有保存在工作区中

    2)、Git下修改文件

    我们把在Git下操作文件和如何把大象装进冰箱中做类比(这是我本人很喜欢的一个学习方法):

    (1).我们修改当前工作区下的 example.txt 文件 (准备大象,肯定有人会问,不用准备冰箱吗?哈哈,Git就是这个冰箱了).

    This is an example file,
    This is creat in Git.
    This is third line.
    

    (2).每二步肯定就是将大象装进冰箱,我们用git add命令

    但在把大象装进冰箱前,我们先学习几个小命令
    git status 查看当前git 的状态

    $git status
    On branch master
    Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git checkout -- <file>..." to discard changes in working directory)
    
    modified: example.txt
    
    no changes added to commit (use "git add" and/or "git commit -a") 
    

    修改了example.txt 文件
    已经修改,但是这个修改没有提交到时工作区,提示你用git add 或git commit

    另一个命令就是 git diff用来查看修改前后两个文件的不同,这个就不做演示了。
    还有一个git checkout -- [文件名] ,撤销修改,也就是说这个时候我们突然不想把大象装进冰箱了,我们只需要撤销就好了。

    $git checkout -- example.txt
    

    这个命令执行完毕后不会有任何提示。 我们接着用git status 查看下我们的git状态,就是说我们的这个git没有任何要commit的文件

    $ git status
    On branch master
    nothing to commit, working tree clean
    

    (3).好了,学完这几个小插曲后,我们继续我们的大象装进冰箱(修改库中文件)的最后一步,就是关闭冰箱门(git commit -m)。
    我们刚刚执行了撤销修改的操作,我们要重新来一次。

    This is an example file,
    This is creat in Git.
    This is third line.
    

    $git add example.txt
    $git commit -m "add third line"

    3)、版本更换:真正的在多个版本之前切换
    为了文件测试,我们多修改几次这个文件,也就是多做几个版本(此操作略,无非就是vim——add——commit)。
    在你修改了好几个版本之后,我们用git log命令来查看当前工作区(其实就是分支)状态.

    $ git log
    commit 7b0b4f5c4c9ffc053db65ed0d7835ae62a27e0b6 (HEAD -> master)
    Author: Lyn4ever <Lyn4ever29@163.com>
    Date: Fri Jun 7 00:23:54 2019 +0800
    
    add fourth line
    
    commit 11b13721088ea653042af7986e0d5b1f1b29a9e9
    Author: Lyn4ever <Lyn4ever29@163.com>
    Date: Fri Jun 7 00:16:12 2019 +0800
    
    add third line
    
    commit 0cecffad8b547533728d2c1a9bef581c6c01f359
    Author: Lyn4ever <Lyn4ever29@163.com>
    Date: Thu Jun 6 23:45:24 2019 +0800
    
    first example file
    
    

    这个很容易看明白的,就是现在有三个版本,这是一个倒序排序,也就是说,当前这个文件的版本就是最上边的这个。commit 后边跟着的那个就是版本号,紧接着两行是作者和时间,再下边就是我们在commit -m 时写的日志了。(注意:你的文件id 肯定和我的不一样,在测试时要以你的为准)
    修改版本的命令是git reset --hard commit_id,我们就来试一试。
    先确保当前git中没有未commit的文件,然后查看文件内容

    $ git status
    On branch master
    nothing to commit, working tree clean
    
    $ cat example.txt
    This is an example file,
    This is creat in Git.
    this is third line.
    this is forth line.
    

    好了,我们就返回日志为"add third line"这个版本,其版本id为:11b13721088ea653042af7986e0d5b1f1b29a9e9

    $git reset --hard 11b13721088ea653042af7986e0d5b1f1b29a9e9
    HEAD is now at 11b1372 add third line
    

    看提示,很明显,就是经成功了。我们再看一个

    $ git status
    On branch master
    nothing to commit, working tree clean
    
    $ cat example.txt
    This is an example file,
    This is creat in Git.
    this is third line.
    

    每次要写这个id是不是很麻烦呢,我们可以只用前7个字符,但是还有一个更简单的方法,就是用HEAD^来代替。HEAD 表示当前版本,往前几个版本,就写几个^。上一个版本就是HEAD^,上上一个就是HEAD^^

    这时,我们再看一个我们的git log

    $ git log
    commit 11b13721088ea653042af7986e0d5b1f1b29a9e9 (HEAD -> master)
    Author: Lyn4ever <Lyn4ever29@163.com>
    Date: Fri Jun 7 00:16:12 2019 +0800
    
    add third line
    
    commit 0cecffad8b547533728d2c1a9bef581c6c01f359
    Author: Lyn4ever <Lyn4ever29@163.com>
    Date: Thu Jun 6 23:45:24 2019 +0800
    
    first example file
    

    发现这个里边只有两个了,我想回到add fourth line 那个版本,也是用同样的方法,但是我找不到commit-id 怎么办?不急,git这么强大的功能不会让我们失望的。我们可以用git reflog查看git版本控制的操作历史

    $ git reflog
    11b1372 (HEAD -> master) HEAD@{0}: reset: moving to 11b13721088ea653042af7986e0d5b1f1b29a9e9
    7b0b4f5 HEAD@{1}: commit: add fourth line
    11b1372 (HEAD -> master) HEAD@{2}: commit: add third line
    0cecffa HEAD@{3}: commit (initial): first example file
    

    这时,我们找到了之前add fourth line 版本的id,就可以了。

    $git reset --hard 7b0b4f5
    

    就这样好了。git log 又发现了三个版本

    $ git log
    commit 7b0b4f5c4c9ffc053db65ed0d7835ae62a27e0b6 (HEAD -> master)
    Author: Lyn4ever <Lyn4ever29@163.com>
    Date: Fri Jun 7 00:23:54 2019 +0800
    
    add fourth line
    
    commit 11b13721088ea653042af7986e0d5b1f1b29a9e9
    Author: Lyn4ever <Lyn4ever29@163.com>
    Date: Fri Jun 7 00:16:12 2019 +0800
    
    add third line
    
    commit 0cecffad8b547533728d2c1a9bef581c6c01f359
    Author: Lyn4ever <Lyn4ever29@163.com>
    Date: Thu Jun 6 23:45:24 2019 +0800
    
    first example file
    
    

    4)、删除文件
    删除文件,其实就是另一种修改操作,和修改是一样的,先用rm file来删除,然后用git commit来提交到工作空间中。

    $rm example.txt
    

    然后我我们看一下状态

    $ git status
    On branch master
    Changes not staged for commit:
    (use "git add/rm <file>..." to update what will be committed)
    (use "git checkout -- <file>..." to discard changes in working directory)
    
    deleted: example.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    发现是deleted ,但是没有commit。也就是已经把大象从冰箱中取出来了,但是没有关冰箱门。所以我们关闭冰箱门
    先从库中删除

    $ git rm example.txt
    rm 'example.txt'
    

    然后提交

    git commit -m "delete example"
    

    然后,想查看这个文件,发现不在了。

    $ cat example.txt
    cat: example.txt: No such file or directory
    

    最后,我们用之前的版本回退回去。

    $ git reset --hard 7b0b4f5
    HEAD is now at 7b0b4f5 add fourth line
    
    $ cat example.txt
    This is an example file,
    This is creat in Git.
    this is third line.
    this is forth line.
    

    tips: 删除文件同样也是修改操作,也可以在rm file后,但还没有git rmgit commit到git库中前,可以使用git checkout -- fileaname 撤销

  • 相关阅读:
    HAProxy、Keepalived 在 Ocatvia 的应用实现与分析
    Octavia 的 HTTPS 与自建、签发 CA 证书
    Octavia 创建 loadbalancer 的实现与分析
    OpenStack Rally 质量评估与自动化测试利器
    自建 CA 中心并签发 CA 证书
    Failed building wheel for netifaces
    通过 vSphere WS API 获取 vCenter Datastore Provisioned Space 置备空间
    OpenStack Placement Project
    我们建了一个 Golang 硬核技术交流群(内含视频福利)
    没有图形界面的软件有什么用?
  • 原文地址:https://www.cnblogs.com/Lyn4ever/p/10987444.html
Copyright © 2011-2022 走看看