Delen via


Set up a Git repository

A Git repository, or repo, is a folder that Git tracks changes in. There can be any number of repos on a computer, each stored in their own folder. Each Git repo on a system is independent, so changes saved in one Git repo don't affect the contents of another.

A Git repo contains every version of every file saved in the repo. This is different than other version control systems that store only the differences between files. Git stores the file versions in a hidden .git folder alongside other information it needs to manage code. Git saves these files very efficiently, so having a large number of versions doesn't mean that it uses a lot of disk space. Storing each version of a file helps Git merge code better and makes working with multiple versions of code quick and easy.

Developers work with Git through commands issued while working in a local repo on the computer. Even when sharing code or getting updates from the team, it's done from commands that update the local repo. This local-focused design is what makes Git a distributed version control system. Every repo is self-contained, and the owner of the repo is responsible for keeping it up to date with the changes from others.

Git repositories

Most teams use a central repo hosted on a server that everyone can access to coordinate their changes. The central repo is usually hosted in a source control management solution, like GitHub or Azure DevOps. A source control management solution adds features and makes working together easier.

Create a new Git repo

You have two options to create a Git repo. You can create one from the code in a folder on a computer, or clone one from an existing repo. If working with code that's just on the local computer, create a local repo using the code in that folder. But most of the time the code is already shared in a Git repo, so cloning the existing repo to the local computer is the recommended way to go.

Create a new repo from existing code

Use the git init command to create a new repo from an existing folder on the computer. From the command line, navigate to the root folder containing the code and run:

> git init

to create the repo. Next, add any files in the folder to the first commit using the following commands:

> git add --all

> git commit -m "Initial commit"

Create a new repo from a remote repository

Use the git clone command to copy the contents of an existing repo to a folder on the computer. From the command line, navigate to the folder to contain the cloned repo, then run:

> git clone https://<fabrikam.visualstudio.com/DefaultCollection/Fabrikam/_git/FabrikamProject>

Be sure to use the actual URL to the existing repo instead of the placeholder URL shown in this example. This URL, called the clone URL, points to a server where the team coordinates changes. Get this URL from the team, or from the clone button on the site where the repo is hosted.

It's not necessary to add files or create an initial commit when the repo is cloned since it was all copied, along with history, from the existing repo during the clone operation.

Next steps

GitHub and Azure Repos provide unlimited free public and private Git repos.

Visual Studio user? Learn more about how to create and clone repos from Visual Studio in this Git tutorial.