修改后的文件无法使用Git add命令添加,除非使用-p(patch)选项。与-p选项一起使用可以成功添加。

7

在工作的前两个小时内,我能够轻松地使用git add指令添加文件,但突然间无法添加其中一个文件。

Casper@PC2015 MINGW64 /c/Workspace/edoping (develop)
$ git add .

Casper@PC2015 MINGW64 /c/Workspace/edoping (develop)
$ git status
    On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

modified:   app/Http/Controllers/API/AuthController.php

no changes added to commit (use "git add" and/or "git commit -a")

我已经尝试了很多方法来添加但都没有成功:
  • git add .
  • git add <path-to-file>
  • git add -f <path-to-file>
  • git add --all
  • git commit -a

所以我还检查了是否有子模块(如果没有我也不知道)。但似乎我也没有这些子模块。 为了找到它们,我执行了以下操作:
  • git config --file .gitmodules --name-only --get-regexp path
  • grep path .gitmodules | sed 's/.*= //'
  • git submodule status --recursive.

我还查看了 .gitignore 文件,但这仍然是默认的 Laravel 5.3 的文件。
git diff 显示了文件所做的更改,就像平常一样。
Casper@PC2015 MINGW64 /c/Workspace/edoping (develop)
$ git diff
diff --git a/app/Http/Controllers/API/AuthController.php b/app/Http/Controllers/API/AuthController.php
index 9bfe453..5add519 100644
--- a/app/Http/Controllers/API/AuthController.php
+++ b/app/Http/Controllers/API/AuthController.php
@@ -65,7 +65,12 @@ class AuthController extends \App\Http\Controllers\Controller {
     public function register(Request $request) {

         $this->validate($request, [
-            'email' => 'unique:users,email'
+            'email'      => 'unique:users,email',
+            'first_name' => 'required|min:2|max:255',
+            'last_name'  => 'required|min:2|max:255',
+            'password'   => 'required|min:2|max:255',
+            'avatar'     => 'required',
+            'birthdate'  => 'required',
         ]);

.gitattributes 和 Laravel 的默认设置并没有什么不同。

* text=auto
*.css linguist-vendored
*.scss linguist-vendored

没有嵌套的代码库:

Casper@PC2015 MINGW64 /c/Workspace/edoping (develop)
$ find -name .git
./.git

git add -p 命令将输出以下内容。

Casper@PC2015 MINGW64 /c/Workspace/edoping (develop)
$ git add -p
diff --git a/app/Http/Controllers/API/AuthController.php b/app/Http/Controllers/API/AuthController.php
index 9bfe453..5add519 100644
--- a/app/Http/Controllers/API/AuthController.php
+++ b/app/Http/Controllers/API/AuthController.php
@@ -65,7 +65,12 @@ class AuthController extends \App\Http\Controllers\Controller {
     public function register(Request $request) {

         $this->validate($request, [
-            'email' => 'unique:users,email'
+            'email'      => 'unique:users,email',
+            'first_name' => 'required|min:2|max:255',
+            'last_name'  => 'required|min:2|max:255',
+            'password'   => 'required|min:2|max:255',
+            'avatar'     => 'required',
+            'birthdate'  => 'required',
         ]);

         $request->only($this->user_fillable);
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?

回答 y 时,文件会被添加到索引中。


git add -p 为什么有效,而上述所有其他方法均无效?

编辑: 我尝试再次提交一些更改,但我发现了一些新问题。 Git似乎认为在app/Http/Controllers/下有两个文件夹,即ApiAPI,但实际只有API一个。

大约1000个提交之前,我将文件夹名从Api更改为API,因为我的上级要求这样做。这可能是问题的起源吗?

Casper@PC2015 MINGW64 /c/Workspace/edoping (develop)
$ gs
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   app/Http/Controllers/API/AuthController.php
        modified:   app/Http/Controllers/Api/AuthController.php

3
当你尝试运行 git add 命令时,Git 会说什么? - zhm
@MartinZhai ,什么都没有。 - Casper Spruit
1
有没有可能一个.gitignore规则适用于所讨论的文件? - domsson
2
所以 git add -p 确实会识别更改。如果您回答 y(是),文件最终会被添加到索引中吗? - mkrieger1
1
很遗憾,不行。好问题 ;) - mkrieger1
显示剩余13条评论
1个回答

5

尝试了很多方法后,我成功添加了更改。

我的做法:

  • git add -p将输出提示信息。
  • Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]?
  • 我对每个更改都回答y,在文件末尾添加了它们。
  • 这之后,我就能够commitpush更改了。

感谢所有人的帮助,以及@mkrieger1提出的git add -p


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接