Creating and Managing Repositories
Learn how to create new Git repositories and clone existing ones
With Git installed and configured, you're ready to start working with repositories. This section covers creating repositories, understanding the basic Git workflow, and making your first commits.
Creating a Git Repository
You can create a Git repository in two ways: initializing a new one or cloning an existing one.
Initializing a New Repository
To create a new Git repository in your project directory:
- Navigate to your project folder:
cd /path/to/your/project
- Initialize the repository:
git init
This creates a .git
subdirectory that contains all the necessary repository files – the skeleton of your Git repository.
After running this command, you should see output similar to:
Initialized empty Git repository in /path/to/your/project/.git/
Your existing files aren't automatically tracked. To start tracking them, you'll need to add and commit them (covered in the next section).
Cloning an Existing Repository
If you want to get a copy of an existing Git repository, use the git clone
command:
git clone https://github.com/username/repository.git
This creates a directory with the repository name, initializes a .git
directory inside it, downloads all the repository data, and checks out a working copy of the latest version.
To clone a repository into a directory with a different name:
git clone https://github.com/username/repository.git my-project
Git supports several transfer protocols:
- HTTPS:
https://github.com/username/repository.git
- SSH:
[email protected]:username/repository.git
- Git protocol:
git://github.com/username/repository.git
SSH is commonly used for development as it allows for secure authentication without entering credentials each time.
Repository Structure
After initializing a repository, Git creates a hidden .git
directory that contains everything Git needs to track your project:
config
: Repository-specific configurationHEAD
: Reference to the current branchindex
: Staging area informationobjects/
: Database of all your files and commitsrefs/
: Pointers to commit objects (branches, tags, etc.)
You generally won't interact with these files directly, but understanding what's happening behind the scenes can help you use Git more effectively.
.gitignore File
Many projects contain files that shouldn't be tracked by Git, such as:
- Build artifacts
- Dependency directories
- Environment files with secrets
- User-specific configuration files
- Log files
You can tell Git to ignore these files by creating a .gitignore
file:
touch .gitignore
Edit this file to specify patterns for files you want to ignore:
# Compiled files
*.class
*.o
*.pyc
# Logs
*.log
log/
# Build directories
build/
dist/
# Environment-specific files
.env
.env.local
node_modules/
Git will not track any files matching these patterns. The .gitignore
file itself should usually be tracked by Git.
Understanding Repository States
Every file in your working directory can be in one of two states: tracked or untracked.
Tracked files are files that Git knows about. They can be:
- Unmodified: match the last commit
- Modified: changed since the last commit
- Staged: marked to go into your next commit
Untracked files are everything else – any files in your working directory that weren't in your last snapshot and aren't in your staging area.
As you edit files, Git sees them as modified. You then stage these changes and commit the staged changes, and the cycle repeats.
Basic Git Workflow Recap
Here's a quick summary of the basic Git workflow:
- Modify files in your working directory
- Stage the changes you want to include in your next commit
- Commit the staged changes, creating a snapshot in your repository history
Now that you understand how to create repositories, let's move on to tracking changes and making commits in the next section.
Found an issue?