Git-版本变更和文件恢复使用
一、版本变更
1:使用git log查看修改过得历史版本,最上面显示的是最近修改过版本,最下面为最早的
[root@ghs gitroot]# git log
commit 02e331c934d0079b458576e16e3ac9328da3c358
Author: ghs <ghs@ghs.com>
Date: Sat Jul 22 21:32:42 2017 +0800
add xiugai 1.txt
commit c343394bb00fda18e6bfe4951848884fb9d4c6e6
Author: ghs <ghs@ghs.com>
Date: Sat Jul 22 20:59:59 2017 +0800
add 1.txt
commit 697af57231e6f07e0ba7be84f997d6d79002429d
Author: ghs <ghs@ghs.com>
Date: Sat Jul 22 20:52:24 2017 +0800
add 1.txt
2:加参数--pretty=oneline显示成一行
[root@ghs gitroot]# git log --pretty=oneline
版本号 修改时描述
02e331c934d0079b458576e16e3ac9328da3c358 add xiugai 1.txt
c343394bb00fda18e6bfe4951848884fb9d4c6e6 add 1.txt
697af57231e6f07e0ba7be84f997d6d79002429d add 1.txt
3:如果想要将版本变更到之前,git reset 加入--hard参数后面跟版本号(版本号最少可以写四个字符串),回滚到c34394版本
语法:git reset --hard 【ID】
[root@ghs gitroot]# git reset --hard c343
HEAD is now at c343394 add 1.txt
变更后查看下1.txt文件内容
变更前1.txt内容
[root@ghs gitroot]# cat 1.txt
sjfdsjfjdshksd
xiugai
1231434
变更后内容
[root@ghs gitroot]# cat 1.txt
sjfdsjfjdshksd
jhfjdsfjdshj
1231434
sdafafsaf
sdasdhjad
4:回滚后的版本发现修改的内容很久的,这个不是你想要的,想要回滚之前比较新版本可以使用git reflog查看之前所有操作记录
使用下面命令显示出没看到02e331c HEAD@{1}: commit: add xiugai 1.txt这个版本
[root@ghs gitroot]# git log --pretty=oneline
c343394bb00fda18e6bfe4951848884fb9d4c6e6 add 1.txt
697af57231e6f07e0ba7be84f997d6d79002429d add 1.txt
使用git reflog查看显示所有版本
[root@ghs gitroot]# git reflog
c343394 HEAD@{0}: c343: updating HEAD
02e331c HEAD@{1}: commit: add xiugai 1.txt
c343394 HEAD@{2}: commit: add 1.txt
697af57 HEAD@{3}: commit (initial): add 1.txt
变更回当前版本后的修改的版本02e3
[root@ghs gitroot]# git reset --hard 02e3
HEAD is now at 02e331c add xiugai 1.txt
查看内容
[root@ghs gitroot]# cat 1.txt
sjfdsjfjdshksd
xiugai
1231434
二、文件恢复
如果你修改了文件,已经git add推送了,想要恢复没编辑前时的内容,可以按下面步骤
1:修改下1.txt文件
[root@ghs gitroot]# vi 1.txt
sjfdsjfjdshksd
xiugai
sadasf
1231434
更改后推送文件
[root@ghs gitroot]# git add 1.txt
查看状态,可以看到提示git reset返回之前的操作
[root@ghs gitroot]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: 1.txt
#
按提示输入git reset
[root@ghs gitroot]# git reset HEAD 1.txt
Unstaged changes after reset:
M 1.txt
输入git checkout命令恢复到修改前的内容
[root@ghs gitroot]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: 1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@ghs gitroot]# git checkout -- 1.txt
恢复后查看内容,显示已经恢复成功了
[root@ghs gitroot]# cat 1.txt
sjfdsjfjdshksd
xiugai
1231434
三、文件删除
删除一个文件步骤:
第一步:rm 删库文件
第二步:git rm 文件
第三步:git add和commit 文件
1:删除库里的2.txt
[root@ghs gitroot]# rm -f 2.txt
查看下git状态,红色字体提示删除了2.txt,但没有完全删除
[root@ghs gitroot]# git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: 2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
用git rm删除
[root@ghs gitroot]# git rm 2.txt
rm '2.txt'
删除文件同样需要git add推送和git commit描述
[root@ghs gitroot]# git add 2.txt
[root@ghs gitroot]# git commit -m "deleted 2.txt"
[master c826e3c] deleted 2.txt
1 files changed, 0 insertions(+), 5 deletions(-)
delete mode 100644 2.txt
再看下git状态
[root@ghs gitroot]# git status
# On branch master
nothing to commit (working directory clean)
git rm 2.txt
记录每一天有趣的事情!!

浙公网安备 33010602011771号