新手问题 git 回退到某版本后,想再前进……

chairy11 · July 17, 2013 · Last by chairy11 replied at July 18, 2013 · 19763 hits

A状态:代码版本A B状态:代码版本B(比A状态时增加了图片、代码)

这时,git add. git commit -m"" 。push 之前,意识到忘了让 git 忽略图片的添加,就: git reset --hard HEAD^

然后在.gitignore 中加了句:app/assets/image,(以为这下会忽略图片上传) 然后 git add. git commit -m"" git push 了

这时,我以为效果是:B状态的一切还在,push 了代码版本B,只是忽略了图片…… 可结果是:B状态一切消失了(代码、图片都没了),一切还原到代码版本A……

请问:怎样回到B状态?

要用git fsck --lost-found去找了

git reset --hard cat .git/ORIG_HEAD ?

git reflog 列出近期修改,找到要的 hash 然后 reset。

问题可能在这里,git reset --hard HEAD^,“--hard”在这里是搞破坏的。

@chairy11

86 gewang@LM-SHC-00355679@09:28:53:~/test 
=> git init git-revert-change
Initialized empty Git repository in /Users/gewang/test/git-revert-change/.git/
87 gewang@LM-SHC-00355679@09:28:57:~/test 
=> cd git-revert-change/
88 gewang@LM-SHC-00355679@09:28:58:~/test/git-revert-change 
=> git config user.name 'loveky'
89 gewang@LM-SHC-00355679@09:29:07:~/test/git-revert-change 
=> git config user.email '[email protected]'
90 gewang@LM-SHC-00355679@09:29:15:~/test/git-revert-change 
=> touch a b c
91 gewang@LM-SHC-00355679@09:29:22:~/test/git-revert-change 
=> git status -s
?? a
?? b
?? c
92 gewang@LM-SHC-00355679@09:29:24:~/test/git-revert-change 
=> git add .
93 gewang@LM-SHC-00355679@09:29:26:~/test/git-revert-change 
=> git commit -m "initial drop"
[master (root-commit) 9e3ca95] initial drop
 0 files changed
 create mode 100644 a
 create mode 100644 b
 create mode 100644 c

#此时为状态A

94 gewang@LM-SHC-00355679@09:29:32:~/test/git-revert-change (master)
=> git log -1
commit 9e3ca95aa23b25031449445d433ac3480471b7aa
Author: loveky <[email protected]>
Date:   Thu Jul 18 09:29:32 2013 +0800

    initial drop
97 gewang@LM-SHC-00355679@09:30:03:~/test/git-revert-change (master)
=> touch code image
98 gewang@LM-SHC-00355679@09:30:07:~/test/git-revert-change (master)
=> git status -s
?? code
?? image
99 gewang@LM-SHC-00355679@09:30:09:~/test/git-revert-change (master)
=> git add .
100 gewang@LM-SHC-00355679@09:30:12:~/test/git-revert-change (master)
=> git commit -m "add code & image"
[master 15a573d] add code & image
 0 files changed
 create mode 100644 code
 create mode 100644 image

# 此时为状态B

101 gewang@LM-SHC-00355679@09:30:21:~/test/git-revert-change (master)
=> git log -2
commit 15a573d0eea96be3e11e3b19f7a763757a85a7c6
Author: loveky <[email protected]>
Date:   Thu Jul 18 09:30:21 2013 +0800

    add code & image

commit 9e3ca95aa23b25031449445d433ac3480471b7aa
Author: loveky <[email protected]>
Date:   Thu Jul 18 09:29:32 2013 +0800

    initial drop
102 gewang@LM-SHC-00355679@09:30:25:~/test/git-revert-change (master)
=> git reset --hard HEAD^
HEAD is now at 9e3ca95 initial drop

# 通过reset --hard回到状态A

103 gewang@LM-SHC-00355679@09:30:40:~/test/git-revert-change (master)
=> ls
a b c

# 所有B的change已经回滚, 可以通过查看reflog找到reset之前HEAD指向的commit,也就是B

104 gewang@LM-SHC-00355679@09:30:42:~/test/git-revert-change (master)
=> git reflog
9e3ca95 HEAD@{0}: reset: moving to HEAD^
15a573d HEAD@{1}: commit: add code & image <<<<<  倒数第2条,即回滚之前的HEAD, commit B
9e3ca95 HEAD@{2}: commit (initial): initial drop

# 然后把B中的tree拿出来覆盖index和workspace,但HEAD依旧指向A

105 gewang@LM-SHC-00355679@09:32:27:~/test/git-revert-change (master)
=> git checkout HEAD@{1} -- .

# 可以看到B里添加的2个文件在index中

106 gewang@LM-SHC-00355679@09:34:09:~/test/git-revert-change (master)
=> git status -s
A  code
A  image

# 将image从index中移除

112 gewang@LM-SHC-00355679@09:40:48:~/test/git-revert-change (master)
=> git rm --cached image
rm 'image'

# 此时image已经成为untrack的状态

113 gewang@LM-SHC-00355679@09:41:01:~/test/git-revert-change (master)
=> git status -s
A  code
?? image

# 将image添加到.gitignore

114 gewang@LM-SHC-00355679@09:41:03:~/test/git-revert-change (master)
=> echo image >> .gitignore

# 看到B已经被忽略,多出来.gitignore。可以将其加入版本控制,也可以忽略它

115 gewang@LM-SHC-00355679@09:41:13:~/test/git-revert-change (master)
=> git status -s
A  code
?? .gitignore

# 选择忽略它

116 gewang@LM-SHC-00355679@09:41:15:~/test/git-revert-change (master)
=> echo .gitignore >> .gitignore

117 gewang@LM-SHC-00355679@09:41:40:~/test/git-revert-change (master)
=> git status -s
A  code

# 重新commit,大功告成

118 gewang@LM-SHC-00355679@09:41:43:~/test/git-revert-change (master)
=> git commit -m "I'm good now"
[master 099a28f] I'm good now
 0 files changed
 create mode 100644 code
119 gewang@LM-SHC-00355679@09:41:58:~/test/git-revert-change (master)
=> git log
commit 099a28f3e19aa7102fad41c95ef28a6d89f8b438
Author: loveky <[email protected]>
Date:   Thu Jul 18 09:41:58 2013 +0800

    I'm good now

commit 9e3ca95aa23b25031449445d433ac3480471b7aa
Author: loveky <[email protected]>
Date:   Thu Jul 18 09:29:32 2013 +0800

    initial drop
120 gewang@LM-SHC-00355679@09:42:00:~/test/git-revert-change (master)
=> git log --name-status
commit 099a28f3e19aa7102fad41c95ef28a6d89f8b438
Author: loveky <[email protected]>
Date:   Thu Jul 18 09:41:58 2013 +0800

    I'm good now

A       code

commit 9e3ca95aa23b25031449445d433ac3480471b7aa
Author: loveky <[email protected]>
Date:   Thu Jul 18 09:29:32 2013 +0800

    initial drop

A       a
A       b
A       c

#5 楼 @loveky 很详细 👍 。 昨天也遇到了类似的情况,我的简化版操作:

# 撤销 commit 操作 git reset --soft HEAD@{1} # 撤销 add 操作 git reset HEAD hello.png

#5 楼 @loveky 谢谢,感动得一塌糊涂:) 那个……我已经执行到“git rm --cached image”移除图片那一步了,但我要移除的是整个文件夹(图片好多好多),用“git rm --cached app/assets/image/”可以吗?貌似提示“fatal: not removing 'app/assets/image' recursively without -r”

#8 楼 @chairy11

git help rm

-r Allow recursive removal when a leading directory name is given.

-r参数就是干这个的,跟系统的 rm 命令一样,递归删除

#9 楼 @loveky 谢谢,搞定了,好开心:)关注了你微博哦……

#10 楼 @chairy11 有时间可以翻翻 pro git 那本书的前 3 章,网上有中文版的

关注你了

You need to Sign in before reply, if you don't have an account, please Sign up first.