-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathResetGitHistory.sh
More file actions
41 lines (31 loc) · 1.03 KB
/
Copy pathResetGitHistory.sh
File metadata and controls
41 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
# 一键重置 Git 仓库历史,只保留当前文件状态
# 支持自定义分支名(默认 master)
# 用法:
# ./reset_git_history.sh [分支名]
# 示例:
# ./reset_git_history.sh # 默认覆盖 master
# ./reset_git_history.sh main # 覆盖 main 分支
set -e
TARGET_BRANCH=${1:-master}
echo "⚠️ 警告:这将清空分支 [$TARGET_BRANCH] 的历史,只保留当前文件。"
read -p "确定继续吗?(Y/N): " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "操作已取消"
exit 1
fi
# 创建孤儿分支
git checkout --orphan latest_branch
# 添加所有文件
git add -A
# 提交
git commit -m "Initial commit with current files"
# 删除目标分支(如果存在)
if git show-ref --verify --quiet refs/heads/$TARGET_BRANCH; then
git branch -D $TARGET_BRANCH
fi
# 重命名当前分支为目标分支
git branch -m $TARGET_BRANCH
# 强制推送到远程
git push -f origin $TARGET_BRANCH
echo "✅ 分支 [$TARGET_BRANCH] 历史已重置并推送到远程。"