简体   繁体   English

如何修复:错误:'<filename> ' 没有检查出致命的提交:输入“git add”时添加文件失败。 在命令提示符下</filename>

[英]How to fix: error: '<filename>' does not have a commit checked out fatal: adding files failed when inputting "git add ." in command prompt

I'm trying to add a ruby rails file to my repository in gitlab but it somehow wouldn't allow me to add the file saying that my file does not have commit checked out.我正在尝试将一个 ruby rails 文件添加到我在 gitlab 中的存储库中,但它以某种方式不允许我添加文件,说我的文件没有提交检出。

I've tried git pull, making the the file again and git adding but still wont work我试过 git pull,再次制作文件并添加 git 但仍然无法正常工作

error: '172069/08_lab_routes_controllers_views_172069_172188-Copy/adventure_game/' does not have a commit checked out
fatal: adding files failed

If you have a subdirectory with a .git directory and try to git add .如果您有一个带有.git目录的子目录并尝试git add . you will see this message.你会看到这条消息。

This can happen if you have a git repo and then create/clone another repo in a subdirectory under that repo.如果您有一个 git 存储库,然后在该存储库下的子目录中创建/克隆另一个存储库,就会发生这种情况。

I had the same error Message.我有同样的错误消息。 I fixed it by deleting the file which causes the error from my Directory.我通过从我的目录中删除导致错误的文件来修复它。

I hope this helps =)我希望这会有所帮助 =)

You don't need to delete the entire file from the directory as the first answer suggests, I just had to delete the .git directory, and then your git add .你不需要像第一个答案所暗示的那样从目录中删除整个文件,我只需要删除.git目录,然后你的git add . will work将工作

This happens for the reason @Mario Zigliotto suggested, there is another repo in a subdirectory under that repo.发生这种情况的原因是@Mario Zigliotto 建议的,该存储库下的子目录中有另一个存储库。

I had the same Error Message.我有同样的错误信息。 I fixed it by command below:我通过以下命令修复了它:

git add folder-name/* instead of git add . git add folder-name/*而不是git add .

My Folder directory is like below:我的文件夹目录如下:

Main-folder
 -folder-one
 -folder-two

ref: https://github.community/t/error-git-add/2937参考: https : //github.community/t/error-git-add/2937

To expand on both the accepted answer from Mario Zigliotto and Albert's answer , the reason this occurs is because of submodules .为了扩展Mario ZigliottoAlbert's answer的已接受答案,发生这种情况的原因是submodules Here is a simple way to re-create the problem:这是重新创建问题的简单方法:

$ mkdir empty-submodule && cd empty-submodule
$ git init
Initialized empty Git repository in [path ending in empty-submodule/.git]
$ mkdir sub && (cd sub && git init)
Initialized empty Git repository in ... [path ending in empty-submodule/sub/.git]
$ ls
sub
$ ls -a sub
.       ..      .git
$ git add .
error: 'sub/' does not have a commit checked out
fatal: adding files failed

Note that the subdirectory sub is itself a Git repository, but no commit is checked out in this subdirectory .请注意,子目录sub本身就是一个 Git 存储库,但在此子目录中未检出任何提交 This is a simple statement of fact, true in this case because I created sub , went into it, and created a new, empty Git repository there.这是一个简单的事实陈述,在这种情况下是正确的,因为我创建了sub ,进入了它,并在那里创建了一个新的空 Git 存储库。 So that Git repository has no commits at all.这样Git仓库已经完全没有提交。

To the above fact, we add one more: No Git repository can hold another Git repository inside it.对于上述事实,我们再添加一个:任何 Git 存储库都不能在其中容纳另一个 Git 存储库。 The reason for this has to do with security, but for our purpose here, the important side effect of this fact is that an attempt to add a sub-repository to a repository (like the superproject in empty-submodule ) does not add the repository itself.这样做的原因与安全有关,但就我们的目的而言,这一事实的重要副作用是尝试将子存储库添加到存储库(如empty-submodule的超级项目)不会添加存储库本身。 Instead, it adds a reference to a commit within the repository .相反,它添加了对存储库中提交引用 This is how submodules are implemented.这就是子模块的实现方式。 1 But to refer to some commit within the submodule, the submodule itself has to have some commit checked out . 1但要参考一些子模块提交,该子模块本身已经具备一定的承诺签出

The way to fix this really depends on the result you want.解决此问题的方法实际上取决于您想要的结果。 See the next section for more information about this.有关这方面的更多信息,请参阅下一节。


1 Technically, submodules are implemented with two parts: 1从技术上讲,子模块由两部分实现:

  • Each commit in the superproject (the "outer" repository) has a reference to a commit within the submodule.超级项目(“外部”存储库)中的每个提交都有对子模块内提交的引用。
  • In order to be able to run git clone on the submodule, the outer repository should also contain a .gitmodules file.为了能够在子模块运行git clone ,外部存储库还应该包含一个.gitmodules文件。 This file will hold the instructions that tell the superproject Git how to git clone the submodule.这个文件将包含告诉超级项目 Git 如何git clone子模块的指令。 Once the submodule repository exists, the superproject Git never needs to run git clone on it again.一旦子模块存储库存在,超级项目 Git 就不再需要在其上运行git clone So it's possible to accidentally, or on purpose, omit the .gitmodules file entirely.因此,可能会意外或故意完全省略.gitmodules文件。

Doing this produces a superproject that is difficult to use.这样做会产生一个难以使用的超级项目。 I like to call this a half-assed submodule .我喜欢称其为半途而废的子模块 If it were fully-assed, the superproject would be able to clone the submodule, but since the .gitmodules file is missing, it can't.如果它是完整的,超级项目将能够克隆子模块,但由于.gitmodules文件丢失,它不能。


Fixing the problem解决问题

There are multiple ways to fix the problem.有多种方法可以解决问题。 Before you pick one, you should know whether you want to have a submodule.在你选择一个之前,你应该知道你是否想要一个子模块。 There's no one correct answer here, as the question of should I use a submodule is a bit like which flavor of ice cream should I pick: different people have different preferences.这里没有一个正确的答案,因为我应该使用子模块的问题有点像我应该选择哪种口味的冰淇淋:不同的人有不同的偏好。

If you don't want a submodule at all you will need to move or remove the .git subdirectory within the subdirectory in question, eg:如果您根本不需要子模块,则需要移动或删除相关子目录中的.git子目录,例如:

rm -rf sub/.git

(see also Nissan's answer if using PowerShell on Windows). (如果在 Windows 上使用 PowerShell,另见Nissan 的回答)。

Before you do that , you should:在你这样做之前,你应该:

  1. Determine whether you want to keep the other repository .确定是否要保留其他存储库 If so, do not remove it!如果是这样,请不要删除它! Just move it somewhere else, eg, mkdir ../keep && mv sub/.git ../keep .只需将它移动到其他地方,例如, mkdir ../keep && mv sub/.git ../keep

  2. Determine whether you need to git checkout some commit or branch before moving or removing the repository (the .git directory).在移动或删除存储库.git目录)之前,确定您是否需要git checkout某些提交或分支。 If so, enter the submodule and check out the desired commit.如果是这样,请进入子模块并检查所需的提交。

If you do want a submodule , you may need to make some commit(s) within the submodule, or check out some existing commit, just as in step 2 above.如果你确实想要一个子模块,你可能需要在子模块中进行一些提交,或者检查一些现有的提交,就像上面的第 2 步一样。 Once the submodule repository is on the correct commit, you can git add it.一旦子模块存储库正确提交,您就可以git add它。

Here is an example of creating a commit in my submodule named sub :这是在名为sub模块中创建提交的示例:

$ cd sub
$ echo this is the submodule > README.md
$ git add .
$ git commit -m initial
[master (root-commit) c834131] initial
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ cd ..

Now that the submodule has a commit, I can add it to the superproject.现在子模块有一个提交,我可以将它添加到超级项目中。 There's a bit of a hitch here though, as I should also create the .gitmodule file mentioned in footnote 1 above.不过这里有一些问题,因为我应该创建上面脚注 1 中提到的.gitmodule文件。 The git submodule add command does everything for you, but you need to know where you'll git push this submodule repository. git submodule add命令为您完成所有工作,但您需要知道将git push这个子模块存储库的位置。 For instance:例如:

$ git submodule add ssh://git@github.com/place/where/submodule/lives.git sub
Adding existing repo at 'sub' to the index

Everything is now ready to commit in the superproject:现在一切准备就绪,可以在超级项目中提交:

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitmodules
        new file:   sub

$ cat .gitmodules
[submodule "sub"]
        path = sub
        url = ssh://git@github.com/place/where/submodule/lives.git

The submodule has not actually been sent to GitHub yet.子模块实际上还没有发送到 GitHub。 Before anyone else will be able to use the superproject , I'd have to create this GitHub repository, giving it sufficient (eg, public) access, and git push the submodule commit to that GitHub repository.在其他人能够使用superproject 之前,我必须创建这个 GitHub 存储库,为其提供足够的(例如,公共)访问权限,并将子模块提交git push该 GitHub 存储库。 Then I can git push my commit in my superproject to whatever location that repository should live at.然后,我可以git push我在我的上层项目承诺的任何位置存储库应该住在。 Then you—the generic "you"—can git clone the superproject , which now has a .gitmodules file with instructions by which your Git will be able to run git clone ssh://git@github.com/place/where/submodule/lives.git sub .然后你——一般的“你”——可以git clone superproject ,它现在有一个.gitmodules文件,其中包含你的 Git 将能够运行git clone ssh://git@github.com/place/where/submodule/lives.git sub

Submodules have a bunch of usability issues, with all of the above complications being one of them.子模块有一堆可用性问题,上述所有复杂情况都是其中之一。 Be sure you know what you're getting into.确保你知道你在做什么。

Note: I assume you have a different problem, but I had the same error message and this was the first Google result, so it might be helpful to others to post my situation, problem and solution.注意:我假设您遇到了不同的问题,但我有相同的错误消息,这是第一个 Google 结果,因此发布我的情况、问题和解决方案可能对其他人有所帮助。

I have a repo with Git submodules.我有一个带有 Git 子模块的仓库。

The error occurs by git commit .错误由git commit .发生git commit . :

error: 'xyz' does not have a commit checked out
fatal: updating files failed

Then I did git submodule init , which printed:然后我做了git submodule init ,它打印了:

Submodule 'xyz' (git@github.com:albertz/...) registered for path 'xyz'
...

And then git submodule update :然后git submodule update

Cloning into '.../xyz'...
...
Submodule path 'xyz': checked out '...'
...

That fixed the error.这修复了错误。 Now git commit .现在git commit . runs fine.运行良好。

I had thesame problem and here is what I found to be the cause.我有同样的问题,这就是我发现的原因。

Under one of the subfolder I had intialized git so I had a two.git folders, one in the parent directory and the other in the subfolder在其中一个子文件夹下我初始化了 git 所以我有两个.git 文件夹,一个在父目录中,另一个在子文件夹中

To solve this I removed the git in the subfolder为了解决这个问题,我删除了子文件夹中的 git

cd project-directory/sub_folder_name

check if there is a.git folder检查是否有.git文件夹

ls -la

rm -rf .git 
cd ..
git add .
git commit -m "commit message"

I had the same error message when trying to add multiple files using "git add ."尝试使用“git add”添加多个文件时,我遇到了相同的错误消息。 I was able to add all those files one by one instead of all at the same time.我能够一个一个地添加所有这些文件,而不是同时添加所有文件。

To add to Mario's (correct) answer and resolve this type of issue which can be quite common (egin scaffolding an application using an app generator inside a repository top folder, and it generates its own .git file), you can run the following commands to cleanup and get the files checked in properly to git.要添加到Mario 的(正确)答案并解决此类非常常见的问题(例如,在存储库顶部文件夹中使用应用程序生成器搭建应用程序的脚手架,并生成自己的.git文件),您可以运行以下命令清理并将文件正确签入到 git。

cd {{appname}}
rm -rf .git 
# rm -r -fo .git # if on Windows powershell
cd ..    
git add .  

My solution ended up using:我的解决方案最终使用:

git mergetool

This brought up an interface where I could mark whether submodules should be used local or remote.这带来了一个界面,我可以在其中标记子模块应该在本地还是远程使用。

Submodule merge conflict for 'abc':
  {local}: submodule commit (hash)
  {remote}: submodule commit (hash)
Use (l)ocal or (r)emote, or (a)bort?

I tried all those solutions but none worked for me.我尝试了所有这些解决方案,但没有一个对我有用。 but the following solved my problem completely.但以下完全解决了我的问题。

git rm -r --cached .

if you want to add a different repo you can use the above solutions如果要添加不同的 repo,可以使用上述解决方案

git remote set-url origin git@gitlab.com:username/project.git

This problem occurs if there is already a .git directory in some nested folder which in most cases is the < filename > which git throws as error.如果某些嵌套文件夹中已经有一个.git目录,在大多数情况下是 git 作为错误抛出的 < filename >,则会出现此问题。

If there is already .git directory and you again use git init in some parent directory then there is some weird situation in which .git tries to track another .git directory.如果已经存在.git目录并且您再次在某个父目录中使用git init ,那么会出现一些奇怪的情况,即.git尝试跟踪另一个.git目录。

To solve this, move to that directory and then use rm -rf .git .要解决此问题,请移至该目录,然后使用rm -rf .git This will remove the .git directory in nested folder and then you can use git add --all or any other command.这将删除嵌套文件夹中的.git目录,然后您可以使用git add --all或任何其他命令。

NOTE : Deleting .git will erase all local tracking information of that folder eg local commits or branches.注意:删除.git将删除该文件夹的所有本地跟踪信息,例如本地提交或分支。

在我的情况下,我只是不允许在分支内推送,所以我不得不创建另一个并发出拉取请求

The folder has a .git folder, due to the .git folder 📁 , it causes the error.该文件夹有一个.git文件夹,由于.git文件夹📁,它会导致错误。 So just delete that folder and then try again.所以只需删除该文件夹,然后再试一次。 It will be working.它将起作用。

Note: It is a hidden folder, so just select Hidden items in windows setting注意:这是一个隐藏文件夹,所以只需在windows设置中选择隐藏项目

If you have both backend and frontend folder navigate to those folders and run this command on both directories如果您同时拥有后端和前端文件夹,请导航到这些文件夹并在两个目录上运行此命令

$ rm -rf .git

then navigate back to your main directory然后导航回您的主目录

Today I was stuck in this problem as well, managed to fix it with this solution:今天我也陷入了这个问题,设法用这个解决方案解决了这个问题:

git rm -r --cached .

after that you can add the different Github Repo之后,您可以添加不同的 Github 回购

git remote set-url origin git@gitlab.com:.../thisIsYourProject.git

hope this helps, happy coding!希望这会有所帮助,快乐的编码!

Just navigate to that suddir and do git add.只需导航到那个 suddir 并执行 git add。 and alos run git commit plus do the pushing aswell.并且还运行 git commit 并进行推送。 After that navigate back to your root dir and do the git add.之后导航回您的根目录并执行 git add。 again.That solved my issue again.That 解决了我的问题

使用git add folderName/而不是git add folderName

I'm trying to add a ruby rails file to my repository in gitlab but it somehow wouldn't allow me to add the file saying that my file does not have commit checked out.我正在尝试将ruby rails文件添加到我在gitlab中的存储库中,但是以某种方式不允许我添加该文件,说我的文件没有提交签出。

I've tried git pull, making the the file again and git adding but still wont work我试过git pull,再次制作文件并git添加,但仍然无法正常工作

error: '172069/08_lab_routes_controllers_views_172069_172188-Copy/adventure_game/' does not have a commit checked out
fatal: adding files failed

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在git命令提示符下键入“ git add”时,收到致命消息,即不是git存储库。 - I am getting fatal message i.e not a git repository, when I am typing “git add .” in git command prompt. Git推送错误,致命:HTTP请求失败 - Git push error, fatal: HTTP request failed 如何修复错误:/mnt/x/coding/webapp/.git/config.lock 上的 chmod 失败:不允许操作致命:无法将 'core.filemode' 设置为 'false' - how to fix error: chmod on /mnt/x/coding/webapp/.git/config.lock failed: Operation not permitted fatal: could not set 'core.filemode' to 'false' 安装活动管理员时,activeadmin.git(在主服务器上)尚未检出错误 - activeadmin.git (at master) is not yet checked out error when installing active admin 未签出... bundle install 不修复帮助! - is not checked out... bundle install does NOT fix help! 如何从git commit历史记录中删除文件? - How to remove files from git commit history? 如果未检出“来自git存储库的gem”,如何解决错误。 请运行“捆绑安装” - How to resolve error where gem from git repository “is not checked out. Please run `bundle install`” 在命令提示符下执行“捆绑安装”时出错 - Error when executing the “bundle install” in the command prompt 我几乎每个git / rails命令都必须使用“ sudo”,否则我将获得拒绝权限。 我该如何解决? - I have to use “sudo” for almost every git / rails command or I get permission denied. How can I fix that? 如何修复 FATAL:数据库“school-grades-project”不存在 - How to fix FATAL: database “school-grades-project” does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM