# Git Integration in LazyVim LazyVim provides seamless git integration through LazyGit and Gitsigns. You can stage hunks, commit, push, browse history, and resolve conflicts without leaving your editor. ## LazyGit - Visual Git Interface **LazyGit** is a terminal UI for git that integrates perfectly with LazyVim. ### Open LazyGit ``` Space + g + g ``` This opens a full-screen git interface inside Neovim. ### LazyGit Interface ``` ┌─────────────────────────────────────────┐ │ Status │ Files │ Branches │ ... │ ├─────────────────────────────────────────┤ │ Modified files: │ │ src/App.tsx │ │ src/utils/helper.ts │ │ │ │ Staged files: │ │ README.md │ └─────────────────────────────────────────┘ ``` ### LazyGit Keybindings **Navigation:** - `j` / `k` - Move down/up - `h` / `l` - Move between panels - `Tab` - Next panel - `Shift+Tab` - Previous panel **File Operations:** - `Space` - Stage/unstage file - `a` - Stage all files - `A` - Unstage all files - `d` - Discard changes (careful!) - `e` - Edit file in Neovim - `Enter` - View file diff **Commits:** - `c` - Commit staged files - `C` - Commit using git editor - `Shift+W` - Commit without pre-commit hooks **Branches:** - `n` - Create new branch - `Space` - Checkout branch - `M` - Merge branch - `r` - Rebase branch - `d` - Delete branch **Remote:** - `p` - Pull - `P` - Push - `Shift+P` - Push with force **History:** - `Shift+L` - View logs - `Enter` on commit - View commit details - `w` - Open commit in browser (if GitHub) **Exit:** - `q` - Quit LazyGit - `Esc` - Go back/cancel ## Gitsigns - Inline Git Changes **Gitsigns** shows git changes directly in your editor with inline diff markers. ### Git Signs in Editor LazyVim shows changes in the sign column (left of line numbers): ``` │+ Added line │~ Modified line │_ Deleted line (shows where it was) ``` ### Navigate Changes (Hunks) ``` ]h # Next hunk (change) [h # Previous hunk (change) ``` **Workflow:** ``` # You're editing a file with multiple changes ]h # Jump to next change ]h # Jump to next change [h # Go back to previous change ``` ### Preview Hunk Diff ``` Space + g + h ``` Shows a floating window with the diff of the current hunk. **Example:** ```diff -const oldValue = 42; +const newValue = 43; ``` ### Stage/Unstage Hunks ``` Space + g + s # Stage hunk Space + g + u # Unstage hunk ``` **Workflow:** ``` # You have 5 changes in a file # Only want to commit 2 of them ]h # Jump to first change Space + g + s # Stage this hunk ]h # Jump to next change Space + g + s # Stage this hunk # Other hunks remain unstaged # Commit only staged hunks ``` ### Reset Hunk (Discard Changes) ``` Space + g + r # Reset hunk (careful!) ``` **Careful:** This discards your changes permanently! ### Stage Visual Selection ``` # In visual mode: V # Select lines Space + g + s # Stage selected lines only ``` **Use case:** Stage only part of a larger change. ### Blame Line ``` Space + g + b # Show git blame for current line ``` Shows who last modified this line and when. **Example output:** ``` John Doe (2 weeks ago) - feat: add user authentication ``` ## Common Workflows ### Workflow 1: Quick Commit **Scenario:** Changed a few files, want to commit all. ```bash Space + g + g # Open LazyGit a # Stage all files c # Commit # Type message, save and quit P # Push q # Close LazyGit ``` ### Workflow 2: Selective Staging **Scenario:** Multiple changes in one file, want to commit only some. ```bash # In your file with changes ]h # Jump to first hunk Space + g + h # Preview diff Space + g + s # Stage this hunk ]h # Next hunk Space + g + s # Stage this too # Leave other hunks unstaged Space + g + g # Open LazyGit c # Commit staged hunks ``` ### Workflow 3: Review Changes Before Commit **Scenario:** See all changes before committing. ```bash Space + g + g # Open LazyGit # Navigate to file Enter # View full diff # Review changes Esc # Back to file list Space # Stage file c # Commit ``` ### Workflow 4: Amend Last Commit **Scenario:** Forgot to include a change in last commit. ```bash # Make your additional changes Space + g + g # Open LazyGit a # Stage all Shift+A # Amend last commit # Edit message if needed # Save and quit Shift+P # Force push (if already pushed) ``` ### Workflow 5: Branch and Merge **Scenario:** Create feature branch, work, merge back. ```bash Space + g + g # Open LazyGit n # New branch # Type: feature/my-feature # Work on your code... Space + g + g # LazyGit c # Commit P # Push # Switch to main branch Space # (on main branch in list) M # Merge feature branch # Select branch to merge ``` ### Workflow 6: Resolve Merge Conflicts **Scenario:** Pull has conflicts. ```bash Space + g + g # Open LazyGit p # Pull # Conflict detected! e # Edit conflicted file # In file, you see: <<<<<<< HEAD const value = 1; ======= const value = 2; >>>>>>> branch # Manually resolve conflict # Delete conflict markers # Keep what you want # Back to LazyGit Space + g + g Space # Stage resolved file c # Commit merge ``` ## Advanced Features ### Interactive Rebase ```bash Space + g + g # LazyGit Shift+L # View log e # Edit commit (starts rebase) # LazyGit guides you through rebase ``` **Use cases:** - Squash commits - Reword commit messages - Reorder commits - Drop commits ### Stash Changes ```bash Space + g + g # LazyGit s # Stash changes # Work on something else... g # Pop stash (reapply changes) ``` **Use case:** Quickly save work to switch contexts. ### Cherry-pick Commits ```bash Space + g + g # LazyGit Shift+L # View log c # Cherry-pick commit # Select target branch ``` ### View File History ```bash # On a file in LazyGit Shift+L # View file history Enter # View commit ``` ### Diff View in Editor ```bash :DiffviewOpen # Open diff view :DiffviewClose # Close diff view :DiffviewFileHistory # View file history :DiffviewFileHistory % # History of current file ``` ## Git Commands from Command Line LazyVim has a built-in terminal, so you can still use git CLI: ```bash Ctrl+\ # Open terminal git status git log --oneline git diff exit # Close terminal ``` ## Keybinding Reference ### Gitsigns (In Editor) | Action | Keybinding | |--------|------------| | Next hunk | `]h` | | Previous hunk | `[h` | | Preview hunk | `Space g h` | | Stage hunk | `Space g s` | | Unstage hunk | `Space g u` | | Reset hunk | `Space g r` | | Blame line | `Space g b` | ### LazyGit | Action | Keybinding | |--------|------------| | Open LazyGit | `Space g g` | | Stage file | `Space` | | Stage all | `a` | | Unstage all | `A` | | Commit | `c` | | Amend | `Shift+A` | | Push | `P` | | Pull | `p` | | New branch | `n` | | Merge | `M` | | Rebase | `r` | | Logs | `Shift+L` | | Stash | `s` | | Quit | `q` | ## Tips and Tricks **1. Use hunks for atomic commits** - Stage individual changes with `Space g s` - Commit only related changes together - Makes git history cleaner **2. Preview before staging** - Always use `Space g h` to preview hunk - Verify you're staging the right change **3. Blame for context** - Use `Space g b` to see who wrote a line - Helpful for understanding why code exists **4. LazyGit is faster than CLI** - Visual interface is quicker for reviewing changes - Staging/unstaging is one keypress **5. Learn rebase in LazyGit** - Interactive rebase is easier in LazyGit than CLI - Use it to clean up commit history before PRs **6. Terminal for complex git** - Ctrl+\ opens terminal in Neovim - Use CLI for advanced git operations ## Common Mistakes **❌ Staging entire files with big changes** → Use hunk staging (`Space g s`) for granular commits **❌ Committing without reviewing diff** → Always preview with `Enter` in LazyGit or `Space g h` **❌ Force pushing without checking** → Double-check branch before `Shift+P` **❌ Not using blame** → `Space g b` gives context about why code exists **❌ Ignoring stash** → Use stash (`s` in LazyGit) when switching contexts ## Integration with GitHub/GitLab LazyGit can open commits in your browser: ```bash # In LazyGit, on a commit w # Open in web browser ``` **For this to work:** - Repo must have a remote on GitHub/GitLab - Remote must be named `origin` ## Troubleshooting **LazyGit not opening:** ```bash # Install lazygit brew install lazygit # Restart Neovim ``` **Git signs not showing:** ```vim :Gitsigns toggle_signs ``` **Merge conflicts not detected:** ```vim :Gitsigns setqflist all ``` **Related:** - [[File Navigation in LazyVim]] - Finding files to edit - [[Code Editing Workflows]] - Making the changes - [[LSP and Code Intelligence]] - Code refactoring - [[Neovim - The Vim Ecosystem]] - Understanding the editor