git/init

Initialize Repository

Create an empty Git repository in a directory to start version controlling your project

git
init
repository
version control
setup
beginner

Command

git init

Explanation

The git init command is the first command you use when starting a new Git repository. It initializes a new Git repository and creates a hidden .git directory that contains all the necessary metadata for version control. This command is essential for converting any directory into a Git repository, allowing you to track changes, create commits, manage branches, and collaborate with others. When you run git init, Git sets up the repository structure including object storage, refs for branches and tags, and configuration files. You can initialize a repository in an existing directory or create a new directory during initialization. This command is safe to run multiple times on an existing repository. It's the foundation of all Git workflows and is used by millions of developers worldwide for source code management, documentation, configuration files, and any other project that benefits from version control.

Common Use Cases

  • Starting version control for a new project
  • Converting an existing project to use Git
  • Creating a new repository from scratch
  • Setting up a repository structure before adding files

Best Practices

  • Always run git init in the root directory of your project
  • Configure user.name and user.email with git config before making commits
  • Consider using git init --bare for shared repositories on servers
  • Review .gitignore before adding files to avoid tracking unwanted files

Common Mistakes to Avoid

  • Running git init in the wrong directory
  • Initializing a repository inside an existing repository (creating nested repos)
  • Forgetting to configure user name and email before first commit
  • Initializing a bare repository when a regular one is needed

Troubleshooting

Problem: Git init seems to do nothing

Solution: This is normal - git init creates a hidden .git directory. Use ls -la to see it or git status to verify.

Problem: Getting "not a git repository" error after init

Solution: Make sure you're in the directory where you ran git init, or use git init again in your current directory.

Examples

Initialize new repo in current directory

git init

Create new directory and initialize repo

git init project-name

Create a bare repository for sharing (no working directory)

git init --bare