-
撤销本地add
# 使用reset默认的--fixed参数,假装回退到当前版本,撤销add git reset HEAD # reset 有三个参数 --fixed # 默认,不删除工作空间改动代码,撤销commit,撤销add --soft # 不删除工作空间改动代码,撤销commit,不撤销add --hard # 删除工作空间改动代码,撤销commit,不撤销add
-
撤销本地commit
git reset HEAD^ git reset HEAD~ git reset HEAD~2 # 回退2个版本 # 注意在windows的终端使用该命令时,由于^为windows的默认换行符 # windows下 git reset "HEAD^" git reset HEAD^^
如果只是改注释,只需
git commit --amend
-
回退remote版本
- git revert:撤销某个版本的提交
# 撤销<commit-hash>这个版本的操作 git revert [-n] <commit-hash> # 默认需要立刻commit,可以添加-n或--no-commit参数推迟commit # 接下来直接commit、push即可,会在log中追加新的commit记录 # 连续多个revert git revert -n <commit-hash_start>..<commit-hash_end> # 会撤销( commit-hash_start, commit-hash_end ] 的提交
若要撤销执行完的revert操作,只需对这个revert操作也执行一次revert即可
- git reset + git push -f
# 回退到<commit-hash>这个版本 git reset <commit-hash> # 强制推送 git push -f # 注意,此种回退相当于彻底回退到之前的版本,晚于该版本的提交log也会被清除,且此次回退无记录
- 对比 revert & reset
- reset + push -f 需要谨慎使用,一般是为了回退短时间内的某个提交,维持清晰的提交日志。需确保晚于该记录的所有内容可清除!!!
- revert 会记录此次回退的commit,清晰记录每次操作,且可撤销revert
Git——撤销本地add、commit操作 & 回退remote版本
猜你喜欢
转载自blog.csdn.net/SJ1551/article/details/100600414
今日推荐
周排行