Git Tooling: Shell Prompt and Completion, GUI Clients, and External Diff and Merge Tools
Make Git easier to use day to day: tab completion and a branch-aware shell prompt for Bash and zsh, graphical and terminal clients from gitk to lazygit, editor integration, and configuring external diff and merge tools with difftool, mergetool and custom commands.
kvi5hnuCode Sprout team member6 min readOn this page
Git's command line is complete, but a few additions make it much more pleasant: tab completion for commands, branches and options, a prompt that shows the current branch and state, a graphical or terminal client for browsing history, and a visual diff and merge tool for conflicts. This article covers setting each one up.
Tab completion
With completion, git sw<Tab> completes to git switch, git switch fea<Tab> to a branch name, and git log --gr<Tab> to --graph. Git ships completion scripts for Bash and zsh in its contrib/completion folder:
- Git for Windows, Homebrew and most Linux packages install and enable Bash completion automatically. If
git sw<Tab>already works, you're set. - zsh has its own Git completion built in, enabled with
autoload -Uz compinit && compinitin~/.zshrc. Frameworks such as Oh My Zsh do this for you. - fish and PowerShell have their own: fish includes Git completion, and PowerShell users install the posh-git module.
To enable the Bash script by hand, download git-completion.bash for your Git version and source it:
# ~/.bashrc
source ~/.git-completion.bash
Completion covers aliases too: an alias to log completes log's options (article 34).
A prompt that shows the branch
A prompt like ~/trail-guide (feature/maps *+)$ shows you the current branch and whether there are changes, which prevents committing to the wrong branch. Git's contrib/completion/git-prompt.sh provides a function, __git_ps1, for Bash and zsh:
# ~/.bashrc
source ~/.git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=1 # * for unstaged, + for staged changes
GIT_PS1_SHOWUNTRACKEDFILES=1 # % when there are untracked files
GIT_PS1_SHOWSTASHSTATE=1 # $ when there are stashes
GIT_PS1_SHOWUPSTREAM=auto # < behind, > ahead, <> diverged, = up to date
PS1='\w$(__git_ps1 " (%s)")\$ '
The prompt also shows operations in progress, such as (main|MERGING), (feature|REBASE 2/5) or ((a1b2c3d...)) for a detached HEAD, which is a valuable reminder when a command stopped halfway. For zsh, use setopt PROMPT_SUBST and PS1='%~$(__git_ps1 " (%s)")%# ', or zsh's own vcs_info.
Cross-shell prompt programs such as Starship, and themes such as Powerlevel10k for zsh or Oh My Posh for PowerShell, show the same information with less setup. In very large repositories, turn off the dirty-state check (GIT_PS1_SHOWDIRTYSTATE), or enable core.fsmonitor (article 33), since the prompt runs on every command.
Graphical and terminal clients
Git comes with two simple graphical tools:
gitkshows history as a graph, with diffs.gitk --allincludes every branch.git guiis for staging and committing, including staging single lines.
Both need Tcl/Tk, which some Linux distributions package separately (gitk and git-gui packages).
Popular alternatives:
| Kind | Examples |
|---|---|
| Editors and IDEs | VS Code's Source Control view, with extensions such as GitLens and Git Graph; the JetBrains IDEs' Git tool window; Visual Studio; Xcode |
| Desktop clients | GitHub Desktop, Sourcetree, GitKraken, Fork, Tower, Sublime Merge, SmartGit |
| Terminal interfaces | lazygit (stage, commit, rebase and resolve with keyboard shortcuts), tig (browse history and blame), gitui |
| Better diffs in the terminal | delta and diff-so-fancy, used as Git's pager |
Graphical tools use the same repository as the command line, so mix them freely: browse history in a client, then run commands in the terminal. Knowing the commands still matters, because clients hide details, and some operations, like recovery from the reflog, are easiest from the command line.
To use delta as the pager for diffs:
[core]
pager = delta
[interactive]
diffFilter = delta --color-only
[delta]
navigate = true
side-by-side = true
External diff tools: git difftool
git difftool takes the same arguments as git diff, but opens each changed file in a visual diff program. Configure the tool once:
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff "$LOCAL" "$REMOTE"'
git config --global difftool.prompt false # don't ask before each file
Git knows many tools by name (meld, kdiff3, vimdiff, bc for Beyond Compare, p4merge, opendiff for FileMerge on macOS, winmerge, vscode and more), so for those, diff.tool alone is enough. git difftool --tool-help lists which are available on your machine. For anything else, define difftool.<name>.cmd, where Git provides the variables $LOCAL (the old version, in a temporary file) and $REMOTE (the new version):
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Lake loop: 5.4 km" > lake.txt
git config set difftool.plain.cmd 'diff "$LOCAL" "$REMOTE"'
git difftool --no-prompt --tool=plain lake.txt
echo "exit status $?"
1c1
< Lake loop: 5 km
---
> Lake loop: 5.4 km
exit status 0
Useful forms:
git difftool # unstaged changes, file by file
git difftool --staged # staged changes
git difftool main..feature # between branches
git difftool -d main feature # whole folders at once, in tools that support it
-d (--dir-diff) opens a single comparison of two folders instead of one window per file, which suits reviewing a whole branch in tools like Meld or Beyond Compare.
Merge tools: git mergetool
During a merge conflict (article 13), git mergetool opens each conflicted file in a three-way merge tool, showing your version, theirs, the common base, and the result:
git config --global merge.tool meld # or kdiff3, vimdiff, p4merge, vscode...
git config --global mergetool.keepBackup false
git mergetool
For a custom tool, mergetool.<name>.cmd gets four variables: $BASE (the common ancestor), $LOCAL (your version), $REMOTE (the other branch's) and $MERGED, the file to write the result into. Set trustExitCode if the tool's exit status reliably says whether the merge succeeded:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c metric; echo "Lake loop: 5.0 km" > lake.txt; git commit -q -am "Use one decimal"
git switch -q main; echo "Lake loop: 3.1 miles" > lake.txt; git commit -q -am "Use miles"
git merge metric > /dev/null
# a "tool" that keeps both versions, one after the other
git config set mergetool.keepboth.cmd 'cat "$LOCAL" "$REMOTE" > "$MERGED"'
git config set mergetool.keepboth.trustExitCode true
git config set mergetool.keepBackup false
git mergetool --no-prompt --tool=keepboth > /dev/null
git status --short
cat lake.txt
M lake.txt
Lake loop: 3.1 miles
Lake loop: 5.0 km
After the tool exits successfully, mergetool stages the file, M , ready for git commit. Without keepBackup false, it leaves a lake.txt.orig copy of the file with conflict markers. Configuration for VS Code as a merge tool:
[merge]
tool = vscode
[mergetool "vscode"]
cmd = code --wait --merge "$REMOTE" "$LOCAL" "$BASE" "$MERGED"
Many editors and IDEs also resolve conflicts directly, without mergetool: open the conflicted file and use the editor's merge view, then git add it.
What's current
- Most installers enable tab completion and many enable a branch-aware prompt. Starship, Powerlevel10k and Oh My Posh are popular prompt programs.
- lazygit and delta have become common companions to command-line Git.
git mergetoolknowsvscode,meld,kdiff3,vimdiff,nvimdiff,smerge(Sublime Merge) and many others by name:git mergetool --tool-helplists them.
Common questions
Tab completion doesn't work for Git. How do I enable it?
On Bash, source git-completion.bash (often installed with Git, otherwise download it for your Git version) from ~/.bashrc. On zsh, run compinit. On PowerShell, install posh-git.
How do I see the branch name in my prompt?
Source git-prompt.sh and add $(__git_ps1 " (%s)") to PS1, or use a prompt program like Starship.
git difftool asks "Launch 'meld' [Y/n]?" for every file. How do I stop it?
Set git config --global difftool.prompt false, or pass --no-prompt (-y). mergetool.prompt false does the same for merges.
Why are there .orig files after git mergetool?
mergetool keeps backups of the conflicted files. Set mergetool.keepBackup false to stop creating them, and delete existing ones, for example with git clean -n to list them first (article 28).
Summary
- Tab completion and
__git_ps1come with Git for Bash and zsh, and installers often enable them. The prompt shows branch, changes and operations in progress. gitkandgit guiare built in. Editors, desktop clients and terminal UIs like lazygit and tig cover the rest. delta improves terminal diffs.git difftoolopens diffs in a visual tool: setdiff.tool, or a customdifftool.<name>.cmdwith$LOCALand$REMOTE.-dcompares whole folders.git mergetoolresolves conflicts in a three-way tool: setmerge.tool, ormergetool.<name>.cmdwith$BASE,$LOCAL,$REMOTEand$MERGED.keepBackup falseavoids.origfiles.
Practice questions
How do you make git difftool use VS Code without prompting for each file?
Answer
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff "$LOCAL" "$REMOTE"'
git config --global difftool.prompt false
vscode is also known to Git by name, so diff.tool vscode alone works if code is on your PATH.
Your prompt shows (main|REBASE 3/7). What does it mean, and what are your options?
Answer
A rebase is in progress and stopped at commit 3 of 7, usually because of a conflict or an edit step. Resolve and run git rebase --continue, skip the commit with git rebase --skip, or undo the whole rebase with git rebase --abort (article 14).
In a custom mergetool.<name>.cmd, which variable must the tool write the resolved result to?
Answer
$MERGED, which is the conflicted file in your working tree. $LOCAL, $REMOTE and $BASE are temporary copies of your version, the other version and the common ancestor, for the tool to read.
Review quiz
Test yourself on Part 7 with the Git Part 7 review quiz.
Previous: Git Hooks: pre-commit, commit-msg, pre-push, Server-Side Hooks and Sharing Hooks
Next: Tags and Releases: Lightweight and Annotated Tags, describe and archive

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.