π Setting Up Git (For the Newbies)
Before diving into the fun stuff, letβs configure Git:
1
2
| git config --global user.name "Your Name"
git config --global user.email "you@example.com"
|
You can check your setup with:
Boom!
Git now knows who you are.
No more anonymous commits from “Some Guy on the Internet.”
π¬ Starting a New Project (Or Cloning an Existing One)
Initialize a new repo:
Clone an existing repo:
1
| git clone https://github.com/user/repo.git
|
Now you have a Git-powered project.
Itβs like giving your project superpowers (but with great power comes great responsibility).
π Tracking Changes
Check the status of your repo:
This tells you whatβs changed, whatβs staged, and whether you accidentally committed debug print statements.
Add files to staging:
1
2
| git add file.txt # Add a specific file
git add . # Add all changes
|
Commit your changes:
1
| git commit -m "Add a meaningful commit message"
|
Make sure your commit messages donβt look like:
1
2
3
| git commit -m "Fixed stuff"
git commit -m "Final fix"
git commit -m "FINAL final fix"
|
Future you (and your team) will thank you.
π Working with Branches
Create a new branch:
1
| git branch feature-branch
|
Switch to another branch:
1
| git checkout feature-branch
|
Or, since Git 2.23, use:
1
| git switch feature-branch
|
Create and switch in one go:
1
| git checkout -b new-branch
|
List all branches:
Delete a branch (once you’re done with it):
1
| git branch -d old-branch
|
Use -D
(uppercase) if Git refuses to delete it because it’s not merged yet.
π€ Pushing and Pulling
Push your code to remote:
1
| git push origin branch-name
|
Pull latest changes from remote:
1
| git pull origin branch-name
|
π Merging and Rebasing
Merge branches:
1
2
| git checkout main
git merge feature-branch
|
Rebase (for a cleaner commit history):
1
2
| git checkout feature-branch
git rebase main
|
Abort a bad rebase:
Think of merge
as adding a new chapter, while rebase
rewrites history.
Use wisely.
π Reset, Revert, and Stash (a.k.a. “Oops, Undo!”)
Undo the last commit (without losing changes):
1
| git reset --soft HEAD~1
|
Undo the last commit (and erase changes):
1
| git reset --hard HEAD~1
|
Revert a commit (create a new commit that undoes it):
1
| git revert <commit-hash>
|
Stash changes (hide them temporarily):
Apply stashed changes:
π΅οΈββοΈ Finding Stuff
View commit history:
1
| git log --oneline --graph --decorate --all
|
See who changed a specific file:
Search in commits:
π₯ Dealing with Conflicts
When Git screams about merge conflicts:
- Open the conflicting file.
- Manually fix the differences (keep what you want, delete what you donβt).
- Stage the file:
git add file.txt
- Commit the changes:
git commit -m "Fix merge conflict"
Merge conflicts are like unexpected plot twists in a movie.
Sometimes painful, but necessary.
π Wrapping Up
Git is an essential tool, and mastering it will save you a ton of headaches.
This cheatsheet covers the basics, but Git is deep, so donβt stop learning!
If all else fails, remember the golden rule:
1
| git reset --hard origin/main
|
Use it with caution.
Itβs the “Ctrl+Z” of Git, but it erases your local changes.
π Key Ideas
Key Concept | Summary |
---|
git init | Start a new Git repo |
git clone | Clone an existing repo |
git add | Stage changes |
git commit | Save staged changes |
git branch | Manage branches |
git merge | Merge branches |
git rebase | Rebase for a clean history |
git reset | Undo changes |
git stash | Temporarily store changes |
git log | View commit history |
git push/pull | Sync with remote |
π References