恢复某个文件到指定的提交
查询提交日志
> git log --pretty=oneline
8e1d139ff7f6875be3d5a60c2245549ca6c953fc (HEAD -> master, origin/master, origin/HEAD) 更新任务详单的修改面板问题
620be0ea945167d0578933c598f471d9bb256045 任务进展更新
f1742c32c03bffab354e525f7f0f9345241ec986 恢复进度表
a24897161559710694ddf3afb7d28132df380265 恢复进度表
...
要恢复到哪个版本就执行
git checkout 版本id(只需要前4位) 具体文件路径
$ git checkout 620b README.MD
恢复分支到某个提交
#1. 先查询那个提交的编号
git log
commit 2bcae3b11f9d1103c637b484f73f0e6dee63dc9d (HEAD -> master, origin/master, origin/HEAD)
Author: gavin <gavin@sunquestor.com>
Date: Wed May 8 11:29:34 2019 +0800
add test2-cp.txt
commit e9042556a2948ac0da9366dcede961ea512e4861
Author: gavin <gavin@sunquestor.com>
Date: Wed May 8 11:28:59 2019 +0800
add test2.txt
commit 3880883d02a38ac7efa670709bab95d7b295a4fe
Author: gavin <gavin@sunquestor.com>
Date: Wed May 8 11:28:16 2019 +0800
add test1.txt
commit b773bad34ab80f836894675bbff3974e60ea32af
Author: gavinsunquestor <gavin@sunquestor.com>
Date: Mon Jan 28 09:45:22 2019 +0800
add README.md
#2. 恢复到add test2.txt这次提交,该提交的编号是e9042556a2948ac0da9366dcede961ea512e4861
git reset --hard e9042556a2948ac0da9366dcede961ea512e4861
HEAD is now at e904255 add test2.txt
#3. 查看当前代码,发现test2-cp.txt 被删掉了,当前代码恢复到了指定提交
$ ls
READEME.md test1.txt test2.txt
#4. 恢复到指定提交后,再次推送会提示错误
$ git push
To ssh://1.2.3.4/temp.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://git@1.2.3.4/temp.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
#5. 强制推送,覆盖远端
git push --force
Total 0 (delta 0), reused 0 (delta 0)
To ssh://1.2.3.4/temp.git
+ 2bcae3b...e904255 master -> master (forced update)
回滚了分支,如何再回到回滚前的提交
#1. 查询git reflog
git reflog
e904255 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to e9042556a2948ac0da9366dcede961ea512e4861
2bcae3b HEAD@{1}: commit: add test2-cp.txt
e904255 (HEAD -> master, origin/master, origin/HEAD) HEAD@{2}: commit: add test2.txt
3880883 HEAD@{3}: commit: add test1.txt
b773bad HEAD@{4}: clone: from ssh://git@101.37.39.7:51107/temp.git
#2. 找到add test2-cp.txt 提交
git reset --hard 2bcae3b
HEAD is now at 2bcae3b add test2-cp.txt
#3. 查看目录,test2-cp.txt恢复了
ll
total 9.0K
drwxr-xr-x 1 xunannan 197609 0 5月 8 11:51 ./
drwxr-xr-x 1 xunannan 197609 0 4月 27 20:46 ../
drwxr-xr-x 1 xunannan 197609 0 5月 8 11:51 .git/
-rw-r--r-- 1 xunannan 197609 29 3月 5 16:16 READEME.md
-rw-r--r-- 1 xunannan 197609 0 5月 8 11:27 test1.txt
-rw-r--r-- 1 xunannan 197609 0 5月 8 11:28 test2.txt
-rw-r--r-- 1 xunannan 197609 0 5月 8 11:51 test2-cp.txt
#4. 提交代码
git push --force
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 225 bytes | 75.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To ssh://101.37.39.7:51107/temp.git
e904255..2bcae3b master -> master