美文网首页
git基本操作

git基本操作

作者: warmsirius | 来源:发表于2019-07-31 14:15 被阅读0次

1. git历史和特点

1.1 git历史

略(有兴趣自行百度)

git两大特点

  • 版本控制:可以解决 多人同时开发的代码问题,也可以解决找回历史代码的问题
  • 分布式:Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上。(分布式: 中央服务器挂掉,还可以进行coding;集中式: 中央服务器挂掉,不可以coding)
分布式和集中式

2. 安装与配置

(1) 安装命令如下:

  • ubuntu
sudo apt-get install git
  • linux
yum install git
  • mac
    Mac自带git

(2) 安装后配置git的本地邮箱和用户名

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

3. 创建一个版本库

(1) 新建一个目录 git_test,在git_test 目录下创建一个版本库,命令如下:

[root@iZq6rpcx9fo0pxZ /]# mkdir git_test
[root@iZq6rpcx9fo0pxZ /]# cd git_test
[root@iZq6rpcx9fo0pxZ git_test]# git init
初始化空的 Git 版本库于 /git_test/.git/

初始化后,查看git_test下的文件:

[root@iZq6rpcx9fo0pxZ git_test]# ls -al
总用量 12
drwxr-xr-x   3 root root 4096 7月  31 13:27 .
dr-xr-xr-x. 22 root root 4096 7月  31 13:27 ..
drwxr-xr-x   7 root root 4096 7月  31 13:27 .git

4. 版本创建与回退

4.1 使用

(1) 在git_test目录下创建一个文件code.txt,编辑内容如下:

[root@iZq6rpcx9fo0pxZ git_test]# touch code.txt
[root@iZq6rpcx9fo0pxZ git_test]# vi code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line

(2) 使用如下2条命令可以创建一个版本

创建版本命令

git add 文件/目录
git commit -m '版本信息'

提交如下命令:

[root@iZq6rpcx9fo0pxZ git_test]# git add code.txt
[root@iZq6rpcx9fo0pxZ git_test]# git commit -m '版本1'
[master(根提交) 5d3efee] 版本1
 1 file changed, 1 insertion(+)
 create mode 100644 code.txt

(3) 查看版本记录

  • a. 查看版本记录命令
git log

执行如下:

[root@iZq6rpcx9fo0pxZ git_test]# git log
commit 5d3efeec4e438f5a2e0ebff799e2cf2c16c32bbc
Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 13:31:30 2019 +0800

    版本1
  • b. 将版本记录在一条展示
git log --pretty=oneline

执行如下

[root@iZq6rpcx9fo0pxZ git_test]# git log --pretty=oneline
5d3efeec4e438f5a2e0ebff799e2cf2c16c32bbc 版本1
  • c. 将版本记录用图标展示
git log --graph --pretty=oneline

执行如下:

[root@iZq6rpcx9fo0pxZ git_test]# git log --graph --pretty=oneline
* 5d3efeec4e438f5a2e0ebff799e2cf2c16c32bbc 版本1

(4) 继续编辑code.txt,在里面增加一行

[root@iZq6rpcx9fo0pxZ git_test]# vi code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line

(5) 使用如下命令再创建一个版本,并查看版本记录

[root@iZq6rpcx9fo0pxZ git_test]# git add code.txt
[root@iZq6rpcx9fo0pxZ git_test]# git commit -m '版本2'
[master bdcd815] 版本2
 1 file changed, 1 insertion(+)

[root@iZq6rpcx9fo0pxZ git_test]# git log
commit bdcd8152c0072afbded59d5f20afc299aac122ed
Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 13:36:17 2019 +0800

    版本2

commit 5d3efeec4e438f5a2e0ebff799e2cf2c16c32bbc
Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 13:31:30 2019 +0800

    版本1

(6) 现在想回到某一个版本,可以使用如下命令:

git reset --hard HEAD^
  • HEAD: 当前最新版本
  • HEAD^: 当前版本的前一个版本,相当于HEAD~1
  • HEAD^^: 当前版本的前前个版本,相当于HEAD~2
  • HEAD~100: 当前版本的前100版本
[root@iZq6rpcx9fo0pxZ git_test]# git reset --hard HEAD^
HEAD 现在位于 5d3efee 版本1
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line

执行命令后使用git log查看版本记录,发现现在只能看到版本1的记录, cat code.txt 查看文件内容,现在只有一行,也就是第一个版本中code.txt的内容

(7) 假如我们现在又想回到版本2,怎么办?

可以使用如下命令:

git reset --hard 版本号

从上面可以看到版本2的版本号为bdcd8152c0072afbded59d5f20afc299aac122ed

版本号不一定全部复制,复制前几位即可

(8) 在终端执行如下命令:

[root@iZq6rpcx9fo0pxZ git_test]# git reset --hard bdcd8152
HEAD 现在位于 bdcd815 版本2

现在发现版本2又回来了。可以cat code.txt 查看其里面的内容如下

[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line

(9) 假如说上面的终端已经关了该怎么回退版本。

下面把终端关了,然后再打开终端,发现之前版本2的版本号看不到了。

[root@iZq6rpcx9fo0pxZ git_test]# git log
commit 5d3efeec4e438f5a2e0ebff799e2cf2c16c32bbc
Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 13:31:30 2019 +0800

    版本1

那么怎么再回到版本2呢?git reflog 命令可以查看我们的操作记录

git reflog

执行如下:

[root@iZq6rpcx9fo0pxZ git_test]# git reflog
5d3efee HEAD@{0}: reset: moving to 5d3ef
bdcd815 HEAD@{1}: reset: moving to bdcd8152
5d3efee HEAD@{2}: reset: moving to HEAD^
bdcd815 HEAD@{3}: commit: 版本2
5d3efee HEAD@{4}: commit (initial): 版本1
[root@iZq6rpcx9fo0pxZ git_test]# git reset --hard bdcd8
HEAD 现在位于 bdcd815 版本2

4.2 工作区和暂存区

4.2.1 工作区(Working Directory)

电脑中的目录,比如我们的git_test,就是一个工作区。

4.2.2 版本库(Repository)

工作区有一个隐藏目录.git,这个不是工作区,而是git的版本库。

git 的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

因为我们创建git版本库时,git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。

可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

git流程
  • 前面把文件往git版本库里添加的时候是分两步执行:
    • 第一步: 用 git add 把文件添加进去,实际上就是把文件修改添加到暂存区
    • 第二步: 用 git commit 提交修改,实际上就是把暂存区的所有内容提交到当前分支
举例

(1) 下面在git_test目录下再创建一个文件code2.txt,然后编辑内容如下

[root@iZq6rpcx9fo0pxZ git_test]# touch code2.txt
[root@iZq6rpcx9fo0pxZ git_test]# vi code2.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code2.txt
the code2 first line

(2) 然后再次编辑code2.txt内容,在其中加入2行,编辑后内容如下

[root@iZq6rpcx9fo0pxZ git_test]# vi code2.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code2.txt
the code2 first line
the code2 second line
the code2 third line

(3) 使用如下命令查看工作的状态

git status

执行后如下:

[root@iZq6rpcx9fo0pxZ git_test]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#   code2.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
工作区和暂存区

4.3 版本库(Repository)

git管理的文件修改,它只会提交暂存区的修改来创建版本

(1) 编辑code.txt,并使用git add命令将其添加到暂存区中

[root@iZq6rpcx9fo0pxZ git_test]# vi code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
[root@iZq6rpcx9fo0pxZ git_test]# git add code.txt

(2) 继续编辑code.txt, 并在其中添加一行

[root@iZq6rpcx9fo0pxZ git_test]# vi code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line

(3) git commit 创建一个版本,并使用 git status 查看,发现第二次修改code.txt 内容之后,并没有将其添加的工作区,所以创建版本的时候并没有被提交

[root@iZq6rpcx9fo0pxZ git_test]# git commit -m '版本3'
[master dd22753] 版本3
 1 file changed, 1 insertion(+)
[root@iZq6rpcx9fo0pxZ git_test]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#   修改:      code.txt
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#   code2.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line

4.4 撤销修改

(1) 继续上面的操作,提示我们可以使用 git checkout --<文件> 来丢弃工作区的改动。执行如下命令,发现工作区干净了,第二次的改动内容也没有了。

  • 修改文件后但是文件修改并未提交到暂存区
git checkout -- <文件名>

执行如下:

[root@iZq6rpcx9fo0pxZ git_test]# git checkout -- code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line
this is the third line

(2) 继续编辑code.txt,并在其中添加如下内容,并将其添加到暂存区,如果需要取消修改,查看git status:

[root@iZq6rpcx9fo0pxZ git_test]# vi code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
add one more line
[root@iZq6rpcx9fo0pxZ git_test]# git add code.txt
[root@iZq6rpcx9fo0pxZ git_test]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#   修改:      code.txt
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#   code2.txt
  • 放在暂存区的文件需要还原修改命令
git reset HEAD 文件名
git checkout -- 文件名

执行如下:

[root@iZq6rpcx9fo0pxZ git_test]# git reset HEAD code.txt
重置后撤出暂存区的变更:
M   code.txt
[root@iZq6rpcx9fo0pxZ git_test]# git checkout -- code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line
this is the third line

4.5 对比文件的不同

对比工作区和某个版本中文件的不同

(1) 继续编辑文件code.txt, 在其添加一行内容

[root@iZq6rpcx9fo0pxZ git_test]# vi code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
the new line

(2) 现在要对比工作区中的code.txt和HEAD版本中的code.txt的不同,使用如下命令

git diff HEAD --文件名

执行结果如下:

[root@iZq6rpcx9fo0pxZ git_test]# git diff HEAD -- code.txt
diff --git a/code.txt b/code.txt
index 01e1274..7696b1c 100644
--- a/code.txt
+++ b/code.txt
@@ -1,3 +1,4 @@
 this is the first line
 this is the second line
 this is the third line
+the new line
image.png

(3) 使用如下命令丢弃工作区的改动

[root@iZq6rpcx9fo0pxZ git_test]# git checkout -- code.txt
[root@iZq6rpcx9fo0pxZ git_test]# git status
# 位于分支 master
无文件要提交,干净的工作区

对比两个版本间文件的不同:

(1) 现在要对比HEAD 和 HEAD^ 版本中 code.txt 的不同,使用如下命令:

git diff HEAD HEAD^ -- 文件名
  • -: 对应第一个版本
  • +: 对应第二个版本
[root@iZq6rpcx9fo0pxZ git_test]# vi code.txt
[root@iZq6rpcx9fo0pxZ git_test]# cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
[root@iZq6rpcx9fo0pxZ git_test]# git add code.txt
[root@iZq6rpcx9fo0pxZ git_test]# git commit -m '新增第4行'
[master eda8050] 新增第4行
 1 file changed, 1 insertion(+)
[root@iZq6rpcx9fo0pxZ git_test]# git diff HEAD HEAD^ -- code.txt
diff --git a/code.txt b/code.txt
index 66f9219..01e1274 100644
--- a/code.txt
+++ b/code.txt
@@ -1,4 +1,3 @@
 this is the first line
 this is the second line
 this is the third line
-this is the forth line
image.png

4.6 删除文件

(1) 我们把目录中的code2.txt删除

[root@iZq6rpcx9fo0pxZ git_test]# ls
code2.txt  code.txt
[root@iZq6rpcx9fo0pxZ git_test]# rm code2.txt
rm:是否删除普通文件 "code2.txt"?y
[root@iZq6rpcx9fo0pxZ git_test]# ls
code.txt

这个时候,git知道删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻提示哪些文件被删除了。

[root@iZq6rpcx9fo0pxZ git_test]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#   删除:      code2.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
  • 丢弃工作区的改动(工作区删除文件复原)
git checkout --文件名
[root@iZq6rpcx9fo0pxZ git_test]# git checkout -- code2.txt
[root@iZq6rpcx9fo0pxZ git_test]# ls
code2.txt  code.txt
  • 更新要提交的内容(git同步删除)
git add/rm 文件名
[root@iZq6rpcx9fo0pxZ git_test]# rm code2.txt
rm:是否删除普通文件 "code2.txt"?y
[root@iZq6rpcx9fo0pxZ git_test]# git rm code2.txt
rm 'code2.txt'
[root@iZq6rpcx9fo0pxZ git_test]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#   删除:      code2.txt
#
  • 查看版本信息
    git log 如果版本信息过多,按q退出
[root@iZq6rpcx9fo0pxZ git_test]# git log
commit c75a88ee04a0e104774b4eecef14a6ab72e2108c
Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 14:08:16 2019 +0800

    删除code2.txt

Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 14:08:16 2019 +0800

    删除code2.txt

commit eda8050db6be11b6a8563fafd36f6d843ced2067
Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 14:04:27 2019 +0800

    新增第4行

commit 3f87b4c09101017b4c08022010324f70e6ff63dd
Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 14:00:15 2019 +0800

    新增code2文件

commit dd227534061d3ffdf17628bcd5b8afaa5de10463
Author: warmsirius <2859626066@qq.com>
Date:   Wed Jul 31 13:52:42 2019 +0800

    版本3

也可以使用 git log --pretty=oneline 以简短的形式展示

(2) 现在你有两个选择:

一是确实要从版本库中删除该文件,那就用命令git rm删除掉,并且git commit

[root@iZq6rpcx9fo0pxZ git_test]# git rm code2.txt
rm 'code2.txt'
[root@iZq6rpcx9fo0pxZ git_test]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#   删除:      code2.txt
#
[root@iZq6rpcx9fo0pxZ git_test]# git commit -m '删除code2.txt'
[master c75a88e] 删除code2.txt
 1 file changed, 3 deletions(-)
 delete mode 100644 code2.txt
[root@iZq6rpcx9fo0pxZ git_test]# git status
# 位于分支 master
无文件要提交,干净的工作区

另一种情况是删错了,可以直接使用git checkout -- code2.txt,这样文件code2.txt又回来了,如果已经提交到了暂存区,需要先git reset HEAD 文件名,然后再git checkout -- 文件名

小结

命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。

Git基本操作思维导图

git基本操作

相关文章

  • Git命令梳理

    基本操作 git init git add . git stauts git commit -m '描述' git...

  • git基本操作 梳理

    git操作梳理一、git基本操作1.创建版本仓库 git init(.git目录)2.版本创建 git add ...

  • GitFlow

    假设你已经熟悉git的基本操作:如不熟悉请参考:GitBook 这里简单回顾一下git基本操作 提交基本操作 把...

  • git基本操作二:git的基本操作

    本文会带各种命令后的图,与大部分文章区分,只有理论,没有实践 初始化本地库 命令:git init 效果: ,gi...

  • Git Cocopods 基本操作

    Git 操作 基本操作 查看当前git状态git status 添加代码到暂缓区git add . 提交代码到本地...

  • git常用操作 🎀

    git常用操作 ? 基本知识 查看git信息 修改git配置 提交类操作 分支类操作 创建分支 查看分支 修改分支...

  • git基本操作

    重要概念 已提交(mommitted)该文件已经被安全地保存在本地数据库中了 已修改(modified)修改了某个...

  • Git基本操作

    前言 Git相关操作总结 2018-7-14, 联创团队分享 文中部分图片见文末参考链接 正文 一. 基本概念 版...

  • git基本操作

    0x001 git基本命令 0x002 远程仓库配置(coding.net) 使用命令ssh-keygen -t ...

  • Git基本操作

    创建新的git仓库 git init # 初始化 git add readme.md # 添加新文件 git co...

网友评论

      本文标题:git基本操作

      本文链接:https://www.haomeiwen.com/subject/rjwnrctx.html