惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
K
Kaspersky official blog
Scott Helme
Scott Helme
C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
T
Threat Research - Cisco Blogs
L
LINUX DO - 热门话题
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
月光博客
月光博客
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
Recorded Future
Recorded Future
Latest news
Latest news
V2EX - 技术
V2EX - 技术
T
The Exploit Database - CXSecurity.com
H
Heimdal Security Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
IT之家
IT之家
博客园 - 叶小钗
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
AWS News Blog
AWS News Blog
H
Help Net Security
S
Security @ Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Full Disclosure
S
Schneier on Security
S
Security Affairs
T
Tenable Blog

博客园 - 冯叶青

OpenAI vs Anthropic API 对比:响应体、请求体、消息格式与工具调用 GitHub Actions 自动部署流程 Next.js lingui.js 多语言自动提取翻译键 - ats-node React实现短信验证码输入组件 Next.js 中优雅地使用 Lottie 动画 Next.js 路由参数更新最佳实践:从 replaceState 到 nextReplaceState Jenkins构建时SWR模块导入报错解决方案 解决 Jenkins 环境下 Lingui 构建报错 "btoa is not defined" git 提交 实现大图自动压缩功能 React lingui.js 多语言自动提取翻译键 - ast node html2canvas 解决截图空白问题 react 实现前端发版监测 JS根据文件名获取文件类型 JS获取本机IP地址 适用于react、vue菜单格式化工具函数 git 内容提交 实现大图拦截功能 JS实现视频截图 next.js 利用中间件(middleware.ts)实现PC与移动路由无缝切换 Android生成签名文件及对apk进行签名 JS-SDK 配置,实现微信分享功能
Git分支自动合并脚本:基于时间戳的冲突解决方案
冯叶青 · 2025-03-14 · via 博客园 - 冯叶青

前言

在团队开发中,分支合并是一个常见但又令人头疼的问题。特别是当多个分支同时对同一文件进行修改时,解决合并冲突往往需要手动干预。今天给大家分享一个基于时间戳的自动分支合并脚本,它能够自动处理合并冲突,大大提高工作效率。

功能特点

  1. 自动合并指定的源分支到目标分支
  2. 基于最后修改时间戳自动解决冲突
  3. 支持详细日志输出
  4. 提供演习模式(dry-run)
  5. 可自定义冲突文件列表输出路径
  6. 完善的错误处理和状态检查

使用方法

基本用法

./merge-branches.sh <目标分支> <源分支>

支持的选项

  • -h, --help:显示帮助信息
  • -v, --verbose:显示详细的执行过程
  • -d, --dry-run:演习模式,不实际执行合并
  • -o, --output FILE:指定冲突文件列表的输出文件

使用示例

# 将develop分支合并到main分支
./merge-branches.sh main develop

# 详细模式下合并
./merge-branches.sh -v master feature

# 演习模式,不实际执行
./merge-branches.sh -d main develop

工作原理

1. 参数解析

脚本首先解析命令行参数,支持多种参数格式,包括简短形式(如-h)和完整形式(如--help)。

2. 分支检查

在执行合并之前,脚本会:

  • 验证源分支和目标分支是否存在
  • 检查当前所在分支
  • 必要时自动切换到目标分支

3. 冲突解决策略

当遇到合并冲突时,脚本会:

  • 获取冲突文件列表
  • 对每个冲突文件:
  • 获取两个分支中该文件的最后修改时间
  • 比较时间戳,选择最新的版本
  • 自动添加解决后的文件到暂存区

4. 合并提交

  • 如果没有冲突,直接创建合并提交
  • 如果有冲突,在解决后创建带有说明的合并提交
  • 保存冲突文件列表到指定文件

完整代码

#!/bin/bash

# 启用详细日志
set -x

# 确保我们在正确的分支上
current_branch=$(git symbolic-ref --short HEAD)
echo "当前分支: $current_branch"

if [ "$current_branch" != "hbjt-test-merged" ]; then
    echo "请先切换到 hbjt-test-merged 分支"
    exit 1
fi

# 创建临时文件来存储冲突文件列表
conflict_files="conflict-files.txt"
echo "创建冲突文件列表: $conflict_files"
> "$conflict_files"

# 开始合并,但不自动提交
echo "开始合并 hb-test 分支..."
git merge --no-commit hb-test || true

# 获取所有冲突文件
echo "检查冲突文件..."
conflicted=$(git diff --name-only --diff-filter=U)
echo "发现的冲突文件:"
echo "$conflicted"

# 如果有冲突文件
if [ ! -z "$conflicted" ]; then
    echo "===== 发现冲突文件,正在根据时间戳处理... ====="
    
    # 遍历每个冲突文件
    echo "$conflicted" | while read file; do
        echo "------------------------------"
        echo "处理文件: $file"
        echo "$file" >> "$conflict_files"
        
        # 获取两个分支中文件的最后修改时间
        current_time=$(git log -1 --format="%at" hbjt-test-merged -- "$file" 2>/dev/null || echo "0")
        incoming_time=$(git log -1 --format="%at" hb-test -- "$file" 2>/dev/null || echo "0")
        
        echo "hbjt-test-merged 分支最后修改时间: $(date -d @$current_time)"
        echo "hb-test 分支最后修改时间: $(date -d @$incoming_time)"
        
        if [ "$incoming_time" -gt "$current_time" ]; then
            echo "使用 hb-test 分支的 $file"
            git checkout --theirs "$file"
        else
            echo "使用 hbjt-test-merged 分支的 $file"
            git checkout --ours "$file"
        fi
        
        # 标记文件为已解决
        echo "标记 $file 为已解决"
        git add "$file"
    done
    
    # 完成合并
    echo "===== 提交合并结果 ====="
    git commit -m "合并 hb-test 到 hbjt-test-merged,解决冲突基于时间戳"
    echo "合并完成!冲突文件列表已保存到 $conflict_files"
else
    # 如果没有冲突,直接提交
    echo "===== 没有发现冲突,直接提交 ====="
    git commit -m "合并 hb-test 到 hbjt-test-merged,无冲突"
    echo "合并完成!没有发现冲突"
fi

# 显示最终状态
echo "===== 合并结果 ====="
git status