在 Git 中,合并一些文件后,我想接受合并中剩余的所有"theirs"。我该怎么做?



一旦我合并了一些文件,我如何接受所有尚未合并的文件(我们的或他们的(?

我试过

git checkout --theirs Projectfolder*.json

但这似乎并没有起到任何作用。


这与现有问题不同,因为我只想接受剩余的文件。并非所有文件(这通常是要问的问题(

如果你只想要一个尚未解析的文件的列表,git ls-files -m,并且如果你想获得这些文件的传入分支版本而不进行任何自动合并,比如签出——他们的,最有效的方法是

git ls-files -u | sed -n 's,3t,0t,p' | git update-index --index-info
git checkout-index -af

这是"列出所有未解决的索引条目。选择已解决的"他们的"条目,更新所选的索引条目,现在更新任何过期的工作树副本"。

要解决此问题,您可以创建一个PowerShell脚本,获取要执行Git命令的文件夹中的所有文件,并在每个文件上迭代运行Git命令。

这是我编的一些代码。请注意,我没有用Git命令测试代码,尽管它应该按原样运行。用您选择的名称保存上面的代码,例如:"AutoMergeMyFiles.ps1";。

要执行它,您需要转到PowerShell命令窗口,并按照参数粘贴此脚本文件的路径。

它应该是这样的:

C:UsersYourNameDesktopAutoMergeMyFiles.ps1 "folderpath of the files to be merged" "operation (ours / theirs)" and finally the file filter (eg: *.json)

示例:C:\Users\YourName\Desktop\AutoMergeMyFiles.ps1"C: \GitFiles\MyProject"他们的*.json

param (
[parameter(Mandatory = $true)] [ValidateScript({
if ( -Not ($_ | Test-Path) ) {
throw "File or folder does not exist"
}
return $true
})]
[string]$mergePath,
[parameter(Mandatory = $true)] [ValidateSet("ours", "theirs")]
[string]$operation,
[parameter(Mandatory = $false)]
[string]$filter
)
$correctOperation = ""
if ($operation.ToString().Contains("ours") ) {
$correctOperation = "--ours"
}
elseif ($operation.ToString().Contains("theirs")) {
$correctOperation = "--theirs"
}
# Get-ChildItem -Path $mergePath -File -Recurse -ErrorAction SilentlyContinue -Force | Select-Object -ExpandProperty FullName
# Get-ChildItem -Path $mergePath -Recurse -ErrorAction SilentlyContinue -Force
$files = Get-ChildItem -Path $mergePath -File -Recurse -ErrorAction SilentlyContinue -Force | Select-Object -ExpandProperty FullName
if ($filter.Length -gt 0) {
$files = Get-ChildItem -Path $mergePath -Filter $filter -File -Recurse -ErrorAction SilentlyContinue -Force | Select-Object -ExpandProperty FullName
}
foreach ($file in $files) {
# Write-Output "git checkout $($correctOperation) $($file)"
git checkout $correctOperation $file
Start-Sleep -Milliseconds 150
}
Write-Output "Finished merging the files!"

如果您想手动合并一些文件,然后从--theirs中提取其余文件,请通过进行合并并暂存所有合并的文件

git add path/to/merged.file

完成手动合并后,从--theirs:中获取所有剩余内容

git checkout --theirs .

(如果您想保留所有文件/文件夹(

git checkout --theirs directory_name/*

(如果您想要特定的文件夹(

现在像其他合并一样继续。

相关内容

最新更新