Git Cheatsheet for Beginners

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).
Introduction to Git
Version Control System: Track changes, collaborate, and manage code efficiently.
Platform: Use Replit for a browser-based experience without setup hassles.
Quick Start Guide
1. Create a Replit Project
Sign in to [Replit].
Create a New Project:
Select Developer Framework.
Search for bash and select the Bash Framework.
Click Remix and name it
my-markdown-project.
2. Check Git Version
bash
git --version
- Expected output:
git version 2.xx.x.
3. Configure Git (One-Time Setup)
Set your username and email:
bash
git config --global user.name "YourName"
git config --global user.email "youremail@example.com"
Check your configuration:
bash
git config --global --list
4. Create a Project Directory
bash
mkdir my-first-git-project
cd my-first-git-project
echo "Hello, Git!" > hello.txt
5. Initialize a Git Repository
bash
git init
6. Check Git Status
bash
git status
7. Add File to Staging Area
bash
git add hello.txt
- To add all files:
bash
git add .
8. Commit Your Changes
bash
git commit -m "First commit: added hello.txt"
9. Make a Change and Commit Again
Edit your file:
bash
echo "Git is awesome!" >> hello.txt
Check changes:
bash
git diff
Stage and commit:
bash
git add hello.txt
git commit -m "Added a second line"
10. View Commit History
Normal view:
bash
git log
Compact view:
bash
git log --oneline
11. Create a New Markdown Git Project
Move to the parent directory: bash
cd ..Create a new repository: bash
mkdir my-git-journal cd my-git-journal git init echo "# My Git Journal" > README.md git add README.md git commit -m "Started my Git journal"View the log: bash
git log --oneline
12. Undoing Changes
Restore a modified file: bash
git checkout -- hello.txtUnstage a file: bash
git restore --staged hello.txtUndo the last commit:
bash
git reset --soft HEAD~1 # Keeps changes staged
# or
git reset --hard HEAD~1 # Deletes commit and changes
Key Commands Summary
| Command | Description |
git --version | Check Git version |
git config --global user.name "YourName" | Set username |
git config --global user.email "youremail@example.com" | Set email |
git init | Initialize a Git repository |
git status | Check the status of the repository |
git add <file> | Stage changes for commit |
git commit -m "message" | Commit staged changes with a message |
git log | View commit history |
git log --oneline | View commit history in compact form |
git diff | View unstaged changes |
git checkout -- <file> | Restore file from last commit |
git restore --staged <file> | Unstage a file |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --hard HEAD~1 | Undo last commit and discard changes |
Conclusion
This cheatsheet provides a quick reference for using Git effectively. By following these steps, you can manage your projects and collaborate with confidence. Happy coding!