Gitsigns — Inline Git Information
Gitsigns shows git diff information directly in Neovim's sign column — added, modified, and deleted lines marked inline. It also allows staging, unstaging, and previewing individual hunks without leaving the editor.
Core Idea
Gitsigns tells you "what changed in this file right now" — without opening a terminal. Combine it with Neogit or fugitive for a complete git workflow inside Neovim.
Installation
lua/plugins/git.lua
return {
{
"lewis6991/gitsigns.nvim",
event = { "BufReadPre", "BufNewFile" },
opts = {
signs = {
add = { text = "▎" },
change = { text = "▎" },
delete = { text = "" },
topdelete = { text = "" },
changedelete = { text = "▎" },
untracked = { text = "▎" },
},
signcolumn = true,
numhl = false,
linehl = false,
word_diff = false,
watch_gitdir = { interval = 1000 },
current_line_blame = false, -- toggle with <leader>gB
current_line_blame_opts = {
virt_text = true,
virt_text_pos = "eol",
delay = 500,
},
on_attach = function(bufnr)
local gs = package.loaded.gitsigns
local map = function(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc })
end
-- Navigate hunks
map("n", "]h", function()
if vim.wo.diff then return "]c" end
vim.schedule(function() gs.next_hunk() end)
return "<Ignore>"
end, "Next hunk")
map("n", "[h", function()
if vim.wo.diff then return "[c" end
vim.schedule(function() gs.prev_hunk() end)
return "<Ignore>"
end, "Prev hunk")
-- Stage / reset
map("n", "<leader>ghs", gs.stage_hunk, "Stage hunk")
map("n", "<leader>ghr", gs.reset_hunk, "Reset hunk")
map("v", "<leader>ghs", function()
gs.stage_hunk({ vim.fn.line("."), vim.fn.line("v") })
end, "Stage selection")
map("v", "<leader>ghr", function()
gs.reset_hunk({ vim.fn.line("."), vim.fn.line("v") })
end, "Reset selection")
-- Buffer actions
map("n", "<leader>ghS", gs.stage_buffer, "Stage buffer")
map("n", "<leader>ghR", gs.reset_buffer, "Reset buffer")
map("n", "<leader>ghu", gs.undo_stage_hunk, "Undo stage hunk")
-- Preview
map("n", "<leader>ghp", gs.preview_hunk, "Preview hunk")
map("n", "<leader>ghd", gs.diffthis, "Diff this file")
map("n", "<leader>ghD", function() gs.diffthis("~") end, "Diff vs HEAD~")
-- Blame
map("n", "<leader>gB", gs.toggle_current_line_blame, "Toggle blame")
map("n", "<leader>gb", function() gs.blame_line({ full = true }) end, "Blame line")
-- Text object: select hunk
map({ "o", "x" }, "ih", ":<C-U>Gitsigns select_hunk<CR>", "In hunk")
end,
},
},
}
Gitsigns Key Bindings Summary
| Key | Action |
|---|---|
]h | Next changed hunk |
[h | Previous changed hunk |
<leader>ghs | Stage hunk under cursor |
<leader>ghr | Reset (discard) hunk |
<leader>ghS | Stage entire file |
<leader>ghR | Reset entire file |
<leader>ghu | Undo last stage |
<leader>ghp | Preview hunk in float |
<leader>ghd | Diff current file |
<leader>gB | Toggle line blame |
<leader>gb | Show full blame for line |
Sign Column Icons Meaning
| Icon | Meaning |
|---|---|
▎ (green) | New line added |
▎ (yellow) | Line modified |
▾ (red) | Line deleted below |
▴ (red) | Line deleted above |
Hunk Text Object
With gitsigns configured, ih works as a text object:
vih → visually select a changed hunk
dih → delete a hunk
yih → yank a hunk