Save your work. Track changes. Work with others.
Git tracks changes to your code.
Think of it as a history of your project:
Edit → Stage → Commit → Push
1. Start a Repository
Inside your project:
git init
This turns the folder into a Git repository.
Usually, you'll instead clone an existing project:
git clone https://github.com/user/project.git
2. Check What's Changed
git status
This is one of the commands you'll use most.
It tells you:
- modified files
- new files
- deleted files
- what's ready to commit
3. See Your Changes
git diff
Shows what you've changed but haven't committed.
4. Stage Changes
Stage one file:
git add app.java
Stage everything:
git add .
Think of staging as:
"I want these changes included in my next commit."
5. Commit
git commit -m "Fix login issue"
A commit is a saved checkpoint in your project's history.
Good commit messages describe what changed.
6. View History
git log
A cleaner version:
git log --oneline
7. Push Your Changes
git push
This sends your commits to the remote repository, such as GitHub or GitLab.
8. Get Other People's Changes
git pull
This downloads and integrates changes from the remote repository.
9. Branches
A branch lets you work on something without changing the main line of development.
Create a branch:
git switch -c feature-login
Move to an existing branch:
git switch develop
See branches:
git branch
10. Merge
Suppose you're on develop and want to merge your feature:
git switch develop
git merge feature-login
11. Undo Unstaged Changes
If you changed a file and want to throw away those changes:
git restore app.java
⚠️ The changes are lost.
12. Remove Something from Staging
If you accidentally staged a file:
git restore --staged app.java
The file remains changed; it is simply no longer staged.
13. The Everyday Git Workflow
git pull
# make changes
git status
git diff
git add .
git commit -m "Add user authentication"
git push
That's the core Git workflow.
The Commands to Remember
| What you want | Command |
|---|---|
| Clone project | git clone |
| Check changes | git status |
| See changes | git diff |
| Stage | git add |
| Commit | git commit |
| History | git log --oneline |
| Download changes | git pull |
| Upload commits | git push |
| Create branch | git switch -c |
| Change branch | git switch |
| Merge | git merge |
| Undo file changes | git restore |
The Golden Rule
Commit small, meaningful changes.
Instead of:
"stuff"
prefer:
"Fix household transfer calculation"
Your Git history should help another developer understand what happened.
Remember
git pull
↓
make changes
↓
git add
↓
git commit
↓
git push
That's Git.
