You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将按文件夹版本化的软件发布版本导入Git并打标签?

批量导入历史版本到Git并打标签的简便方法

1. 整理版本文件夹

  • 将所有版本文件夹按发布时间从早到晚排序,确保版本号顺序正确(若为数字版本号,注意自然排序避免10排在2前面):
    • Linux/macOS:用ls -v命令可按自然顺序列出文件夹
    • Windows PowerShell:执行Get-ChildItem | Sort-Object { [version]$_.Name }实现版本号自然排序

2. 创建并初始化Git仓库

  • 新建一个空文件夹(例如命名为project-git),打开终端进入该文件夹后执行:
    git init
    

3. 批量导入版本并打标签(手动/脚本二选一)

手动方式(适合版本数量少的场景)

  • 复制最早版本文件夹的所有内容到project-git目录
  • 提交第一个版本:
    git add .
    git commit -m "Release <版本号>"  # 替换<版本号>为实际版本名,如v1.0
    
  • 为该版本打标签:
    git tag <版本号>
    
  • 重复上述步骤,依次导入后续版本,覆盖当前文件后完成提交与打标签操作。

自动化脚本(推荐,适配70个版本的场景)

Linux/macOS 脚本示例

创建import-versions.sh脚本,内容如下:

#!/bin/bash

# 替换为所有版本文件夹的父目录路径
VERSION_DIR="/path/to/all/version/folders"
# 替换为你的Git仓库目录路径
GIT_DIR="/path/to/project-git"

# 按自然顺序遍历版本文件夹
cd "$VERSION_DIR"
for version in $(ls -v); do
    echo "Processing version: $version"
    # 清空Git仓库内非.git的所有内容
    cd "$GIT_DIR"
    find . -maxdepth 1 ! -name '.' ! -name '.git' -exec rm -rf {} +
    # 复制当前版本文件到Git仓库
    cp -r "$VERSION_DIR/$version"/* "$GIT_DIR/"
    # 提交版本并打标签
    git add .
    git commit -m "Release $version"
    git tag "$version"
done
  • 给脚本添加执行权限:chmod +x import-versions.sh
  • 运行脚本:./import-versions.sh

Windows PowerShell 脚本示例

创建Import-Versions.ps1脚本,内容如下:

# 替换为所有版本文件夹的父目录路径
$versionDir = "C:\path\to\all\version\folders"
# 替换为你的Git仓库目录路径
$gitDir = "C:\path\to\project-git"

# 按版本号自然排序获取文件夹列表
$versions = Get-ChildItem -Path $versionDir | Sort-Object { [version]$_.Name }

foreach ($version in $versions) {
    Write-Host "Processing version: $($version.Name)"
    # 清空Git仓库内非.git的所有内容
    Get-ChildItem -Path $gitDir -Exclude ".git" | Remove-Item -Recurse -Force
    # 复制当前版本文件到Git仓库
    Copy-Item -Path "$($version.FullName)\*" -Destination $gitDir -Recurse -Force
    # 提交版本并打标签
    Set-Location $gitDir
    git add .
    git commit -m "Release $($version.Name)"
    git tag "$($version.Name)"
}
  • 打开PowerShell运行脚本:.\Import-Versions.ps1

4. 推送到GitHub实现备份

  • 在GitHub创建一个空仓库(不要勾选"Initialize this repository with a README")
  • 在本地Git仓库终端执行以下命令(替换为你的仓库地址):
    # SSH方式
    git remote add origin git@github.com:<你的用户名>/<仓库名>.git
    # 或HTTPS方式
    git remote add origin https://github.com/<你的用户名>/<仓库名>.git
    
    # 推送所有提交记录与标签
    git push origin main --tags
    

内容的提问来源于stack exchange,提问作者PentiumPro200

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 02:25:16