当我做'git commit'时,我得到以下内容:

无法创建“project_path/.git/index”。lock `:文件已存在。

然而,当我执行ls project_path/.git/index。锁定,表示文件不存在。你觉得我该怎么做?我还注意到project_path/。git由root拥有,不确定这是否与我遇到的问题有关。

Git版本为1.7.5.4

编辑:看起来问题很可能是我正在运行的另一个进程,那就是向项目目录写入(我不知道)。我重新启动了我的电脑,然后我就没有问题了。


当前回答

在同一个本地存储库上工作的多个git客户机会竞争该锁。每个客户端都应该等待锁被另一方释放,这是一个好公民。对于我们来说,当我们运行大型提交脚本时,SourceTree或MSVS似乎正在后台进行一些维护。

也许'git'本身应该支持一个'——retriesWhenLocked 5'参数来支持重试。甚至在手动运行时默认为此。

下面是一个名为“gitr”的PowerShell包装器,它可以重新尝试直到创建索引。锁定消失,默认5次,每次3秒。它从不删除索引。锁定,假设用户应该干预。它是从一个较大的提交脚本中提取的。它只使用简单的参数进行最少的测试。

复制脚本到C:\bin,并将C:\bin添加到$PATH。 来自PS1> gitr -救命 从DOS %> powershell gitr -help

gitr.ps1

    #requires -version 2
    <#
    .SYNOPSIS
        gitr
    .DESCRIPTION
        Run "git" as an external process with retry and capturing stdout stderr.
    .NOTES  
      2017/05/16 crokusek: Initial version
    #>

    #---------------------------------------------------------[Initializations]--------------------------------------------------------

    #Set Error Action 
    $ErrorActionPreference = "Stop";

    #----------------------------------------------------------[Declarations]----------------------------------------------------------

    $scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
    #Set-Location $scriptDir

    ## Disabled logging
    # Log File 
    # $logFile = "$($scriptDir)\getr.log"
    # If (Test-Path $logFile) { Clear-Content $logFile }

    #-----------------------------------------------------------[Functions]------------------------------------------------------------

    Function Log([string]$msg, [bool]$echo = $true)
    {
        $timestamp = "$(get-date -Format 'yyyy/MM/dd HH:mm:ss'):  " 
        $fullmsg = $msg -replace '(?ms)^', $timestamp  # the (?ms) enables multiline mode

        ## Disabled Logging 
        # Add-content $LogFile -value $fullmsg

        if ($echo)
        {
            Write-Host $msg
        }
    }

    Function ExecSimple([string]$command, [bool]$echo=$true, [bool]$stopOnNonZeroExitCode=$true)
    {
        $command, $args = $command -split " "
        return Exec $command $args $echo $stopOnNonZeroExitCode
    }

    Function Exec([string]$exe, [string[]]$arguments, [bool]$echo=$true, [bool]$stopOnNonZeroExitCode=$true)
    {   
        # Passing $args (list) as a single parameter is the most flexible, it supports spaces and double quotes

        $orgErrorActionPreference = $ErrorActionPreference 
        Try
        {           
            $error.clear()  # this apparently catches all the stderr pipe lines

            if ($false -and $exe -eq 'git')  # todo make this a generic flag
            {
                $exe = "$($exe) 2>&1"
            }

            $output = ""

            $argflattened = $arguments -join ' '
            Log "`n% $($exe) $($arguments)`n"

            # This way some advantages over Invoke-Expressions or Start-Process for some cases:
            #      - merges stdout/stderr line by line properly, 
            #      - echoes the output live as it is streamed to the current window,
            #      - waits for completion
            #      - works when calling both console and windows executables.
            #       
            $ErrorActionPreference = "Continue"  # required in order to catch more than 1 stderr line in the exception

            if ($echo)
            {
                # Using "cmd.exe" allows the stderr -> stdout redirection to work properly.  Otherwise the 2>&1 runs after PS for 
                # some reason.  When a command such as "git" writes to stderr, powershell was terminating on the first stderr 
                # line (and stops capturing additional lines).
                #
                # but unfortuantely cmd has some bizarre de-quoting rules that weren't working for all cases. 
                #& cmd /c "`"" $exe $arguments "`"" | Tee-Object -variable output | Write-Host | out-null           

                # This is simplest but has some issues with stderr/stdout (stderr caught as exception below)
                #
                & $exe $arguments 2>&1 | tee -variable output | Write-Host | out-null 
            }
            else
            {           
                & $exe $arguments 2>&1 | tee -variable output | out-null 
            }

            $output = $output -join "`r`n"                  

            if ($stopOnNonZeroExitCode -and !$LASTEXITCODE -eq 0)
            {           
                throw [System.Exception] "Exit code ($($LASTEXITCODE)) was non-zero. Output:`n$($output)"
            }       
        }
        catch [System.Management.Automation.RemoteException]
        {
            $output = $_.Exception.ToString().Replace("System.Management.Automation.RemoteException:", "").Trim()

            if ($output.Contains("fatal")) 
            {
                throw 
            }

            if ($echo)
            {
                Log $output
            }
        }
        finally
        {
            $ErrorActionPreference = $orgErrorActionPreference;
        }

        if (-not $output -eq "")
        {
            Log $output $false  # don't echo to screen as the pipe above did    
        }

        return $output
    }

    Function ExecWithRetry([string]$exe, [string[]]$arguments, [bool]$echo=$true, [bool]$stopOnNonZeroExitCode=$true, 
                          [int]$maxRetries = 5, [int]$msDelay = 3000, [AllowNull()][string]$exceptionMustContain = $null)
    {
        for ($i = 0; $i -lt $maxRetries; $i++)
        {
            try
            {
                Exec $exe $arguments $echo $stopOnNonZeroExitCode
                return
            }
            catch
            {
                if (-not [string]::IsNullOrEmpty($exceptionMustContain) -and $_.Exception.ToString().Contains($exceptionMustContain))
                {
                    Log "Last Error from $($exe) is retryable ($($i + 1) of $($maxRetries))" $true
                    Start-Sleep -Milliseconds ($msDelay);
                    continue
                }

                throw
            }
        }

        throw [System.Exception] "Unable to successfully exec '$($exe)' within $($maxRetries) attempts."
    }

    Function GitWithRetry([string[]]$arguments, [bool]$echo=$true)
    {
        ExecWithRetry "git" $arguments $echo -exceptionMustContain "Another git process seems to be running"
    }

#-----------------------------------------------------------[Main]------------------------------------------------------------

function Main([string[]]$arguments)
{   
    GitWithRetry @($arguments)
}


#-------------------------------------- Startup ------------------------------------
try 
{
    Main $args
    Exit 0
}    
catch
{
    #Log "*** A fatal error occured: $($_.Exception)"
    #Read-Host -Prompt "`nA fatal error occurred, press enter to close."    
    exit 1
}

其他回答

在我的例子中,只需转到project_path/。Git并删除索引。锁文件。试着推动你的代码,它会工作的。

当双击SourceTree切换分支时,我遇到了这个问题。这个问题并不常见,Atlassian也知道这个问题,但他们决定不修复它。

幸运的是,有一个解决方案。不要双击你想要切换的分支,只需右键单击并选择“Checkout[分支名称]”。现在应该成功了。

获取错误:

Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
fatal: Unable to create '/home/user/project/.git/index.lock': File exists.

If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.

但是我找不到(也没有删除)那个。git/index。锁文件。

在我的情况下,吉特可乐正在运行!

它显然创建了。git/index。每隔一段时间就会锁一次,或者是由于我在命令行上所做的更改而导致的,在此期间我收到了这个错误-所以Git -cola显然会“干扰”Git的命令行运行(或一些Git CLI操作)。

这可以通过在命令行git rebase期间关闭git-cola来解决。

除非你真的打算让根用户拥有你的repo,否则这听起来像是你意外地以根用户身份运行了Git命令(甚至可能是初始的clone/init)。如果您打算这样做,那么您将不得不以根用户身份在repo中运行所有Git命令。如果没有,运行sudo chown your-user[:your-group] -R .git来获得它的所有权,然后看看事情是否正常。

在我的例子中,它是窗户,而不是完全关闭。

窗口已休眠,拒绝挂载

Windows很有可能真的处于休眠状态。当你告诉Windows正常关机时,它会自动关机。这样做的好处是可以获得更快的启动时间。

在没有hybernating的情况下关闭Windows,在命令提示符下发出以下命令(在Windows中):

shutdown /s

您可能还需要包含/t 0来立即关闭。

我找到了一个很好的教程来设置启动器:如何在Windows 8中完全关机而不禁用混合启动。

真正关闭Windows的更简单的方法是“重新启动”(而不是“关闭”),但随后拦截引导过程并引导Linux,而不是让它引导Windows。

信贷:nobar