BuildWithMatija
Get In Touch
Build with Matija logo

Build with Matija

Modern websites, content systems, and AI workflows built for long-term growth.

Services

  • Headless CMS Websites
  • Next.js & Headless CMS Advisory
  • AI Systems & Automation
  • Website & Content Audit

Resources

  • Case Studies
  • How I Work
  • Blog
  • CMS Hub
  • E-commerce Hub
  • Dashboard

Headless CMS

  • Payload CMS Developer
  • CMS Migration
  • Payload vs Sanity
  • Payload vs WordPress
  • Payload vs Contentful

Get in Touch

Ready to modernize your stack? Let's talk about what you're building.

Book a discovery callContact me →
© 2026Build with Matija•All rights reserved•Privacy Policy•Terms of Service
  1. Commands
  2. Fix Git Repository Ownership and Permissions

Fix Git Repository Ownership and Permissions

Recursively changes ownership of the .git directory to the current user and sets appropriate permissions for Git operations. This command resolves common permission issues that occur when Git repositories are created or modified with root privileges.

sudo chown -R $(whoami):$(whoami) .git && sudo chmod -R ug+rwx,o+rx .git
Categories
Docker
Operating Systems
cross-platform
Prerequisites
Unix-like operating system (Linux, macOS, WSL)sudo privilegesBasic understanding of file permissions and ownership
Last Tested

5/29/2025

Details

This compound command performs two critical operations:

  1. Ownership Change (chown -R $(whoami):$(whoami) .git)

    • Changes both user and group ownership of the entire .git directory tree
    • $(whoami) dynamically gets the current username
    • -R flag applies changes recursively to all subdirectories and files
  2. Permission Setting (chmod -R ug+rwx,o+rx .git)

    • ug+rwx: Grants read, write, and execute permissions to user and group
    • o+rx: Grants read and execute permissions to others (no write access)
    • -R flag applies permissions recursively

Common Use Cases:

  • After cloning a repository with sudo
  • When Git operations fail with "Permission denied" errors
  • After accidentally running Git commands as root
  • When switching between different user accounts on the same repository
  • Resolving issues after copying repositories between systems

Example Scenarios:

# When you see errors like:
# fatal: could not lock config file .git/config: Permission denied
# error: insufficient permission for adding an object to repository database

# Run the fix command:
sudo chown -R $(whoami):$(whoami) .git && sudo chmod -R ug+rwx,o+rx .git

# Then verify with:
ls -la .git/
Safety Notes
  • Always run this command from the repository root directory
  • Verify you're in the correct directory before execution
  • The command uses sudo, so ensure you understand the implications
  • Consider backing up important repositories before making permission changes
Related Articles
Blog posts that might provide additional context

Ultimate Next.js Standalone Dockerfile Guide (Tiny Images)

3/2/2026•Docker

Run GLM-OCR on RunPod Serverless: 17-line Dockerfile

2/9/2026•Docker

Install Docker CLI on Mac with Colima — Fast Guide 2026

1/5/2026•Docker