Home  >  Q&A  >  body text

git 如何回滚

场景:

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

过去多啦不再A梦过去多啦不再A梦2727 days ago697

reply all(10)I'll reply

  • 巴扎黑

    巴扎黑2017-05-02 09:39:50

    $ 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"
    ...

    reply
    0
  • PHPz

    PHPz2017-05-02 09:39:50

    git revert (version number)

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-02 09:39:50

    You won’t lose it by doing this, we all do this..
    git reset --hard command rolls back to the version number modified by A
    git pull --rebase origin branch number Pull down the code of B to see if there is any conflict , git push after conflict resolution
    ..

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-02 09:39:50

    git reset --soft HEAD@{id}, this will withdraw the submission, but the modifications in the workspace will not disappear, then correct the wrong modifications, submit and push to the remote end

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-02 09:39:50

    In this case, I usually check the log directly and restore file A to ensure that file B is complete

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-02 09:39:50

    Can’t you just modify the wrong ones and then submit them once to overwrite them?

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-02 09:39:50

    Don’t use git reset,如果有人已经 pull 了这些 commit,会很麻烦
    这种情况下应该用 git revert on an already submitted commit on the public branch, it will generate a separate commit

    reply
    0
  • ringa_lee

    ringa_lee2017-05-02 09:39:50

    git rebase -i HEAD^^^
    用默认编辑器打开一个文档,修个A那次提交前面改成drop或简写为d Save.
    The submission will be automatically discarded (if there is a conflict, you have to resolve the conflict yourself)

    reply
    0
  • PHP中文网

    PHP中文网2017-05-02 09:39:50

    1. git log View the commitId of A B before A

    2. git reset --hard A's previous commitId

    3. git cherry-pick B’s commitId

    This function is called the checkout function, and you can get the modifications submitted at a certain time

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-02 09:39:50

    You can only revert, not reset. Any commit that has been pushed to the remote cannot be reset or commit --amend. This will destroy other people's version history.

    For information about revert, you can read this article of mine: /a/11...

    reply
    0
  • Cancelreply