一、版本创建与回退

1.git简介

  • git是目前世界上最先进的分布式版本控制系统。
  • 2008年,GitHub网站上线了,它为开源项目免费提供Git存储,无数开源项目开始迁移至GitHub,包括jQuery,PHP,Ruby等等。
  • Git和svn的区别:https://www.cnblogs.com/dazhidacheng/p/7478438.html
  • Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上。
  • 首先找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,
  • 并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交。可以自己搭建这台服务器,也可以使用GitHub网站。
  • 很多Linux系统,自带已经安装了git,查看版本:git --version

2.创建一个版本库

[root@db_server git]# pwd
/data1/git
[root@db_server git]# git init
Initialized empty Git repository in /data1/git/.git/
  • 工作区(Working Directory):/data1/git,就是一个工作区。
  • 版本库(Repository):工作区有一个隐藏目录.git,这个不是工作区,而是git的版本库。
[root@db_server git]# cd .git/
[root@db_server .git]# ll
total 48
drwxr-xr-x  2 root root 4096 Feb 27 14:54 branches
-rw-r--r--  1 root root    8 Feb 27 15:10 COMMIT_EDITMSG
-rw-r--r--  1 root root   92 Feb 27 14:54 config
-rw-r--r--  1 root root   73 Feb 27 14:54 description
-rw-r--r--  1 root root   23 Feb 27 14:54 HEAD
drwxr-xr-x  2 root root 4096 Feb 27 14:54 hooks
-rw-r--r--  1 root root  137 Feb 27 15:17 index
drwxr-xr-x  2 root root 4096 Feb 27 14:54 info
drwxr-xr-x  3 root root 4096 Feb 27 15:07 logs
drwxr-xr-x 10 root root 4096 Feb 27 15:10 objects
-rw-r--r--  1 root root   41 Feb 27 15:17 ORIG_HEAD
drwxr-xr-x  4 root root 4096 Feb 27 14:54 refs
  • git的版本库里存了很多东西,其中最重要的就是称为index(或者叫stage)的暂存区,还有git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

3.版本创建与回退

  • (1)在/data1/git目录下创建一个文件test.go,编辑内容如下:
[root@db_server git]# cat test.go 
111111111111111111111
  • (2)使用如下两条命令可以创建一个版本:
[root@db_server git]# git add test.go
[root@db_server git]# git commit -m '版本1'

*** Please tell me who you are.

Run

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

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@db_server.(none)')
  • (3)添加身份标识(git不做检查)
[root@db_server git]# git config --global user.email "zq9017197@sina.com"
[root@db_server git]# git config --global user.name "ing"
[root@db_server git]# git commit -m '版本1'
[master (root-commit) a57f919] 版本1
 1 file changed, 1 insertion(+)
 create mode 100644 test.go
  • (4)使用如下命令可以查看版本记录:
[root@db_server git]# git log
commit a57f919f12ec7097fce16ad67fa14fc075c94e2b (HEAD -> master)
Author: ing <zq9017197@sina.com>
Date:   Wed Feb 27 15:07:38 2019 +0800

    版本1
  • (5)继续编辑test.go,在里面增加一行。
[root@db_server git]# cat test.go 
111111111111111111111
222222222222222222222
  • (6)使用如下命令再创建一个版本并查看版本记录:
[root@db_server git]# git add test.go 
[root@db_server git]# git commit -m '版本2'
[master 3663114] 版本2
 1 file changed, 1 insertion(+)
[root@db_server git]# git log
commit 36631149ddb0931eef12e9ad33c173c7b7ba9567 (HEAD -> master)
Author: ing <zq9017197@sina.com>
Date:   Wed Feb 27 15:10:09 2019 +0800

    版本2

commit a57f919f12ec7097fce16ad67fa14fc075c94e2b
Author: ing <zq9017197@sina.com>
Date:   Wed Feb 27 15:07:38 2019 +0800

    版本1
  • (7)现在若想回到某一个版本,可以使用如下命令:
  • 其中HEAD表示当前最新版本,HEAD表示当前版本的前一个版本,HEAD^表示当前版本的前前个版本,也可以使用HEAD1表示当前版本的前一个版本,HEAD100表示当前版本的前100版本。
[root@db_server git]# git reset --hard HEAD^
HEAD is now at a57f919 版本1
[root@db_server git]# git log
commit a57f919f12ec7097fce16ad67fa14fc075c94e2b (HEAD -> master)
Author: ing <zq9017197@sina.com>
Date:   Wed Feb 27 15:07:38 2019 +0800

    版本1
[root@db_server git]# cat test.go 
111111111111111111111
  • (8)假如我们现在又想回到版本2,这个时候怎么办?
  • 可以使用如下命令:git reset --hard 版本号
  • 从上面可以看到版本2的版本号为:36631149ddb0931eef12e9ad33c173c7b7ba9567
[root@db_server git]# git reset --hard 36631149ddb0931eef12e9ad33c173c7b7ba9567
HEAD is now at 3663114 版本2
[root@db_server git]# cat test.go 
111111111111111111111
222222222222222222222
  • (9)假如说上面的终端已经关了改怎么回退版本。
  • git reflog命令可以查看我们的操作记录。
[root@db_server git]# git reset --hard HEAD~1
HEAD is now at a57f919 版本1
[root@db_server git]# cat test.go 
111111111111111111111
[root@db_server git]# git reflog
a57f919 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~1
3663114 HEAD@{1}: reset: moving to 36631149ddb0931eef12e9ad33c173c7b7ba9567
a57f919 (HEAD -> master) HEAD@{2}: reset: moving to HEAD^
3663114 HEAD@{3}: commit: 版本2  ##这个短的也是版本号
a57f919 (HEAD -> master) HEAD@{4}: commit (initial): 版本1
[root@db_server git]# git reset --hard 3663114
HEAD is now at 3663114 版本2
[root@db_server git]# cat test.go 
111111111111111111111
222222222222222222222
3.1工作区和暂存区
  • 因为我们创建git版本库时,git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
  • 前面讲了我们把文件往git版本库里添加的时候,是分两步执行的:
  • 第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
  • 第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
  • (1)下面在git目录下再创建一个文件test2.go,然后编辑内容如下:
[root@db_server git]# cat test2.go 
aaaaaaaaaaaaaaaaaaa
  • (2)然后再次编辑test.go内容,在其中加入一行,编辑后内容如下:
[root@db_server git]# cat test2.go 
111111111111111111111
222222222222222222222
aaaaaaaaaaaaaaaaaaaaa
  • (3)使用如下命令查看当前工作树的状态:
[root@db_server 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:   test.go

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.code.go.swp
	test2.go

no changes added to commit (use "git add" and/or "git commit -a")
  • 上面提示我们test.go被修改,而test2.go没有被跟踪。

  • (4)我们使用如下命令把test.go和test2.go加入到暂存区,然后再执行git status命令,结果如下:

[root@db_server git]# git add test.go 
[root@db_server git]# git add test2.go 
[root@db_server git]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   test.go
	new file:   test2.go

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.code.go.swp
  • (5)然后,执行git commit就可以一次性把暂存区的所有修改提交到分支创建一个版本。
[root@db_server git]# git commit -m '版本3'
[master 228ab8b] 版本3
 2 files changed, 2 insertions(+)
 create mode 100644 test2.go
[root@db_server git]# git log
commit 228ab8b3a9e2c479a18a1c9cd2a161f2e81c9641 (HEAD -> master)
Author: ing <zq9017197@sina.com>
Date:   Wed Feb 27 15:56:59 2019 +0800

    版本3

commit 36631149ddb0931eef12e9ad33c173c7b7ba9567
Author: ing <zq9017197@sina.com>
Date:   Wed Feb 27 15:10:09 2019 +0800

    版本2

commit a57f919f12ec7097fce16ad67fa14fc075c94e2b
Author: ing <zq9017197@sina.com>
Date:   Wed Feb 27 15:07:38 2019 +0800

    版本1
  • (6)一旦提交后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的。执行如下命令可以发现:
[root@db_server git]# git status
位于分支 master
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
3.2管理修改
  • git管理的文件的修改,它只会提交暂存区的修改来创建版本。
[root@db_server git]# vi test2.go   ##添加一行ccccccccccccccccccc
[root@db_server git]# cat test2.go 
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
[root@db_server git]# git add test2.go 
[root@db_server git]# git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	修改:     test2.go

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

[root@db_server git]# vi test2.go 
[root@db_server git]# cat test2.go   ##添加一行ddddddddddddddddddd
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
ddddddddddddddddddd
[root@db_server git]# git commit -m '版本4'
[master a6ac731] 版本4
 1 file changed, 1 insertion(+)
[root@db_server git]# cat test2.go 
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
ddddddddddddddddddd
[root@db_server git]# git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     test2.go

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
  • git commit创建一个版本,并使用git status查看,发现第二次修改code.txt内容之后,并没有将其添加的工作区,所以创建版本的时候并没有被提交。
3.3撤销修改
  • (1)继续上面的操作,提示我们可以使用 git checkout – <文件> 来丢弃工作区的改动。执行如下命令,发现工作区干净了,第二次的改动内容也没了。
[root@db_server git]# git checkout -- test2.go
[root@db_server git]# git status
位于分支 master
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@db_server git]# cat test2.go 
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
  • (2)我们继续编辑code.txt,并在其中添加如下内容,并将其添加的暂存区。
[root@db_server git]# cat test2.go ##添加一行ddddddddddddddddddd
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
ddddddddddddddddddd
[root@db_server git]# git add test2.go 
[root@db_server git]# git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	修改:     test2.go

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

  • (3)用命令git reset HEAD file可以把暂存区的修改撤销掉,重新放回工作区。
[root@db_server git]# git reset HEAD test2.go 
重置后取消暂存的变更:
M	test2.go
[root@db_server git]# cat test2.go 
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
ddddddddddddddddddd
[root@db_server git]# git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     test2.go

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
  • (4)现在若想丢弃test2.go的修改,执行如下命令即可。
[root@db_server git]# git checkout -- test2.go 
[root@db_server git]# cat test2.go 
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
[root@db_server git]# git status
位于分支 master
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
  • 场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout – file。
  • 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。
  • 场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节。git reset –hard 版本号
3.4对比文件的不同
  • (1)继续编辑文件test.go,在其中添加一行内容。
[root@db_server git]# cat test2.go #添加一行2222222222222222222
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
1111111111111111111
2222222222222222222
  • (2)现在要对比工作区中test2.go和HEAD版本中code.txt的不同。使用如下命令:git diff HEAD – 文件名
[root@db_server git]# git diff HEAD -- test2.go 
diff --git a/test2.go b/test2.go
index 30f05d2..22a4365 100644
--- a/test2.go
+++ b/test2.go
@@ -2,3 +2,4 @@ aaaaaaaaaaaaaaaaaaa
 bbbbbbbbbbbbbbbbbbb
 ccccccccccccccccccc
 1111111111111111111
+2222222222222222222
  • 减号“-”,代表HEAD版本中test2.go的内容
  • 加号“+”,代表工作区中test2.go的内容
3.5删除文件
  • (1)我们把目录中的test2.go删除。
[root@db_server git]# rm -rf test2.go 
[root@db_server git]# git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	删除:     test2.go

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
  • (2) 现在你有两个选择,一种情况是删错了,可以直接使用git checkout – test2.go,这样文件又回来了。
[root@db_server git]# git checkout -- test2.go
[root@db_server git]# git status
位于分支 master
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@db_server git]# ll test2.go 
-rw-r--r-- 1 root root 100 2月  27 16:54 test2.go
  • 另一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit:
[root@db_server git]# rm -rf test2.go 
[root@db_server git]# git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	删除:     test2.go

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.code.go.swp

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@db_server git]# git rm test2.go
rm 'test2.go'
[root@db_server git]# git commit -m 'R8:rm test2.go'
[master 78db8ea] R8:rm test2.go
 1 file changed, 5 deletions(-)
 delete mode 100644 test2.go
[root@db_server git]# ll teest2.go
ls: 无法访问teest2.go: 没有那个文件或目录
  • 命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值