git init turns any directory into a Git repository.
What Does git init Do?
git init is one way to start a new project with Git. To start a repository, use either git init or git clone – not both.
To initialize a repository, Git creates a hidden directory called
.git. That directory stores all of the objects and refs that Git uses and creates as a part of your project’s history. This hidden .git directory is what separates a regular directory from a Git repository.How to Use git init
Common usages and options for git init
git init: Transform the current directory into a Git repositorygit init <directory>: Transform a directory in the current path into a Git repositorygit init --bare: Create a new bare repository (a repository to be used as a remote repository only, that won’t contain active development)
Examples of git init
git init vs git clone
Starting a new project can be confusing. Sometimes, it’s unclear if you should use git init, git clone, or both.
git init: One Person Starting a New Repository Locally
Your project may already exist locally, but it doesn’t have Git yet. git init is probably the right choice for you. This is only run once, even if other collaborators share the project.
Add the remote URL
Add the remote URL to your local git repository:This stores the remote URL under a more human-friendly name,
origin.git clone: The Remote Already Exists
If the repository already exists on a remote, you would choose to git clone and not git init.
If you create a remote repository first with the intent of moving your project to it later:
- If there are no commits in the remote repository, follow the steps above for
git init - If there are commits and files in the remote repository but you want it to contain your project files,
git clonethat repository, then move the project’s files into that cloned repository. Usegit add,git commit, andgit pushto create a history that makes sense for the beginning of your project.
git init Existing Folder
The default behavior of git init is to transform the current directory into a Git repository.
git init.
Or, you can create a new repository in a directory in your current path using git init <directory>.
git init Gone Wrong
git init in the wrong directory
Running git init in the wrong place will create unintended repositories. You may have noticed strange error messages when using Git. Maybe you suspect that another parent directory is also a Git repository.
Check if current directory is tracked
Use
git status to see if the current directory is tracked by Git:Look for the .git directory
If it is tracked, run
ls -al and look for an otherwise hidden .git directory:Navigate up if needed
If you don’t see it, navigate one level up:Repeat this until you find the
.git directory.Remove the .git directory
Once you find the
.git directory and are sure you don’t want that to be a Git repository:Related Commands
git clone [url]: Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commitsgit status: Always a good idea, this command shows you what branch you’re on, what files are in the working or staging directory, and any other important informationgit remote -v: Show the associated remote repositories and their stored name, likeorigingit remote add origin <url>: Add a remote so you can collaborate with others on a newly initialized repositorygit push: Uploads all local branch commits to the remotegit push -u origin main: When pushing a branch for the first time, this type of push will configure the relationship between the remote and your local repository
Next Steps
Git Add
Stage files for your first commit
Git Commit
Create your first commit
Git Remote
Learn about working with remotes
Git Push
Push your changes to a remote repository