Unboxing GIT Fundamentals: Part 2 of 10

Anand
4 min readJun 7, 2024

--

Part 2: Basic Git Commands and Operations

Welcome back to our Git learning series! In the previous post, we introduced Git and discussed the importance of version control. Now that you have Git installed and configured, it’s time to dive into some basic Git commands and operations. By the end of this post, you’ll be comfortable with setting up repositories, tracking changes, and understanding the core concepts of Git.

Git Configuration and Setup

Before starting with Git commands, let’s ensure your Git configuration is set up properly. If you haven’t done so already, set your username and email:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

You can check your current Git configuration with:

git config --list

This command will display your configuration settings, confirming that your name and email are correctly set.

Creating a Repository

A Git repository is where your project files and their revision history are stored. Let’s create a new repository:

  1. Create a new directory for your project:
mkdir my-project cd my-project

2. Initialize a new Git repository:

git init

This command creates a hidden .git directory that contains all the metadata for your repository.

Adding and Committing Changes

Let’s add some files to our repository and commit them.

  1. Create a new file:
echo "Hello, Git!" > readme.md

2. Check the status of your repository:

git status

This command will show you the current state of your working directory and staging area. You should see that readme.md is an untracked file.

3. Add the file to the staging area:

git add readme.md

The git add command stages the file, preparing it to be committed.

4. Commit the staged changes:

git commit -m "Add readme file"

The -m flag allows you to add a commit message describing the changes. This message should be concise but descriptive.

Understanding the Working Directory, Staging Area, and Repository

To work effectively with Git, it’s important to understand the three main areas:

  • Working Directory: This is where you make changes to your files. It contains the current state of your project files.
  • Staging Area (Index): This is where you place changes that you want to include in your next commit. Think of it as a buffer zone between your working directory and the repository.
  • Repository: This is where Git permanently stores your committed changes along with all the version history.

Here’s a simple workflow to illustrate these areas:

  1. Make changes in the working directory: Modify or create files as needed.
  2. Stage changes: Use git add to move changes from the working directory to the staging area.
  3. Commit changes: Use git commit to move changes from the staging area to the repository.

Tracking and Viewing Changes

Git provides several commands to help you track and view changes in your repository.

  1. View the commit history:
git log

This command shows the commit history, including commit hashes, authors, dates, and commit messages.

2. Show differences between commits:

git diff

The git diff command displays changes between the working directory and the staging area or between different commits.

3. View the status of your repository:

git status

As mentioned earlier, git status shows the current state of your working directory and staging area.

4. Display detailed information about a specific commit:

git show <commit-hash>

Replace <commit-hash> with the hash of the commit you want to inspect. This command shows detailed information about the specified commit.

Removing and Renaming Files

Git also allows you to remove and rename files within your repository.

  1. Remove a file:
git rm <file-name>

This command removes the file from the working directory and stages the removal.

2. Rename a file:

git mv <old-file-name> <new-file-name>

This command renames the file and stages the change.

Best Practices for Using Git

To make the most of Git, consider these best practices:

  • Commit frequently: Make small, frequent commits with descriptive messages to keep your commit history clean and informative.
  • Write meaningful commit messages: Clearly describe what changes you made and why.
  • Use branches: Isolate new features or bug fixes in separate branches to keep your main branch stable.
  • Review changes before committing: Use git diff and git status to review your changes before committing them.

Conclusion

In this part of our Git learning series, we’ve covered the basics of Git commands and operations. You’ve learned how to create a repository, track changes, and understand the working directory, staging area, and repository. These fundamental skills are essential for using Git effectively.

In the next part, we’ll explore branching and merging in Git, allowing you to manage different lines of development and integrate changes seamlessly.

Next Part : Unboxing GIT Fundamentals: Part 3 of 10

Happy Learning!

Explore All Content of Unboxing GIT Fundamentals

  1. Part 1 of 10: Introduction to Git and Version Control
  2. Part 2 of 10: Basic Git Commands and Operations
  3. Part 3 of 10: Branching and Merging in Git
  4. Part 4 of 10: Remote Repositories in Git
  5. Part 5 of 10: Collaboration Workflows in Git
  6. Part 6 of 10: Advanced Git Commands and Techniques
  7. Part 7 of 10: Git Best Practices for Repository Management
  8. Part 8 of 10: Exploring Git Workflows — Git Flow, GitHub Flow, and GitLab Flow
  9. Part 9 of 10: Integrating Git with CI/CD Pipelines on GitHub and GitLab
  10. Part 10 of 10: Debugging and Troubleshooting Common Git Issues

About Author: An experienced IT professional worked in major IT companies for more than 20+ years as Solution Architect with core expertise on DevOps and Cloud. To get trained in DevOps from experts like Anand visit https://www.svsitsolutions.in or contact in WhatsApp

Other Interesting Blogs from the Author:

# 10 Surprising Facts About Git That You Probably Didn’t Know

# Hidden Secrets of Git : That You Never Knew

# Learning Teaser — Git in a Few Hours: A Comprehensive Training Guide

# Cinematic Experience with GIT : Lights, Camera, Action for Your Code!

--

--