Skip to main content

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

KeyAction
]hNext changed hunk
[hPrevious changed hunk
<leader>ghsStage hunk under cursor
<leader>ghrReset (discard) hunk
<leader>ghSStage entire file
<leader>ghRReset entire file
<leader>ghuUndo last stage
<leader>ghpPreview hunk in float
<leader>ghdDiff current file
<leader>gBToggle line blame
<leader>gbShow full blame for line

Sign Column Icons Meaning

IconMeaning
(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

What's Next