
% git add MyClass.java
fatal: this operation must be run in a work tree
If you are trying to stage a file or stage all files using git add -A and you get a fatal error saying this operation must be run in work tree, then its most likely that you are inside the .git hidden directory while trying to run the command,
% /myproj/.git/ master ls -la
total 40
drwxr-xr-x 7 root root 4096 Aug 16 08:52 .
drwxr-xr-x 3 root root 4096 Aug 16 08:41 ..
-rw-r--r-- 1 root root 23 Aug 16 08:41 HEAD
drwxr-xr-x 2 root root 4096 Aug 16 08:41 branches
-rw-r--r-- 1 root root 92 Aug 16 08:41 config
-rw-r--r-- 1 root root 73 Aug 16 08:41 description
drwxr-xr-x 2 root root 4096 Aug 16 08:41 hooks
drwxr-xr-x 2 root root 4096 Aug 16 08:41 info
drwxr-xr-x 4 root root 4096 Aug 16 08:41 objects
drwxr-xr-x 4 root root 4096 Aug 16 08:41 refs
Fix:
Make sure you are within your project directory and not inside .git or any sub-directories as shown in the above list when you run the add command,
% git add MyClass.java
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- Github: How to Invite Collaborators
- Git Commit - Author identity unknown, Please tell me who you are email
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- Github: fatal: Authentication failed Support for password was removed on August 13, 2021
- Get List of all local branches git command
- Git Revision Questions Before the Interview
- git fatal: Authentication failed error [fix]
- How to Install Git on Ubuntu Linux
- GitHub: How to Search Code
- How to change Git Default Author and Committer details in Eclipse
- How to check your installed version of Git
- Install GitHub Command Line Tool on Mac
- How to Merge Branch into Master Branch
- Perform an Empty Commit in Git without anything in Staging Area
- Git Fix: fatal: refusing to merge unrelated histories Error
- How to undo last Git Commit on Local Repository?
- git command to remove/unstage files from staging area
- Step-by-Step: Setting up Docker + Ubuntu Linux + Git + GitHub Tutorial
- Remove git config at Local, Global or System Levels?
- Fix [oh-my-zsh] Cant update: not a git repository
- Git Config Command - A Deep Dive
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier.
- How to change directory in Git bash
- git command to remove/unstage files from staging area - Git
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json - JavaScript
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting - Java
- Read Json File and Convert to Java Object using Jackson - Java
- Add days/weeks/months/years to LocalDate in Java 8 and above examples - Java
- How to display File CR LF Newline Characters in Sublime Text - Sublime-Text
- How to add ruler in Sublime Text tab window - Sublime-Text
- Install Native Java JDK JRE on Apple Silicon M1 Mac - MacOS