Search code examples
gitgit-add

Git add on modified file not working, except with -p (patch)


After the first two hours of working all of the sudden, I can't seem to git add one file.

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")

I have tried a lot if ways to add without success:

  • git add .
  • git add <path-to-file>
  • git add -f <path-to-file>
  • git add --all
  • git commit -a

So I also have checked if I have submodules (which would be without me knowing about it). And I didn't seem to have those either. To find them I did:

  • git config --file .gitmodules --name-only --get-regexp path
  • grep path .gitmodules | sed 's/.*= //'
  • git submodule status --recursive.

I also looked into the .gitignore file, but this is still the default .gitignore from Laravel 5.3


git diff shows the changes made to the file like normal

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 doesn't show anything different from the Laravel defaults either.

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

There are no nested repositories:

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

git add -p gives me the following output.

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,?]?

When responding with y, the file does get added to the index.


Why does git add -p work, while all other methods described above do not?

EDIT: So I tried to commit some changes again, but I did find something new. Git seems to think there are two folders inside app/Http/Controllers/, which are Api and API. But there is only API.

Around a thousand commits back I did change the folders name from Api to API because I had to from my senior. Could this be the origin of the problem?

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

Solution

  • After trying a lot of different things I was able to add the changes.

    What I did:

    • git add -p which will output a prompt.
    • Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]?.
    • I answered y for every change and at the end of the file, it was added.
    • After this I was able to commit and push the changes.

    Thank you all for the help and @mkrieger1 for coming with git add -p