Core Commands
Welcome to the heart of Git! 🌟 In this section, we’ll dive into the four foundational commands that every professional must master: git init, git clone, git add, and git commit. These commands form the bedrock of version control, enabling you to track changes, collaborate seamlessly, and maintain a robust history of your code. Let’s get hands-on.
git init
The git init command initializes a new Git repository in your current directory. This step creates a hidden .git directory that stores all the version control metadata. Without this step, you cannot track changes or collaborate with others.
Why use git init?
It’s the first step in setting up a local repository. Once initialized, you can start tracking your code.
<code class="language-bash"># Initialize a new Git repository in the current directory <p>git init</code>
Example:
Suppose you have a new project called my-project. Run:
<code class="language-bash">cd my-project <p>git init</code>
This creates a .git folder in my-project, ready for your code. You’ll see this hidden directory when you list your current directory (ls -a).
git clone
The git clone command creates a local copy of an existing Git repository from a remote server (like GitHub, GitLab, or Bitbucket). This is how you get a project started from a remote source.
Why use git clone?
It’s essential for collaboration. When you work on a project that’s already hosted online, you use git clone to get a local copy that you can modify and then push back.
Example:
If you want to get the hello-world project from GitHub, run:
<code class="language-bash">git clone https://github.com/username/hello-world.git</code>
This command creates a new directory named hello-world in your current working directory and initializes a Git repository inside it. You can verify the clone with:
<code class="language-bash">ls -l hello-world</code>
git add
The git add command stages changes for the next commit. It tells Git which files you want to include in the next commit. This is a critical step because it allows you to select specific changes before committing.
Why use git add?
It helps you control what goes into a commit. Without staging, you’d be committing all changes at once, which can lead to messy commits.
Example:
After creating a file file.txt, you might run:
<code class="language-bash">echo "Hello, Git!" > file.txt <p>git add file.txt</code>
This stages the change to file.txt for the next commit. You can check staged changes with:
<code class="language-bash">git status</code>
Advanced Tips:
- Stage multiple files at once:
<code class="language-bash"> git add file1.txt file2.txt</code>
- Stage files using glob patterns:
<code class="language-bash"> git add *.txt</code>
git commit
The git commit command saves the staged changes to the local repository’s history. It creates a new version of your project that you can track.
Why use git commit?
It’s the mechanism that records your work. Every commit is a snapshot of your project at a specific point in time.
Example:
After staging changes, commit them with a message:
<code class="language-bash">git commit -m "Initial commit"</code>
This command creates the first commit in your repository’s history. You can verify the commit with:
<code class="language-bash">git log --oneline</code>
Advanced Tips:
- Commit without staging all changes (use
-a):
<code class="language-bash"> git commit -a -m "Add initial files"</code>
- View commit history:
<code class="language-bash"> git log --graph --oneline --decorate</code>
Commit Message Best Practices:
Always write a clear and concise message. For example:
Add initial files(too vague)Initial commit: Add README.md and basic structure(more specific)
Summary
You’ve now mastered the four foundational Git commands:
git init→ Initialize a new repository.git clone→ Copy a remote repository locally.git add→ Stage changes for the next commit.git commit→ Save staged changes as a new commit.
These commands form the core workflow for any Git project. By understanding and using them correctly, you’ll be well on your way to professional version control practices. ✅