Basic Git Commands Cheat Sheet: Essential Git Commands for Beginners

  • Post category:Dev-Ops / Git
  • Post last modified:December 27, 2024

Git is a powerful and essential tool for version control in software development. Whether you’re a beginner or an experienced developer, understanding the most frequently used Git commands can help you streamline your workflow. Git allows you to track changes, collaborate with teammates, and manage project histories effectively.

In this comprehensive guide, we’ll walk through the basic Git commands cheat sheet, explaining each command with examples to help you become proficient in Git. Let’s dive in and explore the core Git commands you’ll need for daily development tasks.

What is Git?

Git is a distributed version control system (VCS) that helps developers track changes in their code over time. Git allows developers to collaborate efficiently by managing multiple versions of codebases, maintaining history, and providing easy ways to revert back to earlier versions of a project.

Git’s distributed nature means that every contributor has a complete copy of the repository, making it highly resilient and enabling offline work. It is widely used in modern software development workflows and integrates well with platforms like GitHub, GitLab, and Bitbucket.

Setting Up Git

Before we dive into Git commands, make sure you have Git installed. You can check if Git is installed by running:

git --version

If you need to install Git, visit Git’s official website and follow the instructions for your operating system.

Once installed, configure Git with your name and email address:

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

Basic Git Commands Cheat Sheet

1. git init

The git init command initializes a new Git repository. You should run this command inside the directory you want to version control.

Example:

git init

This command creates a .git directory where Git will track changes.

2. git clone

Use the git clone command to create a copy of an existing remote Git repository. This command is usually used to pull an existing project from platforms like GitHub or GitLab.

Example:

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

This creates a local copy of the remote repository on your machine.

3. git status

The git status command shows the current state of the working directory and the staging area. It helps you check which changes are staged, unstaged, or untracked.

Example:

git status

This command is helpful for reviewing the changes before committing them.

4. git add

The git add command stages changes, meaning it adds modified or new files to the staging area, preparing them for a commit.

Example:

git add filename
git add .  # Adds all files in the directory

5. git commit

The git commit command saves changes in the Git history. You must add a commit message that describes the changes made.

Example:

git commit -m "Commit message"

The commit message should be clear and concise, describing what changes were made in this commit.

6. git log

Use the git log command to view the history of commits in the current branch. It displays a detailed list of commits, including their unique identifiers (commit hash), author, date, and commit message.

Example:

git log

For a more concise output, you can use git log --oneline to view the log in a compact form.

7. git diff

The git diff command shows the differences between the working directory and the staged changes, or between the staging area and the latest commit.

Example:

git diff          # Show changes in working directory
git diff --staged # Show changes staged for commit

8. git branch

The git branch command is used to list, create, or delete branches.

Example:

git branch        # List all branches
git branch branch-name  # Create a new branch
git branch -d branch-name  # Delete a branch

Branching allows you to work on different features or bug fixes in isolation without affecting the main codebase.

9. git checkout

Use the git checkout command to switch between branches or restore files in the working directory.

Example:

git checkout branch-name   # Switch to a different branch
git checkout filename      # Restore a specific file from the latest commit

10. git merge

The git merge command merges the changes from one branch into the current branch. It’s used to integrate changes made in different branches.

Example:

git checkout main        # Switch to the main branch
git merge feature-branch # Merge the feature branch into the main branch

Merge conflicts may occur when changes in both branches conflict, and Git will prompt you to resolve them manually.

11. git pull

The git pull command fetches and merges changes from the remote repository into your local branch. It is a combination of git fetch and git merge.

Example:

git pull origin main

This command pulls the latest changes from the main branch of the remote repository.

12. git push

The git push command is used to upload local repository changes to a remote repository, such as GitHub or GitLab.

Example:

git push origin main

This uploads your changes to the main branch of the remote repository.

13. git remote

Use the git remote command to manage remote repositories. It is useful for adding, removing, or listing remote repositories.

Example:

git remote add origin https://github.com/username/repository.git
git remote -v  # List remote repositories

14. git fetch

The git fetch command downloads objects and refs from another repository, but it doesn’t merge them into your working directory. Use this command to update your local copy of the repository without merging changes.

Example:

git fetch origin

15. git reset

The git reset command is used to undo changes. It can be used to unstage files or even reset to a previous commit.

Example:

git reset HEAD filename       # Unstage a file
git reset --hard commit-hash  # Reset the entire working directory to a previous commit

Best Practices for Using Git

Here are some tips to effectively use Git in your development workflow:

  1. Commit Often: Commit your changes frequently with clear messages to document the evolution of your project.
  2. Write Meaningful Commit Messages: A good commit message should explain the purpose of the change in a clear, concise manner.
  3. Use Branches for Features: Always create a new branch for each new feature or bug fix to keep your main branch clean and stable.
  4. Pull Before You Push: Before pushing your changes, always pull the latest changes from the remote repository to avoid conflicts.
  5. Resolve Merge Conflicts Promptly: Address merge conflicts as soon as they occur, and always test your changes before committing them.

Ashutosh Dixit

I am currently working as a Senior Technical Support Engineer with VMware Premier Services for Telco. Before this, I worked as a Technical Lead with Microsoft Enterprise Platform Support for Production and Premier Support. I am an expert in High-Availability, Deployments, and VMware Core technology along with Tanzu and Horizon.

Leave a Reply