git index.lock文件在我尝试提交时存在,但无法删除该文件


196

当我执行“ git commit”时,得到以下信息:

fatal: Unable to create 'project_path/.git/index.lock': File exists.

但是,当我这样做时ls project_path/.git/index.lock,该文件不存在。对我应该怎么做有什么想法?我还注意到project_path / .git是root拥有的,不确定是否与我遇到的问题有关。

git版本是1.7.5.4

编辑:似乎问题很可能是我正在运行的另一个进程,该进程正在写入(对我而言未知)到项目目录。我重新启动计算机,然后提交就没有问题了。


3
Git假定由于无法创建文件而已存在,这很可能是一个权限问题。您是否尝试过获取目录所有权或使用sudo执行命令?

1
我认为您对另一个访问git repo的应用程序的解释是正确的。在重新设置期间遇到了相同的问题。Gitx正在运行。一旦我退出,git就会正常工作。
谁的人

2
@asahi:您是否想接受答案?这将对将来的读者有所帮助。
MERose 2015年


3
@asahi:您可以发布编辑内容(这是解决方案)作为答案,然后接受。(尽管比“重新启动机器”更通用的解决方案是,另一个进程正在访问目录;重新启动只是为了试图找出哪个和原因而打断了Gordian的结。)在我的情况下,这是我的IDE。)无论如何,人们在找到自己的解决方案时会经常回答自己的问题,而您确实做到了。
威尔逊F

Answers:


326

这可能是一个旧的答复,但我希望这对于需要此解决方案的下一个人更加有用。

在linux / unix / gitbash / cygwin上,尝试

rm -f .git/index.lock

在Windows命令提示符上,尝试:

del .git\index.lock


1
我看到有时锁定文件会自动删除。有什么线索为什么有时需要手动删除此文件?
Nrj 2015年

我没有index.lock,怎么办?:(
Alex C

56
鉴于问题中的问题在于他无法删除文件,为什么您认为尝试删除文件应该是解决方案?
2013年

4
对我来说,关闭和打开SourceTree可以解决此问题。
安德鲁

1
@skyking在原始问题中存在一个错误,提示fatal: Unable to create 'project_path/.git/index.lock': File exists.说“文件存在”,删除它将是简单的解决方案。我为什么建议删除一个文件,即使它不是原来的问题呢?
瑞安S

40

对于Windows:

  • 从以管理员身份打开的Powershell控制台中,尝试
> rm -Force ./.git/index.lock
  • 如果这样不起作用,则必须杀死所有git.exe进程
> taskkill /F /IM git.exe
SUCCESS: The process "git.exe" with PID 20448 has been terminated.
SUCCESS: The process "git.exe" with PID 11312 has been terminated.
SUCCESS: The process "git.exe" with PID 23868 has been terminated.
SUCCESS: The process "git.exe" with PID 27496 has been terminated.
SUCCESS: The process "git.exe" with PID 33480 has been terminated.
SUCCESS: The process "git.exe" with PID 28036 has been terminated.
> rm -Force ./.git/index.lock

1
由于参数名称“ f”不明确,因此无法处理参数。
3pitt

谢谢@MikePalmice,我更新为-Force。似乎他们更改了API
Andrei Epure

20

在运行Visual Studio 2015 RC(v4.6.00057)和SourceTree(v1.6.14.0)的Windows平台上,也会出现此错误。

解决方案:假设您要使用源代码树作为源代码管理器,只需在Visual Studio中禁用源代码控制提供程序,如下所示:

  1. 转到:工具>选项>源代码管理
  2. 选择当前源代码控制插件为:无

即使我的VS甚至都不应该访问这些存储库,但无论如何,这都是使用SourceTree进行基础调整时的问题。
Kajetan Abt

谢谢你,问题仍然存在与更新3
Elger在一起Mensonides

关闭Visual Studio也可以(删除index.lock文件。)
misterbee

10
  1. 检查git是否仍在运行(ps -ef | grep git)
  2. 如果没有,请删除锁定的文件
  3. 如果是,请首先终止git进程。


6

刚遇到这个问题... Gitbox出了问题。因此,也许您运行的GUI引起了问题。


它不是GUI,但是我有一个单独的过程正在写入项目目录。我想不通,这让我发疯。
朝日2012年

看起来GitX也喜欢引起此问题。
Glutexo

6年后,对我来说是Atom
Milk Milk

6

当您确实取消中间的原点拉动时,就会发生这种情况。

因此,您可以做的是从.git目录中手动删除index.lock文件。

rm -f ./.git/index.lock

cd进入项目目录并运行此命令。


8
鉴于问题中的问题在于他无法删除文件,为什么您认为尝试删除文件应该是解决方案?
2013年

+1 @skyking。删除文件很明显,问题是没有文件要删除并且问题仍然存在。
Catsunami

6
  1. 关闭每个可能影响此.git / index.lock文件的窗口
  2. 删除.git / index.lock文件。
  3. 打开命令行编辑器,并cd到git文件的位置。

(如果创建了文件,只需从cd到那个位置,那么问题就出在您的编辑器上。关闭您的编辑器。不要再次使用此编辑器来执行此任务。打开另一种类型的编辑器-Windows power shell或cmd。现在您可以使用git命令继续)


5

ls命令可能(发生在我身上)说它不存在,因为当前用户没有访问该目录或文件的权限。

删除锁,并确保与正确的用户一起执行git,以避免权限问题。

如果您使用sudo命令位于GNU / Linux框中:

须藤rm project_path / .git / index.lock


在Windows中,可以通过右键单击->属性->属性来检查文件夹是否为只读。
马特

鉴于问题中的问题是文件不存在,为什么您认为尝试删除文件应该是解决方案?
凌晨

@skyking权限问题显示相同的错误。的确,因为标题我来到了这个问题。我写了我的答案作为一种可能的解决方案,一些选票证实了它对其他人也是如此;)
caligari

@caligari不完全是。权限问题给ls project_path/.git/index.lock
凌晨


4

有时,当您进行任何更改时或最有可能在使用子模块时,Git会创建一个与您的回购关联的锁定文件。该错误消息将向您显示锁定文件的路径。修复:只需手动转到终端中的路径并通过$ rm index.lock删除锁定文件

应该会有所帮助。


4

通过双击切换分支时,SourceTree存在此问题。这个问题不是很普遍,Atlassian知道这个问题但是他们决定不解决它。

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


谢谢,右键单击>结帐可以替代。该错误消息非常容易引起误解,尤其是当index.lock不存在时。
欧内斯特(Ernest)

4

我也遇到过同样的情况。我什至没有对本地代码进行任何更改。我刚刚编辑了一个文件并还原了它。我只是删除了隐藏的.git文件夹中的以下文件。有效!

project_path / .git / index.lock


3

除非您实际上打算让root拥有您的存储库,否则这听起来像您不小心以root身份运行了Git命令(甚至可能是初始的clone / init)。如果您打算这样做,那么必须以root身份运行仓库中的所有Git命令。如果您没有这样做,请运行sudo chown your-user[:your-group] -R .git它的所有权,然后查看一切是否正常。


在我来说,我已经搞砸了文件和目录内的模式.git,我和纠正他们:find .git -type f -exec chmod 644 {} \;而且find .git -type d -exec chmod 755 {} \;我从一台计算机移动我的Git项目时,对方已经搞砸模式
user3405291

就我而言,我向.git文件添加了写入权限sudo chmod g+w .git -R
Beatriz Fonseca

2

在同一本地存储库上工作的多个git客户端争夺该锁。每个客户都应等到对方解除锁定才能成为好公民。对我们来说,当我们运行大型提交脚本时,SourceTree或MSVS似乎正在后台进行一些维护。

也许'git'本身应支持'--retriesWhenLocked 5'参数以支持重试。甚至在手动运行时默认为默认设置。

这是一个名为gitr的git周围的PowerShell包装,该包装重试直到index.lock消失为止,使用默认的5次尝试,每次间隔3秒。假设用户应进行干预,它永远不会删除index.lock。它是从较大的提交脚本中提取的。它只有很少的带有简单参数的测试。

  • 将脚本复制到C:\ bin并将C:\ bin添加到$ PATH。
  • 从PS1> gitr --help
  • 从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
}

2

我在Windows 10中也有这个问题。

当我尝试del时./.git/index.lock,它告诉我cannot remove 'index.lock': Device or resource busy

我终于有了原因:

计算机有两个使用git的过程:

  • git bash
  • cmder

所以我用cmder.exe git commit会发生错误。

所以解决方案是使用git bash或终止git bash然后使用cmder.exe


1

我遇到了完全相同的错误,但问题不是锁定文件。相反,问题是我已将另一个git repo的内容复制到此repo中,包括.git invisible文件夹。因此,SourceTree困惑于我想将文件暂存到哪个存储库中(我以为所在的repo SourceTree与我的嵌入式.git dir内容应该存储的存储库之间存在不匹配)。


1

我在Windows上使用Cygwin的TortoiseGit遇到了这个问题。即使具有管理特权,我也无法删除remove ./.git/index.lock,我尝试了Cygwin和命令提示符,它说该文件正在由另一个进程使用。

我发现我有2个TortoiseProc.exe实例正在运行。我杀死了其中一个,并关闭了所有的Windows资源管理器窗口,然后能够删除该文件。我不知道杀死TortoiseProc.exe实例是解决方案还是关闭Windows资源管理器窗口。


1

对我来说,解决方案是删除.index文件,并允许Git重建另一个文件。


1

我没有要删除的inex.lock文件,但是对我有用的是从文件夹“属性”对话框的“属性”窗口中删除“只读”检查。




1

在我的sourceTree应用程序中,我无法提交或切换到任何其他提交/分支。那个时候显示错误

致命的:无法创造等等等等

我只是通过转到.git文件夹(在项目Explorer Dir中)解决了这个问题。并删除索引----- [文件类型:LOCK file]。现在,我取回sourceTree中的所有访问权限。

请确保索引锁定文件。假设您没有获得文件类型,请在计算机中更改文件视图设置。注意:.git文件夹通常是隐藏的文件夹类型。


1

对我来说是什么:

git rebase --abort 并重新启动基准。

正如安德鲁提到的那样,发生这种情况时我也在使用PHPStorm。不必关闭它。


1

首先,您必须导航到项目的特定文件夹。.例如,如果您的项目名称是Firstproject,则首先转到项目的目录。.然后键入cd .git,然后导航到git文件夹后,键入del index.lock之后删除文件index.lock ..您将能够像以前一样提交和推送


1

就我而言,那是窗户,没有完全关闭。

Windows处于休眠状态,拒绝挂载

Windows确实可能处于休眠状态。当您告诉它正常关闭时,Windows会自动执行此操作。这样做的好处是可以缩短表观启动时间。

要关闭Windows而不会休眠,请在命令提示符下(在Windows中)发出以下命令:

shutdown /s

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

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

实际关闭Windows的更简单方法是“重新启动”(而不是“关闭”),然后拦截启动过程并启动Linux,而不是让它启动Windows。

信用nobar


1

如果您使用备用命令行git客户端(例如hub),也会发生这种情况。

几年来,我一直在使用hub作为git的别名替代,但是最近编写了一个bash脚本,在其中执行了很多git工作,并开始出现此索引锁定问题。

直到我记得我正在运行hub而不是git之前,我找不到修复程序。我删除了,问题就消失了!


0

得到错误:

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.lock文件。

就我而言,git-cola正在运行!

显然,它每隔一段时间创建一次.git / index.lock,或者是由于我在命令行中执行的变基导致的,而在此期间我收到了该错误-因此git-cola显然“扰乱了” Git的命令行运行(或某些Git CLI操作)。

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


0

有时,当安装了多个客户端时,另一个Git客户端可能会干扰。

就是 请务必先使用任务管理器或Get-ProcessTGitCache从TortoiseGit不活跃的背景。


0

我最近有同样的问题。如果您将检查整个错误消息,它还表示某些进程正在使用git进程阻止您删除index.lock。您可能已打开IDE,例如Visual Studio或git集成到其中的相关软件。关闭它,然后尝试重新存储您的文件。希望能帮助到你。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.