首页 > git 如何回滚

git 如何回滚

场景:

如果用git reset --hard命令回滚到A修改的版本号,那么B的修改也被丢弃了


git revert (版本号)


不要在 public branch 上对已经提交的 commit 使用 git reset,如果有人已经 pull 了这些 commit,会很麻烦
这种情况下应该用 git revert,会产生一个单独的 commit


这种情况,我一般直接查log,把A文件还原,保证B文件完整


这么做不会丢吧,我们都是这么做的..
git reset --hard命令回滚到A修改的版本号
git pull --rebase origin 分支号 拉下来B的代码看看有没有冲突,有冲突解决
后 git push ..


$ mkdir test
$ cd test
$ git init
$ echo aaaa>a.txt
$ echo bbbb>b.txt
$ git commit -a -m "init two files"
[master (root-commit) 2ca34b8] init
...
$ echo update>a.txt
$ git commit -a -m "update file a"
[master **e216f56**] update file a
...
$ echo update>b.txt
$ git commit -a -m "update file b"
[master 6906147] update file b
...
$ git revert **e216f56**
unix2dos: converting file f:/test/.git/COMMIT_EDITMSG...
dos2unix: converting file f:/test/.git/COMMIT_EDITMSG...
[master 2a9c653] Revert "update file a"
...

  1. git log 查看 A之前 A B的commitId

  2. git reset --hard A之前的commitId

  3. git cherry-pick B的commitId

这个功能叫检出功能,可以拿到某一次提交的修改


只能revert,不能reset,凡是已经push到远程的commit都不能reset或者commit --amend,这个会破坏别人的版本历史。

关于revert可以看看我的这篇文章:https://.com/a/11...


不可以直接把错了的修改好,然后提交一次覆盖掉吗?


git reset --soft HEAD@{id},这样就是将提交撤回了,但是工作区的修改不会消失,然后将错误的修改改对了再提交并push到远端吧


git rebase -i HEAD^^^
用默认编辑器打开一个文档,修个A那次提交前面改成drop或简写为d 保存。
会自动舍弃那次提交(如果有冲突要自己解决冲突)

【热门文章】
【热门文章】