The Essential Git Cheat Sheet for Beginners
Welcome to the Essential Git Cheat Sheet! Whether you’re new to Git or looking to refresh your memory, this guide will provide you with the fundamental commands you need to navigate through your version control journey.
Setup and Configuration
Set Your Identity
Ensure Git knows who you are before you start committing changes.
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
Repository Management
1. Initialize a New Repository
Start a new Git repository in your project directory.
git init
2. Clone an Existing Repository
Clone an existing repository from a remote source.
git clone <repository-url>
Basic Snapshotting
1. Add Files to the Staging Area
Stage changes ready for commit.
git add <filename>
2. Commit Changes
Commit staged changes to the repository.
git commit -m "Commit message"
Branching and Merging
1. Create a New Branch
Start a new branch to work on a feature or bug fix.
git branch <branch-name>
2. Switch to a Branch
Move to a different branch in your repository.
git checkout <branch-name
3. Merge a Branch
Merge changes from one branch into another.
git merge <branch-name>
Remote Repositories
1. Add a Remote Repository
Link your local repository to a remote source.
git remote add origin <repository-url>
2. Push Changes
Upload your commits to a remote repository.
git push origin <branch-name>
3. Pull Changes
Fetch and merge changes from a remote repository.
git pull
Inspecting and Comparing
1. Show Status
View the status of your working directory and staging area.
git status
2. Show Commit History
Explore the commit history of your repository.
git log
3. Show Changes
Inspect the differences between commits, the working tree, etc.
git diff
Undoing Changes
1. Unstage Files
Remove files from the staging area.
git reset <filename>
2. Revert Changes
Discard changes in the working directory.
git checkout -- <filename>
3. Reset to a Commit
Reset your repository to a specific commit.
git reset --hard <commit>
Conclusion
This cheat sheet will serve as your go-to reference as you navigate through various Git operations. Happy coding!