的代码也就是回滚,如何操作:
[root@localhost spider]# git log # 查看提交记录
commit 1560f36d2ebb48a47c7db12cd0aedf88e3d2a0f9
Author: Jefrey <lianzhilei0711@163.com>
Date: Wed Jul 5 16:43:16 2017 +0800
conf commit
commit 81a32a17078f92d290ea29a7bbc928e1b30bf501
Author: Jefrey <lianzhilei0711@163.com>
Date: Wed Jul 5 16:34:08 2017 +0800
third commit
commit 40ac1cff6d9bc827c1c3d8dac2b3aa838e4dfe70
Author: lianzl <lianzl@commchina.net>
Date: Wed Jul 5 16:29:56 2017 +0800
second commit
commit daf94f4cf0919b0fbc86b4a0318568a19bceb307
Author: lianzl <lianzl@commchina.net>
Date: Wed Jul 5 16:18:54 2017 +0800
first commit
git log命令显示从最近到最远的提交日志,我们可以看到4次提交,最近的一次是conf commit,上一次是third commit,最早的一次是first commit。 如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline 参数
[root@localhost spider]# git log --pretty=oneline
1560f36d2ebb48a47c7db12cd0aedf88e3d2a0f9 conf commit
81a32a17078f92d290ea29a7bbc928e1b30bf501 third commit
40ac1cff6d9bc827c1c3d8dac2b3aa838e4dfe70 second commit
daf94f4cf0919b0fbc86b4a0318568a19bceb307 first commit
① 回滚到上一次提交的代码:
[root@localhost spider]# git reset --hard HEAD^
HEAD 现在位于 81a32a1 third commit
[root@localhost spider]# git log --pretty=oneline
81a32a17078f92d290ea29a7bbc928e1b30bf501 third commit
40ac1cff6d9bc827c1c3d8dac2b3aa838e4dfe70 second commit
daf94f4cf0919b0fbc86b4a0318568a19bceb307 first commit
查看文件,发现现在已经回滚到上一次提交代码的状态
② 回滚到指定版本代码:
[root@localhost spider]# git reset --hard 40ac1cff # 指定前7位即可
HEAD 现在位于 40ac1cf second commit
[root@localhost spider]# git log --pretty=oneline
40ac1cff6d9bc827c1c3d8dac2b3aa838e4dfe70 second commit
daf94f4cf0919b0fbc86b4a0318568a19bceb307 first commit
现在,你回退到了某个版本,关掉了电脑,第二天早上就后悔了,想恢复到新版本怎么办?找不到新版本的commit id怎么办?
在Git中,总是有后悔药可以吃的。当你用$ git reset --hard HEAD^回退到third commit版本时,再想恢复到最新conf commit的版本,就必须找到conf commit的commit id。Git提供了一个命令git reflog用来记录你的每一次命令:
[root@localhost spider]# git reflog
40ac1cf HEAD@{0}: reset: moving to 40ac1cff
81a32a1 HEAD@{1}: reset: moving to HEAD^
1560f36 HEAD@{2}: commit: conf commit
81a32a1 HEAD@{3}: commit: third commit
40ac1cf HEAD@{4}: commit: second commit
daf94f4 HEAD@{5}: commit: first commit
[root@localhost spider]# git reset --hard 1560f36 # 指定最新提交代码id
HEAD 现在位于 1560f36 conf commit
[root@localhost spider]# git log --pretty=oneline # 恢复如初
1560f36d2ebb48a47c7db12cd0aedf88e3d2a0f9 conf commit
81a32a17078f92d290ea29a7bbc928e1b30bf501 third commit
40ac1cff6d9bc827c1c3d8dac2b3aa838e4dfe70 second commit
daf94f4cf0919b0fbc86b4a0318568a19bceb307 first commit
注:切记是本地代码回滚,不会影响到github上的仓库内容
5、撤销修改
① 未提交到暂存区撤销
假如现在在new.py文件中添加了两行代码:
#create new file
second commit
third commit
git is great
but my stupid boss still prefers SVN.
在你准备提交前,一杯咖啡起了作用,你猛然发现了“stupid boss”可能会让你丢掉这个月的奖金!
此时用git status 查看一下:
[root@localhost spider]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: new.py
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
你可以发现,Git会告诉你,git checkout -- file 可以丢弃工作区的修改:
[root@localhost spider]# git checkout -- new.py
[root@localhost spider]# more new.py
#create new file
second commit
third commit
好了,一切恢复如初,文件中修改的内容全部清除;new.py文件修改后还没有被放到暂存区,现在,撤 |