π§ Git: From Basics to Advanced Workflow Mastery¶
Git is a distributed version control system (DVCS) β it tracks changes to files over time, lets developers collaborate, and makes every clone a complete backup of the repository.
Itβs the tool that powers GitHub, GitLab, and most of modern software development.
βοΈ 1. What Git Actually Does¶
Git records snapshots of your project (commits), not just diffs.
Each commit stores the entire state of your code at that moment, compressed efficiently.
You can move through history (like checkpoints in a game), branch off to experiment, and merge or rebase to integrate changes.
π In short: Git is your projectβs time machine + collaboration layer.
π§± 2. Core Concepts¶
| Concept | Description |
|---|---|
| Repository (repo) | A Git-managed folder containing code and version history (.git folder inside). |
| Commit | A snapshot of your files at a moment in time. |
| Branch | A movable pointer to a commit β allows parallel development. |
| HEAD | Your current working branch (where you are in history). |
| Staging area | Where you prepare changes before committing. |
| Remote | A copy of your repo hosted elsewhere (e.g., GitHub). |
| Merge | Combines histories from different branches. |
| Rebase | Moves commits onto a new base, creating a linear history. |
βοΈ 3. The Git Workflow (mental model)¶
- Edit files β
git add - Save snapshot β
git commit - Share/pull changes β
git push/git pull
π§ 4. Quick Command Reference¶
Initialize or Clone¶
Inspect¶
git status # show changed files
git diff # show unstaged changes
git log --oneline --graph --decorate
Stage and Commit¶
git add file.txt # stage file
git add . # stage all changes
git commit -m "Add new feature" # create commit
Branch and Merge¶
git branch # list branches
git switch -c feature/login # create + switch
git merge feature/login # merge into current branch
git branch -d feature/login # delete local branch
Sync with Remote¶
git remote -v # show remotes
git fetch # get new data, no merge
git pull # fetch + merge current branch
git push origin main # upload local commits
πΏ 5. Branching and Collaboration Patterns¶
Main workflow:
Branch naming convention:
feature/β¦, fix/β¦, refactor/β¦, release/β¦
Recommended pattern:
mainβ stable release branchdevelopβ active integration branchfeature/*β short-lived brancheshotfix/*β emergency fixesrelease/*β prep for deployment
β‘ 6. Undoing and Time Travel¶
git restore file.txt # discard unstaged changes
git restore --staged file.txt # unstage
git checkout <commit> -- file.txt # restore from old commit
git reset --soft HEAD~1 # undo last commit, keep changes staged
git reset --hard HEAD~1 # undo commit + changes
git revert <commit> # create new commit that undoes another
π reset rewrites history; revert adds a new commit that undoes previous work safely.
Caution
Attention when rewriting history on shared branches!
- Update the remote to match your local (force rewrite remote branch):
# safer force (recommended even when alone)
git push --force-with-lease origin <your-branch>
# or force (less safe)
git push --force origin <your-branch>
Notes:
--force-with-leaseprevents clobbering a remote you donβt expect β it fails if remote changed since you last fetched.- If you accidentally delete something,
git reflogcan usually recover the commit SHA.
π§© 7. Working with Remotes (GitHub, etc.)¶
Next time, just:
Clone and work:
Sync fork or upstream:
git remote add upstream https://github.com/source/repo.git
git fetch upstream
git merge upstream/main
π§° 8. Real-World Example (Feature Workflow)¶
# Start new work
git switch -c feature/login
# Edit code
git add .
git commit -m "Implement login feature"
# Update main and rebase to stay up-to-date
git fetch origin
git rebase origin/main
# Push branch for review
git push -u origin feature/login
# After merge, clean up
git switch main
git pull
git branch -d feature/login
π§Ό 9. Maintenance and Cleanup¶
git branch -vv # see tracking info
git fetch -p # prune deleted remote branches
git gc # clean up unnecessary files and optimize repo
π§ 10. Common Troubleshooting¶
| Problem | Fix |
|---|---|
| Accidentally committed wrong file | git reset HEAD~1 then re-commit |
| Merge conflict | Edit conflicted file β git add . β git commit |
| Detached HEAD | git switch main to reattach |
| βnon-fast-forwardβ error on push | Pull first (git pull --rebase) then push again |
| Wrong commit message | git commit --amend -m "New message" |
π§© 11. Advanced Tools (for later)¶
- Rebase vs Merge: Rebase keeps history linear, merge preserves branching.
- Cherry-pick: apply specific commits between branches.
β Summary¶
- Git snapshots your code and lets you time-travel safely.
- Commits build your local history, remotes sync it across the team.
- Branches isolate work; merges and rebases integrate it.
git statusandgit logare your best debugging friends.- Never panic β you can almost always recover history in Git.
π§ Next Step Ideas¶
When you outgrow this refresher:
- Split into
git-basics.md,git-advanced.md,git-troubleshooting.md - Add practical guides for branching strategies or GitHub workflows.
π File path suggestion:
π» 12. Git in Your IDE (JetBrains & VS Code)¶
Git isnβt just a command-line tool β every modern IDE wraps it into a visual workflow.
But it helps to understand what each action actually does under the hood.
π§© JetBrains IDEs (IntelliJ IDEA, PyCharm, etc.)¶
| Action | What It Actually Does |
|---|---|
| Commit (Ctrl+K) | Stages & commits changes locally. Equivalent to git add + git commit. |
| Push (Ctrl+Shift+K) | Uploads commits to the remote (git push). |
| Update Project (Ctrl+T) | Fetch + merge (or rebase, if configured). |
| Rebase Onto Upstream | Equivalent to git rebase origin/main. Keeps history clean. |
| Show History (Alt+9 β Log tab) | Visual git log + branch graph. |
| Cherry-pick Commit | Applies selected commit(s) onto your branch (git cherry-pick). |
| Shelve Changes | JetBrains' own version of git stash β great for quick context switches. |
π Pro tip: JetBrains auto-detects Git roots. If a folder contains .git, it treats it as a repo automatically.
Good workflow habit inside JetBrains: 1. Regularly commit small, meaningful chunks. 2. Rebase before pushing to keep branches fast-forwardable. 3. Always inspect your commit diff before confirming.
π§ VS Code Integration¶
VS Codeβs Source Control panel is a friendly wrapper around core Git commands.
| Icon | Action | Git Equivalent |
|---|---|---|
| β | Commit | git commit |
| β¬οΈ | Push | git push |
| β¬οΈ | Pull | git pull |
| π | Sync Changes | Pull + Push |
| β | Stage Change | git add |
| ποΈ | Discard Change | git restore |
Extensions worth adding: - GitLens β deep history, blame, branch insights. - Git Graph β visual branching and merges. - GitHub Pull Requests β manage PRs directly in VS Code.
βοΈ 13. Writing Great Commit Messages¶
Clear commit messages are part of professional hygiene.
A Git log should read like a timeline of meaningful decisions, not noise.
Format (conventional style):
Examples:
feat(auth): add JWT token validation
fix(api): correct null pointer on user fetch
docs(readme): add setup instructions
refactor(core): simplify cache layer
Common types:
| Type | Meaning |
|---|---|
feat |
new feature |
fix |
bug fix |
docs |
documentation change |
refactor |
code restructure without behavior change |
test |
adding or fixing tests |
chore |
maintenance or tooling changes |
π A good commit message should explain why a change exists, not just what changed.
π§© 14. Practical Git + IDE Workflow Example¶
Hereβs how a typical developer loop looks:
-
Start new work
(Or use JetBrains βNew Branchβ button.) -
Code β test β commit
- Stage meaningful changes
-
Commit with a clear message:
feat(login): add session management -
Stay up to date
(Or βRebase onto Mainβ in IDE.)
- Push for review
git push -u origin feature/login
(Or βPushβ in IDE.)
- After PR merge
git switch main git pull git branch -d feature/login
(Or βDelete Branchβ safely in IDE.)
π§Ό 15. Tips for Clean Repositories¶
- Keep
.gitignoretight β no IDE caches or.venv/folders. - Avoid committing binaries, logs, or secrets.
- Use
.gitattributesto manage line endings across systems. - Squash small commits before merging to keep history readable:
```bash git rebase -i HEAD~5 ````
- Use
git tagfor versioning:
bash
git tag -a v1.0.0 -m "Initial release"
git push origin v1.0.0
β Final Notes¶
- The IDE is just a lens β Git remains the same underneath.
- Always review the diff before committing.
- Favor small, focused commits over massive all-in-one pushes.
- Treat your Git history as a story β future you will thank you.