Git的安装和使用

一、git的介绍

git一个分布式的版本管理工具,git和svn不同之处在于git不需要依赖服务端就可以工作,即git是分布式的。
在这里插入图片描述在这里插入图片描述

从上面两个图可以看出,使用svn的版本变化数据都是存在于服务端的,最终控制中心只有一个。而git,每一个客户端都可以作为一个版本管理中心,当然它也需要有一个公共的总控制点。

二、git的安装和配置

1、git的安装:

[root@localhost ~]# yum install -y git

查看git的版本:

[root@localhost ~]# git --version
git version 1.8.3.1

git配置用户信息并查看:

[root@localhost ~]# git config --global user.name "zjin"
[root@localhost ~]# git config --global user.email "akipa11@163.com"
[root@localhost ~]# git config --list
user.name=zjin
user.email=akipa11@163.com

这些配置信息都保存在文件~/.gitconfig里面(我们也可以直接修改这个文件来配置):

[user]
        name = zjin
        email = akipa11@163.com

2、git的初始化

新建一个目录,用作git的版本库:

[root@localhost ~]# mkdir /data/gitroot
[root@localhost ~]# cd !$
cd /data/gitroot
[root@localhost /data/gitroot]# git init
Initialized empty Git repository in /data/gitroot/.git/
[root@localhost /data/gitroot]# ll -a
total 0
drwxr-xr-x  3 root root  18 Dec 12 13:29 .
drwxr-xr-x. 5 root root  47 Dec 12 13:29 ..
drwxr-xr-x  7 root root 119 Dec 12 13:29 .git

初始化仓库用命令git init,可以看到/data/gitroot目录下创建了一个隐藏文件.git,我们打开看这个目录下面有哪些目录和文件(不要随意修改这个目录的内容,否则会破坏这个版本库):

[root@localhost /data/gitroot]# cd .git
[root@localhost /data/gitroot/.git]# ls
branches  config  description  HEAD  hooks  info  objects  refs

三、git工作区、暂存区和版本库的分别

1、工作区:

其实就是我们在电脑上能所到的目录。比如上面初始化后的仓库目录/data/gitroot。

2、暂存区:

一般存放在 “.git目录下” 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。

3、版本库:

工作区下的隐藏目录,就是git的版本库(/data/gitroot/.git)

下图展示了三者间的关系

在这里插入图片描述
图中左侧为工作区,右侧为版本库。在版本库中标记为 “index” 的区域是暂存区(stage, index),标记为 “master” 的是 master 分支所代表的目录树。

四、git的基本操作

1、把一个文件提交到git仓库,分为两步走,第一步是先git add 文件,第二步是git commit -m “描述信息”:

[root@localhost /data/gitroot]# echo "I'm learning git.">>learn_git.txt
[root@localhost /data/gitroot]# git add learn_git.txt 
[root@localhost /data/gitroot]# git commit -m "add first file"
[master (root-commit) bb49b95] add first file
 1 file changed, 1 insertion(+)
 create mode 100644 learn_git.txt

2、我们可以随时用命令git status来查看git的版本库状态

[root@localhost /data/gitroot]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost /data/gitroot]# echo "hello">>learn_git.txt
[root@localhost /data/gitroot]# 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:   learn_git.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost /data/gitroot]# git add learn_git.txt 
[root@localhost /data/gitroot]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   learn_git.txt
#
[root@localhost /data/gitroot]# git commit -m "ch file"
[master b94e756] ch file
 1 file changed, 1 insertion(+)
[root@localhost /data/gitroot]# git status
# On branch master
nothing to commit, working directory clean

3、git diff file可以对比仓库里的版本file文件和本次file文件修改了哪些内容:

[root@localhost /data/gitroot]# vim learn_git.txt 
[root@localhost /data/gitroot]# git diff learn_git.txt 
diff --git a/learn_git.txt b/learn_git.txt
index 3b61cc6..3fa1a04 100644
--- a/learn_git.txt
+++ b/learn_git.txt
@@ -1,2 +1,2 @@
 I'm learning git.
-hello      /-号,说明我们删除了此行内容
+Git is a distributed version control system. /+号,是我们新增内容

git status也可以看到,我们修改了learn_git.txt这个文件的内容(状态为modified):

[root@localhost /data/gitroot]# 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:   learn_git.txt  /可以看到我们修改了这个文件
#
no changes added to commit (use "git add" and/or "git commit -a")

4、git log 可以查看所有提交的记录

[root@localhost /data/gitroot]# git log
commit b51e5c8d0fffb77a940eb85f8601602d1061fd7c
Author: zjin <akipa11@163.com>
Date:   Thu Dec 12 14:36:07 2019 +0800

    also change file

commit b94e756d6823bd1c8154b155611328e21559d1b8
Author: zjin <akipa11@163.com>
Date:   Thu Dec 12 13:58:42 2019 +0800

    ch file

commit bb49b95e52eb958e072475304d009fa16d730a6a
Author: zjin <akipa11@163.com>
Date:   Thu Dec 12 13:56:34 2019 +0800

    add first file

可以看到每次更改的记录,其中,commit后面的为版本号。

5、git log --pretty=oneline单行显示

[root@localhost /data/gitroot]# git log --pretty=oneline
b51e5c8d0fffb77a940eb85f8601602d1061fd7c also change file
b94e756d6823bd1c8154b155611328e21559d1b8 ch file
bb49b95e52eb958e072475304d009fa16d730a6a add first file

可以看到,git log --pretty=oneline可以更直观的显示我们的更改记录。

6、git reflog可以查看所有的历史版本

[root@localhost /data/gitroot]# git reflog
b51e5c8 HEAD@{0}: commit: also change file
b94e756 HEAD@{1}: commit: ch file
bb49b95 HEAD@{2}: commit (initial): add first file

git reflog命令,可以方便我们回退到历史、未来版本。

7、git reset 可以进行版本回退操作:

[root@localhost /data/gitroot]# git log --pretty=oneline
b51e5c8d0fffb77a940eb85f8601602d1061fd7c also change file
b94e756d6823bd1c8154b155611328e21559d1b8 ch file
bb49b95e52eb958e072475304d009fa16d730a6a add first file
[root@localhost /data/gitroot]# git reset --hard bb49b95e5 /回退到最初版本
HEAD is now at bb49b95 add first file
[root@localhost /data/gitroot]# ls
learn_git.txt
[root@localhost /data/gitroot]# cat learn_git.txt 
I'm learning git.
[root@localhost /data/gitroot]# git reset --hard b51e5c8 /回退到最新版本
HEAD is now at b51e5c8 also change file
[root@localhost /data/gitroot]# cat learn_git.txt 
I'm learning git.
Git is a distributed version control system.
[root@localhost /data/gitroot]# git reset --hard b94e756d /l回退到任一版本
HEAD is now at b94e756 ch file
[root@localhost /data/gitroot]# cat learn_git.txt 
I'm learning git.
hello

可以看到,通过命令git reset --hard 版本号,我们可以任意的进行版本的回退。

8、撤销修改的操作:

  1. 还没有git add之前,直接操作修改即可;或者使用命令git checkout – file(注意,git checkout如果不加–,这个命令就是切换分支的命令了):

    [root@localhost /data/gitroot]# vim learn_git.txt 
    [root@localhost /data/gitroot]# 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:   learn_git.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@localhost /data/gitroot]# git checkout -- learn_git.txt
    [root@localhost /data/gitroot]# cat learn_git.txt 
    I'm learning git.
    hello
    
  2. git add 后,还没git commit之前;

    [root@localhost /data/gitroot]# vim learn_git.txt 
    [root@localhost /data/gitroot]# git add learn_git.txt 
    [root@localhost /data/gitroot]# git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       modified:   learn_git.txt
    #
    
    

    git提示我们,可以使用git reset HEAD 命令把暂存区的修改撤销掉,重新放回到工作区:

    [root@localhost /data/gitroot]# git reset HEAD learn_git.txt 
    Unstaged changes after reset:
    M       learn_git.txt
    [root@localhost /data/gitroot]# 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:   learn_git.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    

    然后可以用命令git checkout – 丢弃工作区的修改

    [root@localhost /data/gitroot]# git checkout -- learn_git.txt 
    [root@localhost /data/gitroot]# git status
    # On branch master
    nothing to commit, working directory clean
    
  3. git add和git commit后,还没推送到远程仓库之前;

    这个时候,我们可以通过git log查看提交历史,然后使用命令git reset --hard 版本号回退到之前的版本。

  4. 误删了文件,可以用命令git checkout – file恢复

    [root@localhost /data/gitroot]# ls
    learn_git.txt
    [root@localhost /data/gitroot]# rm -f learn_git.txt 
    [root@localhost /data/gitroot]# ls
    [root@localhost /data/gitroot]# git checkout -- learn_git.txt
    [root@localhost /data/gitroot]# ls
    learn_git.txt
    

9、git rm file删除文件(还要git commit 后才算真正删除):

[root@localhost /data/gitroot]# ls
learn_git.txt
[root@localhost /data/gitroot]# git rm learn_git.txt 
rm 'learn_git.txt'
[root@localhost /data/gitroot]# git commit -m "del learn_git.txt"
[master 33962d8] del learn_git.txt
 1 file changed, 2 deletions(-)
 delete mode 100644 learn_git.txt
[root@localhost /data/gitroot]# ls

这时候,用git checkout – 命令是恢复不了文件的,想要将文件恢复回来,需要用git reset --hard 版本号:

[root@localhost /data/gitroot]# git checkout -- learn_git.txt
error: pathspec 'learn_git.txt' did not match any file(s) known to git.
[root@localhost /data/gitroot]# git log --pretty=oneline
33962d864f54c495be72bab9cc869a64d6c9353e del learn_git.txt
b51e5c8d0fffb77a940eb85f8601602d1061fd7c also change file
b94e756d6823bd1c8154b155611328e21559d1b8 ch file
bb49b95e52eb958e072475304d009fa16d730a6a add first file
[root@localhost /data/gitroot]# git reset --hard b51e5c8d
HEAD is now at b51e5c8 also change file
[root@localhost /data/gitroot]# ls
learn_git.txt

可以看到,learn_git.txt文件又回来了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值