Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

删除git远程仓库所有提交记录

删除git远程仓库所有提交记录 how to delete all commit history in github?

操作如下

1
2
3
4
5
6
7
8
9
10
11
12
# 切换到一个全新主分支,无所谓叫什么,后面还会修改分支名称
git checkout --orphan test
# 暂存所有改动过的文件,内容为当前旧分支的最新版本所有文件
git add -A
# 提交更改
git commit -am "commit message"
# 删除原始主分支
git branch -D master
# 将当前分支重命名为 master
git branch -m master
# 最后,强制更新您的存储库
git push -f origin master

语句解释

orphan

What is git checkout --orphan used for?

The core use for git checkout –orphan is to create a branch in a git init-like state on a non-new repository.

Without this ability, all of your git branches would have a common ancestor, your initial commit. This is a common case, but in no way the only one. For example, git allows you to track multiple independent projects as different branches in a single repository.

That’s why your files are being reported as “changes to be committed”: in a git init state, the first commit isn’t created yet, so all files are new to git.

git -A

git-add
这个命令会将当前目录下包括子目录下所有改动的文件提交到暂存区。

git branch -D

--delete --force的快捷方式。强制删除

git branch -m

git branch -m

Move/rename a branch, together with its config and reflog.

git push -f

git push -f

删除一次提交记录

删除最后一次提交

借鉴 Git远程删除某个历史提交记录方法详解

第一步:回滚上一次提交

1
git reset --hard HEAD^

第二步:强制提交本地代码

1
git push origin master -f

删除指定commit提交(非最后一次提交)

查看提交记录,确定需要删除记录的前一次提交ID ,这里是 5606512

1
2
3
4
5
✗ git log --pretty=format:"%h | %ai | %an | %s" | head -n 40
1ea683e | 2022-11-22 15:41:23 +0800 | xxx | normal modify 2
3b872dd | 2022-11-22 15:38:39 +0800 | xxx | add secret key
5606512 | 2022-11-22 15:35:26 +0800 | xxx | normal modify 1
d33f9e7 | 2019-10-28 10:45:20 +0800 | xxx | normal modify 0

执行

1
git rebase -i 5606512

执行交互
必须要删除的 commit 前面的 pick 改为 drop , vim 保存退出。
提示冲突解决冲突,之后执行

1
2
3
4
✗ git add .
✗ git commit -m "xxx"
✗ git rebase --continue
✗ git push origin master -f

就成功了。

ps. 如果直接push

1
2
3
4
5
6
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'gitlab.com:xxx/test-git.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

看下git log

1
2
3
✗ git log --pretty=format:"%h | %ai | %an | %s" | head -n 40
5606512 | 2022-11-22 15:35:26 +0800 | xxx | normal
d33f9e7 | 2019-10-28 10:45:20 +0800 | xxx | normal modify 0

ps. github给出的解决方案
Removing sensitive data from a repository