您的位置:首页 > 编程语言

git远程仓库代码回退

2016-06-14 16:18 706 查看
有时候项目运行后,发现修改产生了新的问题,这时候我们就要回退代码。如果修改的代码很少的话,可以手动修改回来,有时候修改了很多内容,甚至忘了修改了哪些内容,这时候就需要用到reset命令了,其实git提交的内容并不能真的回退,只是用以前的代码覆盖现在的代码,这样我们就不需要手动覆盖错误的代码了

[root@localhost gitdemo]# echo 'hello world'>hello.txt 

[root@localhost gitdemo]# git ci -am '提交一次错误的代码'

[master b00ef94] 提交一次错误的代码

 1 file changed, 1 insertion(+), 2 deletions(-)

[root@localhost gitdemo]# git push

我们提交了一次有问题的代码,并push到远端了,现在我们要回退回正确的代码

显示最近的n此提交

[root@localhost gitdemo]# git log -3

commit b00ef94a4841e59f60a3cd2e112bad4df6029d13

Author: chao.zeng <576178421@qq.com>

Date:   Tue Jun 14 15:39:06 2016 +0800

    提交一次错误的代码

commit d0ad0126b17518a61d85e8f1ec70c47a16a45532

Author: chao.zeng <576178421@qq.com>

Date:   Tue Jun 14 15:33:03 2016 +0800

    tt

commit 4a8762c8c911251f513a4d0c008fd7fa75fa54b5

Author: chao.zeng <576178421@qq.com>

Date:   Tue Jun 14 09:06:40 2016 +0800

    add aaa

回退到上一次提交,即 commit d0ad0126b17518a61d85e8f1ec70c47a16a45532

回退代码用reset ,--hard会完全回退,包括头指针,暂存区,和工作区,因为我们要将工作区代码回退到以前,所以使用--hard,d0ad是上次的提交号

[root@localhost gitdemo]# git reset --hard d0ad

HEAD 现在位于 d0ad012 tt

[root@localhost gitdemo]# cat hello.txt 

hello

world

之前hello.txt的内容是一行'hello world',现在变成两行了

恢复头指针

[root@localhost gitdemo]# git reset --soft b00e

[root@localhost gitdemo]# git status

位于分支 master

您的分支与上游分支 'origin/master' 一致。

要提交的变更:

  (使用 "git reset HEAD <文件>..." 以取消暂存)

修改:     hello.txt

这样我们又回到了最新的提交,即头指针指到最新的提交,但是工作区的内容已经回退了(如果头指针不是指向最新的提交,是无法push的)

提交

[root@localhost gitdemo]# git commit -am '回退到正确的代码'

[master d2fd6d2] 回退到正确的代码

 1 file changed, 2 insertions(+), 1 deletion(-)

[root@localhost gitdemo]# git push

Username for 'https://git.coding.net': xiangyi

Password for 'https://xiangyi@git.coding.net': 

对象计数中: 3, 完成.

压缩对象中: 100% (2/2), 完成.

写入对象中: 100% (3/3), 313 bytes | 0 bytes/s, 完成.

Total 3 (delta 0), reused 0 (delta 0)

To https://git.coding.net/xiangyi/gittest.git
   b00ef94..d2fd6d2  master -> master

[root@localhost gitdemo]# cat hello.txt 

hello

world

[root@localhost gitdemo]# 

[root@localhost gitdemo]# git log -3

commit d2fd6d2178f11a1a90622219e4e45543a6d44820

Author: chao.zeng <576178421@qq.com>

Date:   Tue Jun 14 16:13:47 2016 +0800

    回退到正确的代码

commit b00ef94a4841e59f60a3cd2e112bad4df6029d13

Author: chao.zeng <576178421@qq.com>

Date:   Tue Jun 14 15:39:06 2016 +0800

    提交一次错误的代码

commit d0ad0126b17518a61d85e8f1ec70c47a16a45532

Author: chao.zeng <576178421@qq.com>

Date:   Tue Jun 14 15:33:03 2016 +0800

    tt

[root@localhost gitdemo]# 

从log可以看出,代码不是真的回退了,只是用之前的代码再执行了一次提交操作并push到远程仓库
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git reset 远程回退