Git历史重置
Git历史重置
有时候需要克隆github的项目使用,自己进行修改需要提交到自己的代码仓库,但是会包含前仓库的大量提交历史,看起来不方便,就需要清理历史记录并重置为一个全新的提交
新项目(如git clone 的项目)
1# 进入项目目录
2cd template-repo-master
3# 删除原有的.git目录(如果存在)初始化全新的Git仓库
4rm -rf .git
5git init
6
7git add .
8git commit -m "Initial commit from template"
9
10git remote add origin git@github.com:your-username/your-new-repo.git
11
12git push -u origin master --force
已经将代码推送到自己的远程仓库 清理历史记录并重置为一个全新的提交
1# 1. 克隆你的远程仓库(可选)
2git clone git@github.com:your-username/your-repo.git
3cd your-repo
4
5# 2.创建孤立分支(全新的历史起点)
6git checkout --orphan new-branch
7
8# 3. 添加所有文件到新分支
9git add -A
10git commit -m "Initial commit (cleaned history)"
11
12#4. 删除旧的主分支
13git branch -D master # 或 main
14
15# 5. 重命名新分支为主分支
16git branch -m master # 或 main
17
18# 6. 强制推送到远程仓库
19git push -f origin master
20
21# 7. 更新远程仓库设置(如有必要)
22git branch --set-upstream-to=origin/master master
23
24# 检查提交历史应该只显示你的"Initial commit"
25git log --oneline
发表评论