Git Basics: Init, Clone, Commit
Key Concepts
- Git Init
- Git Clone
- Git Commit
Git Init
The git init
command initializes a new Git repository in the current directory. This creates a hidden .git
directory that contains all the necessary files and metadata for version control. Once initialized, you can start tracking changes to your project.
Example:
$ mkdir my_project $ cd my_project $ git init
Git Clone
The git clone
command creates a copy of an existing Git repository. This is useful when you want to work on a project that is hosted remotely, such as on GitHub. The clone includes all the files, branches, and commit history from the original repository.
Example:
$ git clone https://github.com/username/repository.git
Git Commit
The git commit
command records changes to the repository. Each commit is a snapshot of the project at a specific point in time, along with a message describing the changes. Commits are essential for tracking the history of a project and reverting to previous states if necessary.
Example:
$ git add . $ git commit -m "Initial commit"
Examples and Analogies
Think of git init
as setting up a new photo album. You create a space where you can start adding photos (commits) to document your journey.
git clone
is like making a copy of someone else's photo album. You get all the photos and the history of how they were taken, so you can continue adding your own photos.
git commit
is like adding a new photo to the album with a caption. Each photo represents a snapshot of your project at a specific moment, and the caption explains what happened in that moment.