Git 在 vagrant 机器中没有 sudo 命令无法工作

Git commands not working without sudo in vagrant machine

我在我的 vagrant 机器上有一个项目,我想将它推送到我的 github 存储库中。但是我不能不使用 sudo.

添加对象
  1. 我已经初始化了git
    vagrant@vagrant:/vagrant/testDir$ git init
    Initialized empty Git repository in /vagrant/testDir/.git/`
    
  2. 之后如果我运行命令添加内容
    vagrant@vagrant:/vagrant/testDir$ git add .`
    
    然后我收到这个错误:

error: insufficient permission for adding an object to repository database .git/objects

error: file1: failed to insert into database

error: unable to index file file1

fatal: adding files failed

虽然我可以 运行 命令没有任何错误如果我 运行 与 sudo,即 sudo git add ..

我确认了项目里面的权限,没问题:

vagrant@vagrant:/vagrant/testDir$ ls -al
total 0
drwxr-xr-x 1 vagrant vagrant 160 Dec 23 10:33 .
drwxr-xr-x 1 vagrant vagrant 160 Dec 23 10:33 ..
drwxr-xr-x 1 vagrant vagrant 352 Dec 23 10:33 .git
-rw-r--r-- 1 vagrant vagrant   0 Dec 23 10:33 file1
-rw-r--r-- 1 vagrant vagrant   0 Dec 23 10:33 file2.txt

我在 macOS High Sierra 上的流浪机器 运行ning 上安装了 ubuntu 18.04。 流浪汉 = 2.2.2 虚拟盒子 = 6.0.8 r130520

你能帮帮我吗?

更新 1

另外,.git/目录的权限没问题

vagrant@vagrant:/vagrant/testDir/.git$ ls -al
total 12
drwxr-xr-x 1 vagrant vagrant 320 Dec 23 10:52 .
drwxr-xr-x 1 vagrant vagrant 160 Dec 23 04:12 ..
-rw-r--r-- 1 vagrant vagrant  23 Dec 23 04:12 HEAD
drwxr-xr-x 1 vagrant vagrant  64 Dec 23 04:12 branches
-rw-r--r-- 1 vagrant vagrant 111 Dec 23 04:12 config
-rw-r--r-- 1 vagrant vagrant  73 Dec 23 04:12 description
drwxr-xr-x 1 vagrant vagrant 416 Dec 23 04:12 hooks
drwxr-xr-x 1 vagrant vagrant  96 Dec 23 04:12 info
drwxr-xr-x 1 vagrant vagrant 160 Dec 23 04:12 objects
drwxr-xr-x 1 vagrant vagrant 128 Dec 23 04:12 refs

更新 2

.git/objects目录里面的权限也可以

vagrant@vagrant:/vagrant/testDir/.git/objects$ ls -al
total 0
drwxr-xr-x 1 vagrant vagrant 160 Dec 23 11:06 .
drwxr-xr-x 1 vagrant vagrant 320 Dec 23 11:06 ..
drwxr-xr-x 1 vagrant vagrant  96 Dec 23 11:06 e6
drwxr-xr-x 1 vagrant vagrant  64 Dec 23 11:06 info
drwxr-xr-x 1 vagrant vagrant  64 Dec 23 11:06 pack

if the check the permission of the contents, the permissions are okay:

他们是,在第一层。
但他们在 .git/objects 吗?

你可能想做,当然,sudo chown -R vagrant:vagrant .git

同时添加 core.sharedRepository 设置:

git config --global core.sharedRepository group

OP Kakar mentions the hashicorp/vagrant issue 10913,其中指出:

I believe the bug is in the Guest Additions so you need to make sure the correct version of those are installed.
Run modinfo vboxguest in Ubuntu to find out the version that is installed.

I appear to have solved this by updating my Virtualbox Guest Additions from 6.0.10 to 6.0.12, using dotless-de/vagrant-vbguest.

这些是我为解决问题所采取的步骤:

  1. 我将 macOS 上的 virtualbox 从 6.0.8 r130520 降级到 6.0.4 r128413。
  2. 已安装vagrant-vbguest插件

    $ vagrant plugin install vagrant-vbguest

  3. 重新加载 vagrant 机器。

    user@MacBook-Pro vagrant-project-machine $ vagrant reload ==> default: Attempting graceful shutdown of VM... ==> default: Checking if box 'bento/ubuntu-18.04' is up to date... ... ... ==> default: Machine booted and ready! ... [default] GuestAdditions versions on your host (6.0.4) and guest (6.0.8) do not match. ... ... The following additional packages will be installed: linux-headers-4.15.0-51 ... ...

最后你可以通过运行这个命令来确认它:

user@MacBook-Pro vagrant-project-machine $ vagrant vbguest --status
...
...
[default] GuestAdditions 6.0.4 running --- OK.

你也可以通过运行这个命令在流浪机器内部确认:

vagrant@vagrant:/vagrant/testDir$ modinfo vboxguest
filename:       /lib/modules/4.15.0-51-generic/misc/vboxguest.ko
version:        6.0.4 r128413
license:        GPL
description:    Oracle VM VirtualBox Guest Additions for Linux Module
author:         Oracle Corporation
srcversion:     95F53B9B95BC6E03156A1D8
alias:          pci:v000080EEd0000CAFEsv00000000sd00000000bc*sc*i*
depends:        
retpoline:      Y
name:           vboxguest
vermagic:       4.15.0-51-generic SMP mod_unload 

现在我可以 运行 git 命令而无需使用 sudo