Git-TFS 是一个 Git 和 Microsoft Team Foundation Server (TFS) 之间的双向桥接工具,允许你使用 Git 作为 TFS 的客户端。以下是 Git-TFS 的基本使用方法:
1. 安装 git-tfs
git-tfs
依赖于 Git 和 .NET Framework,确保你已经安装:
- Git(可以使用 Git for Windows)
- .NET Framework 4.5 及以上(Windows 平台)
git-tfs
本身(可以用 Chocolatey 或手动下载)
方法 1:使用 Chocolatey(推荐)
如果你使用 Windows 并安装了 Chocolatey,可以直接运行:
choco install gittfs
方法 2:手动安装
- 从 GitHub 下载最新版本并解压到你的路径中,下载最新的
git-tfs.zip
。:https://github.com/git-tfs/git-tfs/releases
- 解压到本地目录(如
C:\git-tfs
)。 - 把
git-tfs.exe
所在目录添加到系统的环境变量PATH
。
git tfs --version
如果正确安装,你会看到 git-tfs
的版本信息。
2. 克隆 TFS 代码库到 Git
git-tfs
允许你将 TFS(TFVC)代码库克隆到 Git 进行本地管理。
git tfs clone http://tfs.example.com:8080/tfs/YseProj "$/Yse-Ecommerce/YseStore-Common" "D:\Code\TfsCode"
这个命令会:
- 从 TFS 服务器拉取 TFVC 代码。
- 在本地创建一个 Git 仓库,并映射 TFVC 的历史记录。
3. 在 Git 中进行开发
git checkout -b my-feature-branch
git add . git commit -m "My changes"
4. 拉取最新的 TFS 更新
如果 TFS(TFVC)上的代码有更新,你需要同步它:
git tfs pull
这个命令相当于 git pull
,会把 TFS 上的新提交拉取到 Git。
5. 将 Git 代码提交回 TFS
当你在 Git 里完成了一些提交后,你可以使用 git tfs checkin
将它们推送到 TFS:
git tfs checkin -m "My commit message"
这个命令会:
- 把你的 Git 提交转换为 TFS 的 changeset 并推送到 TFS。
如果你有多个 Git 提交,但只想推送部分:
git tfs checkin --autosquash
这个命令会把所有的 Git 提交合并成一个,并提交到 TFS。
其实我们最常用的就是获取和推送命令,如果你不想希望每次都输入命令进行操作,那么可以使用下面的脚本:
git-tfs-pull.ps1
# 脚本: git-tfs-pull.ps1
# 用途: 从TFS拉取最新更新到Git仓库
Write-Host "=== Starting to pull latest updates from TFS ===" -ForegroundColor Green
# 保存当前工作目录路径
$currentDir = Get-Location
try {
# 执行git tfs pull命令
Write-Host "Executing: git tfs pull" -ForegroundColor Cyan
git tfs pull
if ($LASTEXITCODE -eq 0) {
Write-Host "✓ Successfully pulled updates from TFS" -ForegroundColor Green
} else {
Write-Host "✗ Failed to pull updates from TFS, error code: $LASTEXITCODE" -ForegroundColor Red
}
} catch {
Write-Host "✗ Error occurred: $_" -ForegroundColor Red
} finally {
# 恢复工作目录
Set-Location $currentDir
}
Write-Host "=== Operation completed ===" -ForegroundColor Green
git-tfs-checkin.ps1
# 脚本: git-tfs-checkin.ps1
# 用途: 将Git代码提交回TFS
param(
[Parameter(Mandatory=$false)]
[string]$CommitMessage = ""
)
Write-Host "=== Starting to check in code to TFS ===" -ForegroundColor Green
# 如果没有提供提交消息,则请求用户输入
if ([string]::IsNullOrEmpty($CommitMessage)) {
$CommitMessage = Read-Host "Please enter commit message"
}
# 保存当前工作目录路径
$currentDir = Get-Location
try {
# 执行git tfs checkin命令
Write-Host "Executing: git tfs checkin -m `"$CommitMessage`"" -ForegroundColor Cyan
git tfs checkin -m "$CommitMessage"
if ($LASTEXITCODE -eq 0) {
Write-Host "✓ Successfully checked in code to TFS" -ForegroundColor Green
} else {
Write-Host "✗ Failed to check in code to TFS, error code: $LASTEXITCODE" -ForegroundColor Red
}
} catch {
Write-Host "✗ Error occurred: $_" -ForegroundColor Red
} finally {
# 恢复工作目录
Set-Location $currentDir
}
Write-Host "=== Operation completed ===" -ForegroundColor Green
可以将脚本放在由git托管的项目根目录下,最简单的使用方式:右键powershell运行
或者你也可以下载一个powershell插件,以vscode为例:
然后直接点击脚本即可
6. 处理 TFS 更新(冲突解决)
如果 TFS 上的代码有更新,而你本地的 Git 也有改动,你可能需要进行合并:
git tfs pull --rebase
这样可以让你的 Git 分支保持与 TFS 一致,同时保留你的更改。
如果遇到冲突:
- 手动解决冲突。
- 运行
git add .
- 运行
git rebase --continue
7. 将本地 Git 分支推送回 TFS
如果你在 Git 里创建了一个新分支,并希望将其推送到 TFS:
git checkout my-feature-branch git tfs checkin -m "Feature complete"
但 git-tfs
主要用于在 主分支(Main) 上进行同步,因此如果你要管理多个分支,建议使用 git tfs branch
命令:
git tfs branch --init "$/MyProject/Branches/MyFeatureBranch"
然后你可以在本地切换到这个 TFS 分支:
git checkout -b MyFeatureBranch tfs/MyFeatureBranch
8. 提高 git-tfs
的克隆速度
如果 TFS 代码库很大,克隆时可能会很慢,你可以:
- 只克隆最近的提交:
git tfs clone http://tfs.example.com:8080/tfs/defaultcollection "$/MyProject/Main" --changeset=100
- 使用
--branches=none
避免克隆所有分支:git tfs clone http://tfs.example.com:8080/tfs/defaultcollection "$/MyProject/Main" --branches=none
总结
Git-TFS 操作 | 命令 |
---|---|
克隆 TFS 代码库到 Git | git tfs clone URL "$/TFS路径" |
拉取最新的 TFS 代码 | git tfs pull |
提交代码到 TFS | git tfs checkin -m "提交信息" |
只克隆最近的 N 个更改集 | git tfs clone URL "$/TFS路径" --changeset=N |
解决冲突后继续合并 | git rebase --continue |
只拉取主分支 | git tfs clone URL "$/TFS路径" --branches=none |