# Fixing Git Commit Permission Errors: A Simple Guide with a Sample Use Case

When working with Git, encountering errors can be frustrating—especially when they seem to halt your workflow. One common issue is:

```ruby
error: insufficient permission for adding an object to repository database .git/objects
error: Error building trees
```

This post explains why this error occurs and how you can fix it, using a simple, real-world example.

---

## Understanding the Error

This error indicates that Git doesn’t have the proper permissions to write new data into your repository’s internal database (stored in the `.git/objects` directory). This can happen when:

* **Ownership issues:** Files within `.git/objects` might be owned by another user.
    
* **Accidental use of** `sudo`: Running Git commands with `sudo` can inadvertently change file ownership.
    
* **File permission settings:** Your user might not have the required write permissions.
    

---

## A Sample Use Case: A Personal Project on Linux

Imagine you’re working on your personal project called **“my\_cool\_project”** on your Linux machine. One day, while trying to quickly fix an issue, you run a Git command with `sudo`—for example:

```ruby
sudo git commit -m "Quick fix commit"
```

After this, you notice that some files inside the `.git/objects` directory now belong to `root`. Later, when you try to commit further changes without `sudo`, Git throws this permission error because your regular user account no longer has write access to those files.

---

## How to Fix the Permission Error

Follow these simple steps to resolve the issue:

### 1\. **Check the Permissions**

First, inspect the ownership and permissions of the `.git/objects` directory:

```ruby
ls -la .git/objects
```

This command lists all files along with their ownership details. If you see that files are owned by `root` instead of your username, that’s the root of the problem.

### 2\. **Change Ownership of the** `.git` Directory

To fix this, change the ownership of the entire `.git` directory back to your user account. Replace `$(whoami)` with your username if necessary:

```ruby
sudo chown -R $(whoami) .git
```

This command recursively changes the owner of all files in `.git` so that your user account can manage them.

### 3\. **Ensure Proper Write Permissions**

After adjusting ownership, ensure that your user has the correct read and write permissions:

```ruby
chmod -R u+rw .git
```

This command makes sure you can read from and write to every file and directory inside `.git`.

### 4\. **Retry Your Commit**

With the permissions corrected, try your commit again:

```ruby
git commit -m "full fledged feature"
```

This time, Git should be able to add objects to the repository database without any issues.

---

## Best Practices to Avoid This Error

* **Avoid using** `sudo` with Git: Running Git commands as `sudo` can alter file ownership unexpectedly. Always operate as your regular user.
    
* **Regularly check permissions:** Especially if you work with multiple user accounts or switch between environments.
    
* **Consistent environment:** Use the same shell or terminal session settings to avoid unexpected permission issues.
    

---

## Conclusion

File permission issues in Git, especially those related to the `.git/objects` directory, are usually a sign of an ownership mismatch—often caused by using `sudo` inadvertently. By understanding the error and following the simple steps above, you can quickly restore proper permissions and continue with your project.

This approach not only fixes your immediate problem but also reinforces best practices to prevent such issues in the future. Happy coding!
