Git


Conventional Commits

    Type Meaning Usage SemVer Impact Example
    feat A new user-visible feature New endpoint, new UI component, CLI option, etc. Minor (or Major if breaking) feat(search): add fuzzy matching
    fix A bug fix Correct wrong logic, null checks, crash fixes, etc. Patch fix(parser): handle CRLF line endings
    docs Documentation only README, API docs, comments None docs(readme): add install steps for openSUSE
    style No logic change, formatting whitespace, semicolons, formatter None style: run clang-format
    refactor Code change that neither fixes a bug nor adds a feature rename, restructure, extract function None (unless breaking) refactor(ui): split dashboard into widgets
    perf Performance improvements reduce allocations, optimize query Path (or minor if user-facing behavior) perf(scanner): cut token allocs by 49%
    test Add / modify tests unit / integration/ regression tests None test: add case for empty CSV
    build Build system changes make / cmake / gradle compiler flags None build(cmake): cache vcpkg artifacts
    ci Continuoous integration GitHub Actions, Jenkins, pipelines None ci: add matrix for macOS
    chore Housekeeping bump tooling, rename files, no src change None chore: update editorconfig
    revert Revert a previous commit rollback with reference Reverts prior impact revert: feat(search) add fuzzy matching
    deps Dependency updates bump libs, lockfiles None / Patch (security fixes often Patch) deps: bump lodash to 4.17.21
    security Security fixes vulns, hardening Path (Major if breaking) security: sanitize user input


Branches

  1. In Git, a branch is simply a pointer (a reference) to a specific commit in the repo history.
    It allows you to develop features, fix bugs, or experiment independently from the main codebase.


  2. When you create a new Git repository, Git starts with a default branch (it is usually the branch that holds your production-ready code), commonly named:


  3. A new branch starts at the current commit and lets you diverge from there:
  4. git checkout -b feature/new-api
    
    # or (modern syntax)
    git switch -c feature/new-api
    
    This creates a new branch named feature/new-api starting from wherever you currently are (HEAD), and switches you to it.


  5. HEAD is Git's special pointer to your current branch or commit.
    When you checkout a branch, HEAD points to that branch. When you're in a detached HEAD state (e.g., on a specific commit), HEAD points directly to the commit hash.


  6. After a feature is merged, you often want to delete the branch:
  7. git branch -d feature/new-ui                # Local
    git push origin --delete feature/new-ui     # Remote
    


  8. Push a new branch to GitHub: git push -u origin feature/x


  9. Pull a remote branch: git fetch then git checkout feature/x


  10. List Branches:
  11. git branch          # local
    git branch -r       # remote
    git branch -a       # all
    


  12. General format of git branch naming:
  13. <type>/<short-description>
    <type>/<issue-number>-<short-description>
    


  14. Links youor current local branch to the remote-tracking branch:
  15. # assuming you're in the local branch "feature/generic",
    # and you wish to link it with the "feature/generic" in the origin remote repo.
    git branch --set-upstream-to=origin/feature/generic
    


  16. Push a local branch to the remote and set upstream.
  17. git checkout -b <branch-name>
    git push -u origin <branch-name>
    



The Three Stages of Git

  1. Introduction



  2. The Three Stages Overview

  3. Stage Description Example Files
    Working Directory Files you are editing right now modified.txt
    Staging Area (Index) Files marked for the next commit added.txt
    Repository Committed snapshots stored permanently .git/objects


  4. 1. Working Directory (Untracked or Modified State)

  5. git status
    
    Untracked files:
      newfile.txt
    
    Changes not staged for commit:
      modified: README.md
    


  6. 2. Staging Area (Index)

  7. git add filename
    git add .
    

    git status
    
    Changes to be committed:
      new file:   newfile.txt
      modified:   README.md
    



  8. 3. Repository (Local Git Database)

  9. git commit -m "message"
    

    git log --oneline
    
    a4c71f1  Add README and initial setup
    



  10. The Full Git Workflow with Example
  11. # 1. You modify two files
    echo "hello" >> a.txt
    rm b.txt
    
    # 2. You stage only one file
    git add a.txt
    
    # 3. You commit staged changes
    git commit -m "Add hello to a.txt"
    



  12. Visual Representation of the Stages
  13. Working Directory  --(git add)-->  Staging Area  --(git commit)-->  Repository


  14. Commands Related to Each Stage

  15. Stage Action Command
    Working Directory See changes git status
    Staging Area Add file to staging git add file
    Staging Area Unstage a file git restore --staged file
    Repository Commit staged changes git commit -m "message"
    Repository View history git log


  16. Undoing in Each Stage

  17. git restore file.txt
    
    git restore --staged file.txt
    
    git reset --soft HEAD~1
    
    git reset --hard HEAD~1
    


  18. Summary of the Three Stages

  19. Stage State Command to Move Forward
    Working Directory Editing files git add
    Staging Area Preparing commit git commit
    Repository Stored history git push (optional)




Git Add and Git Rm

  1. Introduction




  2. Common git add Commands
  3. git add .


    git add -A


    git add -p


    git add "*.txt"



  4. Staging Modified and Deleted Files
  5. rm old.txt
    git add old.txt
    



  6. git rm: Remove Files from Git

  7. git rm file.txt
    


  8. Common git rm Options
  9. git rm --cached file.txt
    
    git rm -r docs/
    git rm -f file.txt


  10. Workflow Examples

  11. echo "hello" > a.txt
    git add a.txt
    git commit -m "Add a.txt"
    

    git rm old.txt
    git commit -m "Remove old.txt"
    

    git rm --cached secrets.env
    echo "secrets.env" >> .gitignore
    git commit -m "Stop tracking secrets.env"
    

    git add largefile.zip
    git restore --staged largefile.zip
    


  12. Comparison Summary

  13. Command Purpose Modifies Disk? Modifies Index?
    git add file Stage file modifications No Yes
    git add -A Stage all changes No Yes
    git rm file Delete file, stage deletion Yes Yes
    git rm --cached file Remove from git, keep file No Yes


Git Stash

  1. Introduction



  2. Basic Usage: Saving Changes
  3. git stash
    

    git stash save "WIP: working on login feature"
    


  4. Saving Untracked Files
  5. git stash -u
    

    git stash -a
    


  6. Listing Stashes
  7. git stash list
    
    stash@{0}: WIP on feature/login
    stash@{1}: WIP: fixing UI
    stash@{2}: WIP: initial setup
    


  8. Applying Stashes
  9. git stash apply
    

    git stash apply stash@{2}
    


  10. Applying and Removing a Stash (Pop)
  11. git stash pop
    
    git stash pop stash@{1}
    


  12. Dropping a Stash Entry
  13. git stash drop stash@{0}
    

    git stash clear
    


  14. Stashing Only Staged Changes
  15. git stash --staged
    


  16. Stashing Only Specific Files
  17. git stash -m "WIP: styling" css/style.css
    


  18. Showing the Contents of a Stash
  19. git stash show
    

    git stash show -p stash@{1}
    


  20. Creating a Branch from a Stash
  21. git stash branch feature/login-fix stash@{0}
    


  22. Handling Merge Conflicts During Pop/Apply
  23. git stash pop
    # conflict markers appear
    



  24. Common Stash Workflow Examples

  25. git stash
    git switch main
    git pull
    git switch feature/login
    git stash pop
    

    git stash -u
    

    git stash push -m "partial fix" app.js
    

    git stash branch hotfix stash@{1}
    


  26. Summary of git stash Commands

  27. Command Description
    git stash Save tracked modified files
    git stash -u Save tracked + untracked files
    git stash apply Apply stash without removing
    git stash pop Apply and remove stash
    git stash list Show all stash entries
    git stash show -p Show stash diff
    git stash drop Delete one stash entry
    git stash clear Delete all stashes
    git stash branch name stash@{n} Create branch with stashed changes applied



Git Merge

  1. Introduction



  2. Basic Merge Example
  3. git switch main
    git merge feature/login
    


  4. Fast-Forward Merge
  5. git merge feature/ui
    
    # Before:
    main --- A --- B
                 \
                 (feature)
    
    # After fast-forward merge:
    main --- A --- B   (feature merged)
    


  6. Forcing a Merge Commit (No Fast-Forward)
  7. git merge --no-ff feature/login
    


  8. Fast-Forward vs Merge Commit

  9. Type Git Behavior Usage
    Fast-forward Moves branch pointer; no merge commit Simple linear history
    Merge commit Creates a commit that joins histories Feature branch workflow


  10. Checking Merge Status
  11. git status
    
    git log --oneline --graph --decorate
    


  12. Merge Conflicts

  13. git merge feature/login
    # CONFLICT (content): Merge conflict in src/app.js
    

    <<<<<<< HEAD
    current branch code
    =======
    incoming branch code
    >>>>>>> feature/login
    

    git add src/app.js
    git commit
    


  14. Aborting a Merge
  15. git merge --abort
    


  16. Squash Merging
  17. git merge --squash feature/api
    git commit -m "Add API feature"
    



  18. Merge vs Rebase (Conceptual)

  19. Operation Description History Style
    Merge Joins two histories with a merge commit Non-linear
    Rebase Replays your commits on top of another branch Linear



  20. Common Merge Workflow

  21. git switch main
    git pull
    git merge feature/cart
    git push
    

    git switch feature/cart
    git merge main
    

    git merge --no-ff feature/cart
    


  22. Summary of Git Merge Options

  23. Command Description
    git merge branch Merge branch into current branch
    git merge --no-ff branch Force merge commit even if fast-forward is possible
    git merge --squash branch Merge changes as one commit (no merge commit)
    git merge --abort Cancel merge and restore previous state




Git Fetch vs Git Pull

  1. Introduction



  2. How Git Stores Remote Changes



  3. git fetch: Download Only

  4. git fetch
    
    git fetch origin main
    

    git log HEAD..origin/main --oneline
    git diff HEAD..origin/main
    



  5. git pull: Fetch + Integrate

  6. git fetch
    git merge origin/<your-branch>
    

    git pull
    

    git pull --rebase
    
    git fetch
    git rebase origin/<your-branch>
    



  7. Step-by-Step Comparison

  8. Aspect git fetch git pull
    Updates remote-tracking branches? Yes Yes
    Updates current local branch? No Yes (merge or rebase)
    Risk of merge conflicts right away? No (nothing merged yet) Yes (integration happens immediately)
    Good for reviewing incoming changes? Excellent Possible, but history already changed
    Typical use case Inspect, compare, then decide how to merge Fast update to latest remote state


  9. Example: Using git fetch for Safe Updates

  10. # 1. Download changes only
    git fetch origin
    
    # 2. See what changed on origin/main
    git log HEAD..origin/main --oneline
    
    # 3. Merge (or rebase) when you're ready
    git merge origin/main
    # or
    git rebase origin/main
    



  11. Example: Quick Update with git pull

  12. # On branch main, tracking origin/main
    git pull
    

    git pull --rebase
    


  13. When to Use Which?



  14. Common Aliases and Configs

  15. git config --global pull.rebase true
    
    git config --global alias.fp "fetch --prune"
    git config --global alias.pl "pull --rebase"
    


  16. Summary

  17. Command What it does Mental Model
    git fetch Download changes from remote, update origin/* branches, leave your current branch untouched. "Update my knowledge of the remote, but do not touch my work."
    git pull Fetch changes and immediately integrate them into the current branch by merge or rebase. "Download and merge now."




Git Remote

  1. Introduction



  2. Viewing Existing Remotes

  3. git remote
    
    git remote -v
    
    origin  https://github.com/user/repo.git (fetch)
    origin  https://github.com/user/repo.git (push)
    


  4. Adding a Remote

  5. git remote add origin https://github.com/user/repo.git
    


  6. Removing a Remote

  7. git remote remove origin
    
    git remote rm origin
    


  8. Renaming a Remote

  9. git remote rename origin upstream
    


  10. Changing the URL of a Remote

  11. git remote set-url origin https://github.com/newuser/newrepo.git
    
    git remote set-url origin git@github.com:user/repo.git
    


  12. Fetching From a Remote

  13. git fetch
    
    git fetch origin
    


  14. Pushing to a Remote

  15. git push origin main
    
    git push -u origin feature/api
    


  16. Inspecting a Remote in Detail
  17. git remote show origin
    
    * remote origin
      Fetch URL: https://github.com/user/repo.git
      Push  URL: https://github.com/user/repo.git
      HEAD branch: main
      Local branches configured for 'git pull':
        main merges with remote main
    


  18. Working With Multiple Remotes

  19. git remote add upstream https://github.com/original/project.git
    
    git fetch upstream
    
    git merge upstream/main
    


  20. Removing Orphaned Remote Branch References

  21. git fetch --prune
    


  22. Summary Table

  23. Command Purpose
    git remote -v Show all remotes with their URLs.
    git remote add name url Add a new remote.
    git remote remove name Remove an existing remote.
    git remote rename old new Rename a remote.
    git remote set-url name url Change the URL of a remote.
    git remote show name Inspect remote details.
    git fetch name Download new commits from the remote.
    git push name branch Upload local commits to the remote.
    git fetch --prune Remove stale remote-tracking branches.




Git Config

  1. Introduction



  2. Viewing Configuration
  3. git config --list
    
    git config --global --list
    
    git config --get user.name
    
    git config --show-origin user.email
    



  4. Setting Up Identity

  5. git config --global user.name "Your Name"
    git config --global user.email "username@example.com"
    
    git config user.name "Local Repo Name"
    


  6. Editing Configuration Files Directly
  7. git config --global --edit
    
    git config --local --edit
    


  8. Configuring the Default Editor
  9. git config --global core.editor "nvim"
    
    git config --global core.editor "code --wait"
    


  10. Configuring Default Branch Name
  11. git config --global init.defaultBranch main
    


  12. Configuring Line Endings
  13. git config --global core.autocrlf input
    
    git config --global core.autocrlf true
    


  14. Aliases: Custom Git Commands

  15. git config --global alias.st "status"
    git config --global alias.co "checkout"
    git config --global alias.br "branch"
    git config --global alias.ci "commit"
    
    git config --global alias.lg "log --oneline --graph --decorate --all"
    

    git lg
    


  16. Configuring Merge & Diff Tools

  17. git config --global merge.tool vimdiff
    git config --global mergetool.keepBackup false
    
    git config --global diff.tool vscode
    git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
    


  18. Configure Pull Behavior (Merge vs Rebase)
  19. git config --global pull.rebase false
    
    git config --global pull.rebase true
    


  20. Configuring Push Behavior

  21. git config --global push.default current
    


  22. Enabling Colored Output

  23. git config --global color.ui auto
    
    git config --global color.diff auto
    git config --global color.status auto
    git config --global color.branch auto
    


  24. Configuring Credential Caching

  25. git config --global credential.helper cache
    
    git config --global credential.helper "cache --timeout=3600"
    

    git config --global credential.helper store
    


  26. Configuring HTTPS vs SSH

  27. git config --global url."ssh://git@github.com/".insteadOf "https://github.com/"
    


  28. Unsetting (Removing) a Config Value

  29. git config --global --unset user.email
    
    git config --unset-all alias.lg
    


  30. Summary

  31. Command Purpose
    git config --list View all effective config values.
    git config --global user.name Set username globally.
    git config --edit Edit config file using editor.
    git config --global alias.xxx Create a custom shorthand command.
    git config --global core.editor Set default editor.
    git config --global pull.rebase true Make git pull use rebase by default.
    git config --unset key Remove a config value.
    git config --show-origin key Show where config was defined.