Coding Notes 系列
Coding

git中通过ForcePush避免Merge记录

昨天迷迷糊糊地又推错了一个版本号...为了避免给上百个纯粹的commit里面插入一个无用的merge记录,我决定用ForcePush来解决这个问题。

Hako Chest
更新于
4420 字
6 min read
git中通过ForcePush避免Merge记录

开场白

昨天迷迷糊糊地又推错了一个版本号…为了避免给上百个纯粹的commit里面插入一个无用的merge记录,我决定用ForcePush来解决这个问题。

流程

大致分为如下几个步骤

  1. 提前备份好你新推送部分的代码(很重要!或者你记得写了啥再重写也行,或者你等着stash之后在再改也行~)
  2. 进到当前仓库
  3. 列出commit日志
  4. Checkout到错前的commit
  5. 修改代码
  6. Add
  7. Commit
  8. Push
  9. 再次检查日志
  10. (附) 如果push遇到网络问题
  11. (附) 然后通过Github Desktop来看看效果,并解决冲突

进到当前仓库

cd /path/to/your/repo

例如我这里就是

cd C:\\Users\\Chest\\Documents\\GitHub\\self\\osynic

列出commit日志

git log

然后就可以看到一大串的历史了

Chest@DESKTOP-KUEMADK MINGW64 ~/Documents/GitHub/self/osynic (main)
$ git log
commit xxxxxxxxxxxxxxxxxxxxxxx (HEAD -> main, origin/main, origin/HEAD)
Author: HakoChest <zoneherobrine@gmail.com>
Date:   Thu Mar 13 12:05:36 2025 +0800

    0.1.1 Update release.yml

commit xxxxxxxxxxxxxxxxxxxxxxx
Author: HakoChest <zoneherobrine@gmail.com>
Date:   Thu Mar 13 12:01:12 2025 +0800

    0.3.85 Actions & Formatted & Deps

commit xxxxxxxxxxxxxxxxxxxxxxx
Author: HakoChest <zoneherobrine@gmail.com>
Date:   Tue Mar 11 18:08:41 2025 +0800

    0.3.84 Improve Cloud fe

commit xxxxxxxxxxxxxxxxxxxxxxx
Author: HakoChest <zoneherobrine@gmail.com>
Date:   Tue Mar 11 17:54:07 2025 +0800

    0.3.83 PlaylistFeaturedCard In Vue Router

这里面的xxxxxx就是commit的hash,因为我在0.3.85版本后面推了一个0.1.1的版本号,要回到0.3.85的commit,找到它的的hash即可

然后你按q退出log页面,准备checkout

Checkout到错前的commit

git checkout xxxxxxxxxxxxxxxxxxxxxxx

然后它会提示你已经处于一个detached HEAD状态,这时候你可以直接修改你的文件,然后commit,然后push

Chest@DESKTOP-KUEMADK MINGW64 ~/Documents/GitHub/self/osynic (main)
$ git checkout xxxxxxxxxxxxxxxxxxxxxxx
Note: switching to 'xxxxxxxxxxxxxxxxxxxx'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 03f0001 0.3.85 Actions & Formatted & Deps

修改代码

因为这个commit里面我只是改了一下release.yml文件,所以我对应复现一下之前的修改,接下来就可以add了

这个操作在IDE里面完成即可

Add

git add .

全部添加

Commit

git commit -m "0.3.86 Update release.yml"

行云流水,一切就绪

Push

我们先试试直接去push

git push origin HEAD:main

然后遇到的问题是

$ git push origin HEAD:main
To https://github.com/Osynicite/osynic.git
 ! [rejected]        HEAD -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/Osynicite/osynic.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you want to integrate the remote changes, use 'git pull'
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

是在说你的远程仓库的main分支比你的本地仓库的main分支要新,所以此时我们就需要强制push了

这时候你可以用--force来强制push

git push origin HEAD:main --force

然后就完美成功了(๑˃ᴗ˂)ﻭ

$ git push origin HEAD:main --force
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 592 bytes | 592.00 KiB/s, done.
Total 5 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/Osynicite/osynic.git
 + xxxxxxxx...xxxxxx HEAD -> main (forced update)

再次检查日志

git log

然后就能看到正确流畅的commit记录了,耶∑d(°∀°d)

Chest@DESKTOP-KUEMADK MINGW64 ~/Documents/GitHub/self/osynic ((01e5508...))
$ git log
commit xxxxxxxxxxxxxxxxxxxxxxx (HEAD, origin/main, origin/HEAD)
Author: HakoChest <zoneherobrine@gmail.com>
Date:   Thu Mar 13 21:48:02 2025 +0800

    0.3.86 Update release.yml

commit xxxxxxxxxxxxxxxxxxxxxxx
Author: HakoChest <zoneherobrine@gmail.com>
Date:   Thu Mar 13 12:01:12 2025 +0800

    0.3.85 Actions & Formatted & Deps

commit xxxxxxxxxxxxxxxxxxxxxxx
Author: HakoChest <zoneherobrine@gmail.com>
Date:   Tue Mar 11 18:08:41 2025 +0800

    0.3.84 Improve Cloud fe

commit xxxxxxxxxxxxxxxxxxxxxxx
Author: HakoChest <zoneherobrine@gmail.com>
Date:   Tue Mar 11 17:54:07 2025 +0800

    0.3.83 PlaylistFeaturedCard In Vue Router

(附) 如果push遇到网络问题

如果push遇到了网络问题,可以尝试关闭git代理,可能会有帮助( ̄▽ ̄)/

git config --global --unset http.proxy
git config --global --unset https.proxy

(附) 然后通过Github Desktop来看看效果,并解决冲突

但是就算完成了上述流程,打开Github Desktop,你会发现你的分支变成了Detached HEAD状态,这时候你可以直接切换到main分支,然后就会发现你的分支已经更新了

你原来错推的commit会变为没有push过的commit,这个时候你直接给他无痛undo掉即可( ̄ω ̄)

然后Fetch来pull一下云端的更新,然后就可以看到你的commit记录已经更新了(´• ω •`)

完结撒花,继续你行云流水的项目开发叭叭叭叭叭(〃^▽^〃)~

#Git #Merge #Checkout #ForcePush