
% 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
More Posts related to Git,
- How to ignore files in git using .gitignore file
- How to update expired GitHub token on local git remote config
- How to undo last Git Commit on Local Repository?
- Git Commit - Author identity unknown, Please tell me who you are email
- Git: Step-by-Step - How to Push Local Brach to GitHub
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- Remove git config at Local, Global or System Levels?
- Change the default git branch name from master to main
- fix fatal: --local can only be used inside a git repository error
- Git Remove Untracked Files using Command
- How to remove or unstage a file from git staged area
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Fix [oh-my-zsh] Cant update: not a git repository
- Get List of all local branches git command
- Github: fatal: Authentication failed Support for password was removed on August 13, 2021
- [fix] fatal: this operation must be run in a work tree in git
- How to Install Git on Windows
- Perform an Empty Commit in Git without anything in Staging Area
- Rename git branch on Local and GitHub Remove using Command
- Fix: error: could not lock config file /etc/gitconfig: Permission denied
- Get the file location of git config values
- Git: Delete Branch Locally and Remotely at Origin
- 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.
- Github: How to Invite Collaborators
- How to change directory in Git bash
More Posts:
- PHP Base64 encoding decoding a variable - PHP
- Plug-in com.android.ide.eclipse.adt was unable to lead class Error - Android
- [fix] Java NullPointerException ComparableTimSort countRunAndMakeAscending when sorting a List - Java
- Trace using cURL Command Example - cURL
- Replace delimiter with new line in Notepad++ - NotepadPlusPlus
- Calculate discount amount python code - Python
- The default interactive shell is now zsh. [macOS] - MacOS
- Take Screenshot on Mac OS X (Keyboard Shortcuts) - Mac-OS-X