🧠 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.

# See history
git log --oneline --graph --decorate

πŸ‘‰ 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)

Working Directory β†’ Staging Area β†’ Local Repository β†’ Remote Repository
  1. Edit files β†’ git add
  2. Save snapshot β†’ git commit
  3. Share/pull changes β†’ git push / git pull

πŸ”§ 4. Quick Command Reference

Initialize or Clone

git init                           # start a new repo
git clone https://github.com/user/repo.git

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:

main
 β”œβ”€ feature/login
 β”œβ”€ feature/dashboard
 └─ fix/typo

Branch naming convention: feature/…, fix/…, refactor/…, release/…

Recommended pattern:

  • main β€” stable release branch
  • develop β€” active integration branch
  • feature/* β€” short-lived branches
  • hotfix/* β€” emergency fixes
  • release/* β€” 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-lease prevents clobbering a remote you don’t expect β€” it fails if remote changed since you last fetched.
  • If you accidentally delete something, git reflog can usually recover the commit SHA.

🧩 7. Working with Remotes (GitHub, etc.)

git remote add origin https://github.com/user/repo.git
git push -u origin main

Next time, just:

git push
git pull

Clone and work:

git clone <url>
git switch -c feature/branch

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.

git cherry-pick <commit>
* Stash: temporarily save changes.

git stash
git stash pop
* Hooks: automate pre-commit checks and CI workflows. * Submodules: include external repos inside your repo.


βœ… 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 status and git log are 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:

docs/
└─ cheatsheets/
   └─ tools/
      └─ git/
         └─ quick-start.md

πŸ’» 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):

<type>(scope): short summary

[optional body]
[optional footer]

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:

  1. Start new work

    git switch -c feature/login
    
    (Or use JetBrains β€œNew Branch” button.)

  2. Code β†’ test β†’ commit

  3. Stage meaningful changes
  4. Commit with a clear message:
    feat(login): add session management

  5. Stay up to date

git fetch origin
git rebase origin/main

(Or β€œRebase onto Main” in IDE.)

  1. Push for review

git push -u origin feature/login

(Or β€œPush” in IDE.)

  1. 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 .gitignore tight β€” no IDE caches or .venv/ folders.
  • Avoid committing binaries, logs, or secrets.
  • Use .gitattributes to manage line endings across systems.
  • Squash small commits before merging to keep history readable:

```bash git rebase -i HEAD~5 ````

  • Use git tag for 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.