Git Commands Cheatsheet 🎯

beginner
21 min

Git Commands Cheatsheet 🎯

Welcome to our comprehensive Git Commands Cheatsheet! This guide is designed to help both beginners and intermediates understand and master essential Git commands. Let's dive in! 📝

Introduction to Git 💡

Git is a powerful version control system that helps developers track changes in their codebase, collaborate effectively, and manage projects efficiently. It's essential for any developer looking to work on complex projects or contribute to open-source projects.

Why use Git?

  • Version control: Git allows you to track changes in your code, making it easy to revert to previous states if necessary.
  • Collaboration: Git makes it simple for multiple developers to work on the same project without stepping on each other's toes.
  • Backup and recovery: Git automatically creates backups of your code as you work, providing a safety net in case of errors or accidents.

Basic Git Commands 📝

git init

Creates a new Git repository in your current directory.

bash
$ git init

git add <file>

Stages a file for the next commit. You can also use git add . to stage all changes.

bash
$ git add index.html

git commit -m "Commit message"

Creates a new commit with the given message.

bash
$ git commit -m "Added index.html"

git status

Shows the current state of your repository, including untracked, modified, and staged files.

bash
$ git status

git log

Displays the commit history for your repository.

bash
$ git log

Git Branching and Merging 💡

Branching allows you to work on different features or bug fixes without affecting the main codebase. Here are some essential branching commands:

git branch <branch_name>

Creates a new branch.

bash
$ git branch feature-branch

git checkout <branch_name>

Switches to a different branch.

bash
$ git checkout feature-branch

git merge <branch_name>

Merges the specified branch into the current branch.

bash
$ git checkout main $ git merge feature-branch

Advanced Git Commands 📝

git stash

Stashes changes that aren't yet committed, allowing you to switch branches or work on something else temporarily.

bash
$ git stash

git revert <commit_hash>

Reverts changes made in a specific commit.

bash
$ git revert <commit_hash>

git pull

Pulls the latest changes from the remote repository.

bash
$ git pull origin main

Quiz 🎯

Quick Quiz
Question 1 of 1

What does `git init` do?

Quick Quiz
Question 1 of 1

How do you switch to a different branch?

Quick Quiz
Question 1 of 1

What does `git pull` do?