git介绍以及常用命令操作

一、git与SVN的对比【面试

  ①git是分布式的,SVN是集中式的(最核心)

  ②git是每个历史版本都存储完整的文件,便于恢复,SVN是存储差异文件,历史版本不可恢复(核心)

  ③git可离线完成大部分操作,SVN则不能

  ④git有着更优雅的分支和合并实现。

  ⑤git有着更撤销修改和修改历史版本的能力。

  ⑥git速度更快,效率更高。

  基于以上几点,git有了很明显的优势,特别是他具有本地的仓库。

二、git的几个概念【面试

  ①工作目录:工作目录是对项目的某个版本独立提取出来的内容。这些从 Git 仓库的压缩数据库中提取出来的文件,放在磁盘上供你使用或修改。

  ②暂存区域:是一个文件,保存了下次将提交的文件列表信息,一般在 Git 仓库目录中。有时候也被称作`‘索引’’,不过一般说法还是叫暂存区域。

  ③仓库工作目录:是Git 用来保存项目的元数据和对象数据库的地方。这是 Git 中最重要的部分,从其它计算机克隆仓库时,拷贝的就是这里的数据。

三、git工作流程

  在工作目录中修改文件 > 暂存文件,将文件的快照放入暂存区域 > 提交更新,找到暂存区域的文件,将快照永久性存储到 Git 仓库目录。(

如果 Git 目录中保存着的特定版本文件,就属于已提交状态。如果作了修改并已放入暂存区域,就属于已暂存状态。如果自上次取出后,作了修改但还没有放到暂存区域,就是已修改状态。)

四、安装(yum安装与编译安装)

yum安装: yum install -y git

编译安装:http://xin.kendd.cn/?p=253

五、git常用操作

  git add      添加文件至缓存区域

  git branch      查看分支和创建分支  git branch jam(创建jam分支)

  git checkout     进行撤销也可以进行分支切换  git checkout jam(切换jam分支)

  git clone        克隆远程主机仓库

  git commit     把暂存区的文件提交到仓库中

  git init           初始化目录(工作目录)

  git merge      合并分支

  git pull        拉取远程主机的仓库

  git push     把本地仓库推送到远程主机

  git reset     撤销

  git log        查看所有仓库

  git status     查看git目录中文件的状态

六、git常用演示

  ①git使用演示   

[root@localhost ~]# mkdir jam          #创建目录jam
[root@localhost ~]# cd jam               #切换到jam目录下
[root@localhost jam]# git init            #初始化目录为git工作目录
Initialized empty Git repository in /root/jam/.git/
[root@localhost jam]# touch test       #创建文件
[root@localhost jam]# echo 'hello,world' >> test    #写入内容
[root@localhost jam]# git add .         #提交当前目录下的所有文件
[root@localhost jam]# git commit -m v1
[master (root-commit) 79ca1b2] v1
 1 file changed, 1 insertion(+)
 create mode 100644 test
[root@localhost jam]# git status        #查看git目录下的文件状态
# On branch master
nothing to commit, working directory clean
[root@localhost jam]# git log            #查看所有本地仓库
commit 79ca1b2e870763dd017978abb75d9985322bd5c1
Author: Your Name <[email protected]>
Date:   Thu May 23 09:32:45 2019 -0400

    v1      
[root@localhost jam]# echo 'jamhisao' >> test #再次写入内容
[root@localhost jam]# git add . #提交
[root@localhost jam]# git commit -m v2
[master f62e8df] v2
 1 file changed, 1 insertion(+)
[root@localhost jam]# git log #查看仓库
commit f62e8df90d59dfc3424ac38da4b5d4b07b2ea82b
Author: Your Name <[email protected]>
Date:   Thu May 23 09:37:52 2019 -0400

    v2

commit 79ca1b2e870763dd017978abb75d9985322bd5c1
Author: Your Name <[email protected]>
Date:   Thu May 23 09:32:45 2019 -0400

    v1
[root@localhost jam]# git reset --hard 79ca1b2e870 #回滚到v1
HEAD is now at 79ca1b2 v1
[root@localhost jam]# cat test
hello,world

  ②撤销工作区的内容

[root@localhost jam]# git status         #查看当前文件状态
# On branch master
nothing to commit, working directory clean
[root@localhost jam]# echo 'now jam is here beijing' >> test [root@localhost jam]# cat test #编辑当前文件并查看 hello,world now jam is here beijing [root@localhost jam]# git add . #提交 [root@localhost jam]# git commit -m v2 [master 25b8377] v2 1 file changed, 1 insertion(+) [root@localhost jam]# git log #查看仓库 commit 25b8377377722043fa4dd9b79c0223bb036c51b6 Author: Your Name <[email protected]> Date: Thu May 23 20:22:14 2019 -0400 v2 commit 79ca1b2e870763dd017978abb75d9985322bd5c1 Author: Your Name <[email protected]> Date: Thu May 23 09:32:45 2019 -0400 v1 [root@localhost jam]# git reset --hard 79ca1b2e870763dd #撤销工作内容 HEAD is now at 79ca1b2 v1 [root@localhost jam]# git log commit 79ca1b2e870763dd017978abb75d9985322bd5c1 Author: Your Name <[email protected]> Date: Thu May 23 09:32:45 2019 -0400 v1 [root@localhost jam]# git status #查看当前文件状态 # On branch master nothing to commit, working directory clean

  ③撤销暂存区文件(每执行一步都得查看状态)

[root@localhost jam]# cat test   #查看初始文件内容,并查看文件状态
hello,world
jam jam jam hahahha
[root@localhost jam]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost jam]# echo 'hahahahahahahaha' >> test          #编辑文件并查看状态
[root@localhost jam]# 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:   test
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost jam]# git add .            #提交文件并查看状态
[root@localhost jam]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   test
#
[root@localhost jam]# git reset HEAD test       #回撤到工作区并查看状态
Unstaged changes after reset:
M    test
[root@localhost jam]# 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:   test
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost jam]# git checkout -- test            #撤销工作区的文件并查看状态
[root@localhost jam]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost jam]# cat test          #查看文件内容
hello,world
jam jam jam hahahha

  ④回滚到任意版本的操作演示

[root@localhost jam]# git reflog                #查看本地所有仓库
5d0fe77 HEAD@{0}: reset: moving to 5d0fe77
f2dd664 HEAD@{1}: commit: v4
5d0fe77 HEAD@{2}: commit: v3
79ca1b2 HEAD@{3}: reset: moving to 79ca1b2e870763dd
25b8377 HEAD@{4}: commit: v2
79ca1b2 HEAD@{5}: reset: moving to 79ca1b2e870
f62e8df HEAD@{6}: commit: v2
79ca1b2 HEAD@{7}: commit (initial): v1
[root@localhost jam]# echo '1' >> test           #提交多个仓库
[root@localhost jam]# git add .
[root@localhost jam]# git commit -m v6
[master 9e7c573] v6
 1 file changed, 1 insertion(+)
[root@localhost jam]# echo '2' >> test
[root@localhost jam]# git add .
[root@localhost jam]# git commit -m v7
[master f1591dc] v7
 1 file changed, 1 insertion(+)
[root@localhost jam]# git log                   #查看本地所有仓库
commit f1591dcf316e51a5c7d6bef3df8e58791e65beaa
Author: Your Name <[email protected]>
Date:   Thu May 23 20:50:53 2019 -0400

    v7

commit 9e7c573a8c8cdd48df4383686d4e5b67ebd57477
Author: Your Name <[email protected]>
Date:   Thu May 23 20:50:27 2019 -0400

    v6

commit 5d0fe77ad291a1d832ba8dd6f5c4fd665199294c
Author: Your Name <[email protected]>
Date:   Thu May 23 20:29:45 2019 -0400

    v3

commit 79ca1b2e870763dd017978abb75d9985322bd5c1
Author: Your Name <[email protected]>
Date:   Thu May 23 09:32:45 2019 -0400

    v1
...skipping...
[root@localhost jam]# git reset --hard 5d0fe77ad291a1 #回滚到v3仓库
HEAD is now at 5d0fe77 v3
[root@localhost jam]# git log #查看本地所有仓库
commit 5d0fe77ad291a1d832ba8dd6f5c4fd665199294c
Author: Your Name <[email protected]>
Date:   Thu May 23 20:29:45 2019 -0400

    v3

commit 79ca1b2e870763dd017978abb75d9985322bd5c1
Author: Your Name <[email protected]>
Date:   Thu May 23 09:32:45 2019 -0400

    v1
[root@localhost jam]# git reflog #查看历史仓库的信息
5d0fe77 HEAD@{0}: reset: moving to 5d0fe77ad291a1
f1591dc HEAD@{1}: commit: v7
9e7c573 HEAD@{2}: commit: v6
5d0fe77 HEAD@{3}: reset: moving to 5d0fe77
f2dd664 HEAD@{4}: commit: v4
5d0fe77 HEAD@{5}: commit: v3
79ca1b2 HEAD@{6}: reset: moving to 79ca1b2e870763dd
25b8377 HEAD@{7}: commit: v2
79ca1b2 HEAD@{8}: reset: moving to 79ca1b2e870
f62e8df HEAD@{9}: commit: v2
79ca1b2 HEAD@{10}: commit (initial): v1
[root@localhost jam]# git reset --hard f2dd664 #回滚到历史v4
HEAD is now at f2dd664 v4
[root@localhost jam]# git log
commit f2dd6646c3e711f8d81ce4cd00e90b6d3babff70
Author: Your Name <[email protected]>
Date:   Thu May 23 20:31:27 2019 -0400

    v4

commit 5d0fe77ad291a1d832ba8dd6f5c4fd665199294c
Author: Your Name <[email protected]>
Date:   Thu May 23 20:29:45 2019 -0400

    v3

commit 79ca1b2e870763dd017978abb75d9985322bd5c1
Author: Your Name <[email protected]>
Date:   Thu May 23 09:32:45 2019 -0400

    v1

  ⑤分支查看、分支、切换(重点),及其演示操作(分支对主线无影响)

[root@localhost jam]# cat test           
hello,world
[root@localhost jam]# git branch                #查看分支
* master
[root@localhost jam]# git branch jam             #创建jam分支
[root@localhost jam]# git branch                  
  jam
* master
[root@localhost jam]# git checkout jam            #切换分支jam
Switched to branch 'jam'
[root@localhost jam]# git branch
* jam
  master
[root@localhost jam]# echo '3' >> test       编辑文件并提交查看
[root@localhost jam]# git add .
[root@localhost jam]# git commit -m v8
[jam 6178165] v8
 1 file changed, 1 insertion(+)
[root@localhost jam]# cat test
hello,world
3
[root@localhost jam]# git checkout master          #切换分支master
Switched to branch 'master'
[root@localhost jam]# cat test                   #查看文件内容
hello,world
[root@localhost jam]# git merge jam               #合并分支(master和jam)
Updating 79ca1b2..6178165
Fast-forward
 test | 1 +
 1 file changed, 1 insertion(+)
[root@localhost jam]# cat test                  #查看文件发现jam分支的内容已合并到master
hello,world
3

猜你喜欢

转载自www.cnblogs.com/daisyyang/p/10916109.html