Git回滚文件

git回滚单个文件到指定版本

在使用 git 的过程中,我们要回滚某个文件到指定版本,可以按照如下操作。首先,查看提交记录:

git log filename

输出如下:

commit 4f172b54fee90481dcb61f1e6b9cfe56d7d1cc79 Author: haicoder <haicoder@xxxx.com> Date: Tue Feb 7 10:19:12 2017 +0800 修改更新数据库传递单条记录,和相等判断逻辑 commit 0177f797b069dc869f71bbf3640130c91826ab46 Author: haicoder <haicoder@xxxx.com> Date: Sat Feb 4 18:14:41 2017 +0800 jingtong_limit impl commit 87b9a6c1ef85a7f3fc97498445d61f1565d67a8e Author: XXXX <william@xxxx.com> Date: Tue Apr 12 14:08:49 2016 +0800 加mq队列 commit 96870c39011d3106a0c6087579ef64fb4a5adc0e Merge: 9798c6d 8fbc37f Author: XXXX <lijiang@xxxx.com> Date: Mon Mar 28 09:57:35 2016 +0800 Merge branch 'master' into zxfund commit 2b0bd9781aaf0dba5dae80d7290beb0afdb1167d Merge: fc795fb 24dc13a Author: XXXX <lijiangl@xxxx.com> Date: Tue Mar 15 10:01:31 2016 +0800 Merge branch 'master' into zxfund commit 5cf3166c7755deb217b6d63e4e0f097596be57b1 Author: jack <jack@xxxx.com> Date: Thu Mar 10 15:48:46 2016 +0800 添加trans_records_consumer

回退到指定版本:

git reset 87b9a6c1ef85a7f3fc97498445d61f1565d67a8e filename

输出如下:

Unstaged changes after reset: M filename

如果上一步 reset 执行错了,比如未加文件名,则文件全部回滚,此时 git log 是看不到之前的版本的,执行如下操作:

git log

输出如下:

commit 87b9a6c1ef85a7f3fc97498445d61f1565d67a8e Author: XXXX <william@xxxx.com> Date: Tue Apr 12 14:08:49 2016 +0800 加mq队列 commit 042aff4592d210537de052b894c15c714256ec6b Author: XXXX <william@xxxx.com> Date: Tue Apr 12 09:15:24 2016 +0800 confirmSettleDailyRedeemImpl()接口完成 commit 824459911d3a5b565aea60edbea6e36b08021e97 Author: XXXX <william@xxxx.com> Date: Mon Apr 11 17:05:32 2016 +0800 writing confirmSettleDailyRedeemImpl()接口 commit 08a6d75e0261f5c2edab2d5f4d815dafd672278c Author: XXXX <william@xxxx.com> Date: Mon Apr 11 14:10:11 2016 +0800 sm_daily_batch_record表增加申请赎回笔数batchCountE6字段 commit b6c553590d6a6646a60ff36a601a05dc27df3542 Author: XXXX <william@xxxx.com> Date: Mon Apr 11 13:50:03 2016 +0800 增加了listPreBatchDailyRedeemImpl()接口

此时,我们可以使用如下命令:

git reflog

执行后,如下所示:

87b9a6c HEAD@{0}: reset: moving to 87b9a6c1ef85a7f3fc97498445d61f1565d67a8e 4f172b5 HEAD@{1}: commit: 修改更新数据库传递单条记录,和相等判断逻辑 fd1dc92 HEAD@{2}: checkout: moving from master to jintong_limit a0c39e0 HEAD@{3}: clone: from git@gitlab.haicoder.com:cpp/server.git

现在,我们执行如下操作:

git reset --hard 4f172b5

现在,我们再次查看 log,执行如下命令:

git log

执行后,输出如下:

commit 4f172b54fee90481dcb61f1e6b9cfe56d7d1cc79 Author: zzz10310 <haicoder@xxxx.com> Date: Tue Feb 7 10:19:12 2017 +0800 修改更新数据库传递单条记录,和相等判断逻辑 commit fd1dc927d8b8d2f66086621f8b85e79eaa6c251d Author: zzz10310 <haicoder@xxxx.com> Date: Mon Feb 6 15:34:19 2017 +0800 修改金额限制0和999999999 commit 0177f797b069dc869f71bbf3640130c91826ab46 Author: haicoder <haicoder@xxxx.com> Date: Sat Feb 4 18:14:41 2017 +0800 jingtong_limit impl commit b98cf97006f0d56c0f1980445b4c51ff90020bd3 Author: XXXX <chenzh@xxxx.com> Date: Tue Jan 3 16:19:01 2017 +0800 添加all_admin.sh, 更新crontab.txt commit 75514399bf4e65bae5c1f86ee2da47371e35c245 Merge: aa3a6b9 f09a3a4 Author: XX <zhengqig@xxxx.com> Date: Tue Jan 3 17:02:59 2017 +0800 Merge branch 'fund_fof_v3.5.1' into 'master' fofId See merge request !682

我们可以看到,全部回来啦。最后,我们提交到远程仓库即可:

git push origin jintong_limit

输出如下:

Counting objects: 14, done. Delta compression using up to 4 threads. Compressing objects: 100% (13/13), done. Writing objects: 100% (14/14), 1.10 KiB | 0 bytes/s, done. Total 14 (delta 10), reused 2 (delta 0) remote: remote: View merge request for jintong_limit: remote: https://gitlab.xxxx.com/cpp/server/merge_requests/735 remote: To gitlab.xxxx.com:cpp/server.git 4f172b5..57f16fc jintong_limit -> jintong_limit