Skip to content

Introduction to Git: Basic Commands and Examples

Published: at 08:40 AM

Introduction

Git is a powerful version control system that helps developers track changes, collaborate on projects, and manage their code efficiently. This guide will introduce basic Git commands to help you get started.

TL;DR

You can find a shorter cheat sheet version of this article here.

Table of contents

Open Table of contents

Configuring Git

Before you begin using Git, set your username and email address to track commits.

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

Initializing a Repository

Navigate to your project directory and initialize a Git repository:

cd /path/to/your/project
git init

This command creates a .git folder in your project directory to track changes.


Tip: mailmap file

If you use different email addresses across commits in the repository you can use .mailmap file to manage and consolidate commit author information. It allows you to map multiple email addresses or names to a single, canonical identity. By creating a .mailmap file in the repository, Git tools like git shortlog and git blame can display consistent author information.

Example format in .mailmap:

Proper Name <canonical.email@example.com> <alias.email@example.com>

This helps maintain cleaner and more accurate contributor lists.


Basic Git Workflow

Cloning a Repository

To work on an existing repository, clone it to your local machine:

git clone https://github.com/user/repository.git

Checking the Status

Check the current status of your working directory:

git status

Adding Files

Stage your changes before committing:

git add filename.txt      # Adds a specific file
git add .                 # Adds all changes

Committing Changes

Once files are staged, commit them with a descriptive message:

git commit -m "Describe what changes you made"

Pushing Changes

Send your local commits to a remote repository:

git push origin main      # Push to the main branch

Pulling Updates

Update your local repository with changes from the remote repository:

git pull origin main

Viewing Differences (git diff)

The git diff command shows the differences between your working directory, staging area, and the repository. It’s useful for seeing what has changed before committing.

  1. Viewing changes in the working directory: To see changes that have been made but not yet staged:

    git diff
  2. Viewing staged changes: To see changes that have been staged but not yet committed:

    git diff --staged
  3. Comparing two branches: To compare changes between the current branch and another branch:

    git diff main new-feature

Example:
Let’s say you modified a file example.txt in your project but haven’t committed the changes yet. Running git diff might output something like this:

diff --git a/example.txt b/example.txt
index e69de29..3b18e52 100644
--- a/example.txt
+++ b/example.txt
@@ -1 +1 @@
-Hello World
+Hello Git World

This output shows that the line "Hello World" was replaced with "Hello Git World".


Branching in Git

Creating a New Branch

Create a new branch to work on a feature:

git branch new-feature

Switching Branches

Switch to a different branch:

git checkout new-feature

Alternatively, create and switch to the new branch in one command:

git checkout -b new-feature

Merging Branches

Merge the new branch into the main branch:

  1. Switch to the main branch:

    git checkout main
  2. Merge the changes from the new-feature branch:

    git merge new-feature

Conclusion

This guide covered essential Git commands, from initializing a repository to branching and viewing differences with git diff. These commands will help you track your changes and manage your project efficiently. Explore Git’s advanced features like resolving conflicts and interactive rebasing as you become more comfortable with the tool.

For more details, visit the official Git documentation.