Git Mastery: From Beginner Basics to Advanced Techniques

Git Mastery is your ultimate resource for learning Git—from easy, beginner-friendly tutorials to deep dives into advanced strategies. Discover step-by-step guides on everything from Git fundamentals to branching, merging, and optimizing workflows, empowering you to manage your code with confidence.

Git Cheatsheet: Quick Reference for Common Commands

2025-03-03

Introduction

This cheatsheet provides a concise reference to the most common Git commands. Whether you're just starting out or need a quick reminder, these commands will help you navigate your Git workflow with ease.

Basic Commands

Use these commands for everyday Git operations:

Initialize a Repository

git init

Starts a new Git repository in the current directory.

Check Status

git status

Displays the current state of your working directory and staging area.

Add Files

git add [filename]

Adds changes from the specified file to the staging area. Use git add . to stage all changes.

Commit Changes

git commit -m "Your commit message"

Saves your staged changes as a new commit.

Branching and Merging

Manage parallel development and combine changes with these commands:

Create a Branch

git branch new-feature

Creates a new branch called "new-feature".

Switch Branches

git checkout new-feature

Switches to the "new-feature" branch.

Merge Branches

git merge new-feature

Merges the "new-feature" branch into your current branch.

Delete a Branch

git branch -d new-feature

Deletes the "new-feature" branch (only if it's fully merged).

Remote Operations

Commands for interacting with remote repositories:

Add a Remote

git remote add origin https://github.com/username/repository.git

Connects your local repository to a remote server named "origin".

Push Changes

git push -u origin main

Sends your commits on the "main" branch to the remote repository and sets the upstream reference.

Pull Updates

git pull origin main

Fetches and integrates changes from the remote "main" branch into your current branch.

Advanced Commands

For more detailed operations and workflow management:

View Commit History

git log

Displays a list of commits with details such as author, date, and commit message.

Compare Changes

git diff

Shows the differences between your working directory, staging area, and commits.

Stash Changes

git stash

Temporarily saves your uncommitted changes so you can work on something else.

Rebase Your Branch

git rebase main

Reapplies commits from your current branch on top of the latest commits from "main".

Other Useful Commands

Additional commands to further streamline your workflow:

Clone a Repository

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

Creates a copy of an existing repository on your local machine.

View Differences Between Branches

git diff branch1 branch2

Shows the differences between two specified branches.

Reset Changes

git reset --hard HEAD~1

Resets your repository to the previous commit, discarding all changes after that commit.

Conclusion

Keep this cheatsheet handy as a quick reference guide to the most common Git commands. With regular use, these commands will become second nature, making your development workflow smoother and more efficient. Happy coding!