V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
shazh520
V2EX  ›  git

git 如何删掉一个远程的 commit

  •  
  •   shazh520 · 2018-10-10 12:36:40 +08:00 · 6285 次点击
    这是一个创建于 1996 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在的状态是这样:

    git log

    至于为什么会出现这种情况呢,是我在一次 checkout revision 之后,重新写了代码并提交了。现在我想把那个多余的 commit 删除掉,master 和 origin 都还指向它。现在我每次提交都会显示下面这个弹窗 弹窗

    前辈们支支招,诚谢.

    12 条回复    2018-10-10 14:17:30 +08:00
    yianbin
        1
    yianbin  
       2018-10-10 12:57:03 +08:00
    git push -f -u origin master 试试?
    samuel37
        2
    samuel37  
       2018-10-10 13:14:52 +08:00
    为什么不直接把 repo 删了呢?
    把错误的 commit revert 再 push。
    lnim
        3
    lnim  
       2018-10-10 13:17:13 +08:00 via iPhone
    本地修改好 push -f
    jowan
        4
    jowan  
       2018-10-10 13:18:34 +08:00
    先退回:reset --soft 版本
    再强行 push 远程库:git push -f
    接下来 把同步版本后增加的 commit 提交就可以了

    我一般都是这样做的
    Deville
        5
    Deville  
       2018-10-10 13:19:45 +08:00   ❤️ 1
    同 1L ,另外。。 有个建议。。个人习惯。。 觉得 commit 还是正式化些比较好。。少些口语表述。。
    que01
        6
    que01  
       2018-10-10 13:43:19 +08:00
    修改 .git/refs/heads/branch_name 里面的版本号(这个版本号要注意长度) 这样指向的就是目标版本号 并记住后面几次有效的版本号 使用 git cherry-pick aaa1111 bbb222 ccc333 这样重做有效提交这块
    Ooooooooozil
        7
    Ooooooooozil  
       2018-10-10 13:55:43 +08:00   ❤️ 1
    commit message 推荐 http://gitmoji.carloscuesta.me
    icylogic
        8
    icylogic  
       2018-10-10 13:59:19 +08:00 via iPhone   ❤️ 1
    可以不删就尽量别删,比起删除一个 commit,更好的做法是用一个新的 commit 做 revert,并且写好为什么要这么做。

    如果一定要删,就 reset 之后 push --force,不过做好被同事打的心理准备……
    icylogic
        9
    icylogic  
       2018-10-10 14:02:01 +08:00 via iPhone
    revert 具体怎么做可以参考 https://www.atlassian.com/git/tutorials/undoing-changes/git-revert

    The git revert command can be considered an 'undo' type command, however, it is not a traditional undo operation. Instead of removing the commit from the project history, it figures out how to invert the changes introduced by the commit and appends a new commit with the resulting inverse content. This prevents Git from losing history, which is important for the integrity of your revision history and for reliable collaboration.
    CoCoMcRee
        10
    CoCoMcRee  
       2018-10-10 14:04:57 +08:00
    我一般是从出错前一个 commit 拉个 branch
    CoCoMcRee
        11
    CoCoMcRee  
       2018-10-10 14:06:44 +08:00
    我一般是从出错的分支先改个名字(本地和远端都改), 再前一个 commit 拉个 branch , 然后把正确的再合过来, 然后把推到远端,再把名字改回来, 把之前出错的分支删掉就好了, 这样远端就相当于没有了这个出错的 commit
    Raymon111111
        12
    Raymon111111  
       2018-10-10 14:17:30 +08:00
    reset 然后 push force
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4226 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:14 · PVG 18:14 · LAX 03:14 · JFK 06:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.