Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-add

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
like image 503
Casper Spruit Avatar asked Mar 02 '17 10:03

Casper Spruit


People also ask

How do you git add all modified files?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.

Why git add command is not working?

The git add command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add will fail with a list of ignored files. Ignored files reached by directory recursion or filename globbing performed by Git (quote your globs before the shell) will be silently ignored.

Does git add include untracked files?

Second, git add . adds both tracked and untracked files.

How do I add modified files to github?

To add and commit files to a Git repository Create your new files or edit existing files in your local project directory. Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed.


1 Answers

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

like image 57
Casper Spruit Avatar answered Sep 22 '22 16:09

Casper Spruit