remove .git/index.lock': Permission denied - macos

I'm completely stuck as to why my git has completely locked me out. I have a laptop that I use at work and when I'm home. For both accounts I use git extensively and both are located in different paths. Today I came into work and I can't do anything, all I see is:
/Applications/MAMP/htdocs/my_site/.git/index.lock': Permission denied
For all I care the branch I'm on can be deleted. I've tried removing the branch, checking out any other branch, removing the index.lock file (as suggested by other users on sites). I only have the terminal window open, no other possible programs using git (as far as I know and nothing noticeable in the activity window). I have rebooted the computer straight into my work account and still no luck. How can I remove this lock?

I had the exact same problem wanting to commit my changes to git, and solved it this way:
I needed to change the group of my .git folder and its contents:
sudo chown -R <owner>:<group> .git
needed to change the permission of this folder:
sudo chmod -R 775 .git

Check which user owns the git lock:
ls -la /Applications/MAMP/htdocs/my_site/.git/index.lock
Then you can use sudo to remove it.

After updating xcode you are maybe asked for agreeing to the new license.
git init
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
When you do this
sudo git init
you'll get a root user .git dir
drwxr-xr-x 10 root XXXX 340 25 Sep 12:40 .git
If you call other git commands which create files, these files are also created for the root user.
Change the permissions or remove .git if you don't need it yet.
=> Don't ever call git with sudo!!!
If your are asked to just call git on an empty directory
mkdir foo
cd foo
sudo git init

I had this issue as I running the following command
sudo git fetch && git checkout<branch>
Note the second sudo was missing. Running the following solved the issue:
sudo git fetch && sudo git checkout<branch>
You shouldn't have to change the owner ship of the .git directory when running sudo.

I was experiencing the same issue when trying to update the submodules of my repo:
$ git submodule update
fatal: Unable to create '.../.git/modules/deps/luajit/index.lock':
Permission denied
Unable to checkout '04dc64b558025e76a820f89a8e41840bf8269f32' in
submodule path 'deps/luajit'
It seems the problem was the submodules belonged to a different user, so I set back the ownership to me:
cd .git/modules/
chown -R user.group *

My computer system is Windows. When I open WSL remote vscode and local vscode in this path, system show this error. I closed remote vscode WSL and it solved this problem.

Change the permissions from root to the current user for the /.git folder
sudo chown username .git

Checking permission is surely the way. I faced this error when logged in with the wrong account. So, answer from my personal experience is:
"Be sure you logged in with the correct account".

Related

Git repo returns dubious ownership when not run as admin

Last week I created a git repo for a project. This morning, I tried git status which results in fatal: detected dubious ownership in repository at 'C:/ProjectPath/ProjectName'.
I believe I created the git repository using an administrator command prompt, so I tried git status in an administrator command prompt and it works. The same results happen when I use the Git Bash: it works when run as an administrator.
How can I change the repo's ownership to allow non-administrative usage? This SO question suggests multiple solutions using sudo; however, these are not applicable to git for windows: sudo echo test returns bash: sudo: command not found.
These two answers contain details on how to change owner of directory or file in Windows:
https://stackoverflow.com/a/73265433/1339137
https://stackoverflow.com/a/73875183/1339137

Permission denied when creating symbolic links in git

there is a similar question, but the difference here is that I am working on Windows. I am running git on Windows (working in git bash tool) and I have successefully cloned my forked repository. Now I need to make symbolic link and it gives me Permission denied. I can make new dir for example in .git folder and I have also set chmod /R 777 for .git directory, so it seems I have permissions.
I tried to run:
rm -rf hooks and then ln -s ../git_hooks hooks.
You might be running into a basic incompatibility of ln on windows platforms (in this case within MINGW-MSYS). You can replace ln with a version that "does the right thing" on Windows; have a look at this:
Git Bash Shell fails to create symbolic links
I had this issue on Windows and I did a couple of steps to resolve this :
Enabled core.symlinks by modifying git config
git config --edit
Opened Git Bash as an Administrator and executed the git checkout command
This worked for me, hope it helps someone.
If you have are using Git for Windows SDK, you can install winln using the pacman package manager by running: pacman -Su winln
Then you can run: winln -s target link, it will check that you have SeCreateSymbolicLinkPrivilege, otherwise it will fail. This is equivalent to running Window's mklink.exe.
I am surprise Git for Windows, does not ship with winln vs an ln command that does a copy without telling you that it did a copy.
When installing git-bash for windows, there is an option called "Enable symbolic links". When I installed git-bash with that option I had the same issue as OP. So I installed the same setup (git-bash) again with that "Enable symbolic links" option unchecked. Then my issue was resolved.
If you are using a local web server on Windows (OpenServer, Laragon, Local), then start the server as an administrator. It worked for me

Git status only showing newly created files after becoming root

I have an empty repo and cloned it on my local machine.
Next I have created created some files.
When running git status in the terminal (in the project directory), no files show up as user, but when I run it as root they are visible.
I've installed git via the git installer for OSX, installed XCode command line tools, and added git to $PATH.
I'm also using the Atlassian SourceTree app.
Same problem, but when I run this app as root, it sees the new files.
Anybody got an idea of what can be the problem?
Best,
Richard
You should not run git as root user, and you should never ever run git as root user on a project which your normally use as normal user.
Running git as root will create files belonging to root (e.g. .git/index, ...) which your normal user won't be able to update later.
Solution:
run chown to restore the correct ownership
stop using git as root.
Do you have permission as user to view the folder files? sounds like the permissions are for root only
ls -la in terminal will show you the group and owner. If your user is not in the group or not the owner you will not be able to read the folder content.
How to fix?
sudo chmod 655 .
how to verify the problem?
Open terminal as user and try to list the file content. If you can cant see the files change the group or permissions of the folder so everyone (or whoever you choose) can read/write to this folder.

Git commands require sudo on OSX

I've moved back from my work MBP to my home MBP. The work one allowed to run git commands without the need for sudo. But I'm finding this is a requirement on my home machine.
I would prefer to be able to run git commands without sudo.
I looked at the git commands in /usr/bin, and they all have rwxr-xr-x (755), and are owned by root and group wheel. I suppose I could change this to rwxrwxrwx (777), but I'm wondering if there's a better way. I'm thinking of adding my user to the wheel group; but would there be other side effects?
The permission on the git executable are fine; there is no reason you should need to change that, or change your group.
It's more likely that the permissions on your Git checkout are wrong. If you accidentally checked it out with something like sudo git clone ssh://user#host/project.git, or at some point you performed some kind of operation in the repository that caused some files to be owned by root, git running under your account won't be able to write to those files.
Most likely, what you need to do is chown -R <user> myproject in order to make your project owned by your own user account, rather than root.
If you were prompted like this:
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
And then you ran:
sudo git init .
After updating xcode license. Just remove the init and re-init w/o sudo
sudo rm -rf .git
git init .
This fixed my issue.

Changing Ownership of a directory in OS X

I've installed homebrew, and am trying to change the write permissisons for the /usr/local/include directory.
When I run 'brew doctor', I get this error message:
Error: The /usr/local directory is not writable. Even if this
directory was writable when you installed Homebrew, other software may
change permissions on this directory. Some versions of the "InstantOn"
component of Airfoil are known to do this.
You should probably change the ownership and permissions of /usr/local
back to your user account.
I tried doing that with chown, but I'm pretty new at this and don't think I was running it correctly. I ran:
chown myusername /usr/local/include
I didn't get any error message, but when I run brew doctor it says I still lack permission to write to /usr/local/include.
Any help would be greatly appreciated!
Edit:
I'm getting an "operation not permitted" error.
cd /usr
chown myusername local
chown: local: Operation not permitted
Simple solution that worked for me:
click on your background to go to finder
click on go and go to folder /usr
right click on local and do get info
unlock the lock at the bottom
click + sign and add your user to the list and give read/write privileges
click on the gear sign at the bottom and choose apply to enclosed items to recurse under that directory and assign privileges too all directories beneath it.
type brew doctor from command prompt to test. My result:
Your system is ready to brew.
On my system, /usr/local is owned by root:admin and is rwxrwxr-x. My user is a member of the admin group, thus has write permissions. I haven't messed with ownership and permissions there, and my Homebrew installation is not complaining, so I assume my setup fits its requirements.
Check the ownership of your /usr/local; if it is owned by group admin, check if your non-adminsitrator account is a member of that group.
You should use: sudo chown username folder
Be careful with the change of ownership
Do not try to change ownership for /usr/ or recursively for /usr/local/
if you are not getting the same error posted in the original question.
You could screw up your system or weakness its security.
Some users can receive with homebrew permission errors that could be solved without changing the ownership of the whole /usr/ or /usr/local/ directories.
According to my experience, in most cases, it is just enough to restore the ownership for /usr/local/Cellar:
sudo chown -R "$USER" /usr/local/Cellar/
Alternatively, if you have a not standard installation
sudo chown -R "$USER" "$(brew --prefix)/Cellar"
You can try use command ls -l. With this command you will see list of files in current directory with some useful information, like who is owner of this file. So you will see if chown worked out. I believe that chown is a right command for changing ownership.
Exactly this problem has just started occuring on my MacBook.
The owner of /usr/local is:
drwxr-xr-x 24 root wheel 768 Apr 9 17:25 local
I am unable to change the permissions and add myself via Finder - I get a "you don't have the necessary permission" error.
Is this maybe due to security changes in mac os? (I have 10.13.6)

Resources