Skip to content

Instantly share code, notes, and snippets.

@Bergiu
Last active February 7, 2025 16:55
Show Gist options
  • Save Bergiu/21fe121af261bb13678e3e01e0ae00a0 to your computer and use it in GitHub Desktop.
Save Bergiu/21fe121af261bb13678e3e01e0ae00a0 to your computer and use it in GitHub Desktop.
FZF Personalised Git Diff
_git_diff_author() {
# Toggle the comments if you use diff-so-fancy
#view_diff='git diff $(git my-last-commit) --color=always -- $(echo {}) | diff-so-fancy | less -R'
view_diff='git diff $(git my-last-commit) --color=always -- $(echo {}) | less -R'
git my-diff-since-last-commit --name-status | cut -f2- | tr \\t ' ' |\
fzf --preview="$view_diff" \
--cycle --ansi --no-multi \
--header="ENTER fullscreen, C-F toggle view, C-Y copy filename, C-O open" \
--bind "enter:execute:$view_diff" \
--bind "ctrl-f:change-preview-window(up,80%|default)" \
--bind "ctrl-u:preview-half-page-up" \
--bind "ctrl-d:preview-half-page-down" \
--bind "ctrl-y:execute:echo {} | clip.exe" \
--bind "ctrl-o:execute:code {}"
}
[alias]
first-child = "!f() { git log --reverse --ancestry-path --pretty=%H $1..HEAD | head -n 1; }; f"
my-last-commit = "!f() { git log --author=\"$(git config user.name)\" --pretty=%h -n 1; }; f"
my-files-only = "!f() { git log --author \"$(git config user.name)\" --name-only --no-merges --pretty=format:\"\" | sort -u; }; f"
my-renamed-files = "!f() { git diff --name-status -C $(git my-last-commit) --diff-filter=R | grep \"$(git my-files-only)\" | cut -f3; }; f"
my-files = "!f() { git my-files-only; git my-renamed-files; }; f"
my-diff = "!f() { git diff $@ -- $(git my-files); }; f"
my-diff-since-last-commit = "!f() { git my-diff $(git my-last-commit) $@; }; f"
personal-diff = "!f() { bash -c 'source $HOME/.bash_aliases && _git_diff_author;'; }; f"

Requirements:

  • fzf
  • git

Commands:

  • git first-child REV: Returns the first child of REV
  • git my-last-commit: Returns the last commit of the current user (from the current branch)
  • git my-files: Returns a list of files the current user has changed sometime
  • git my-diff REV: Returns a diff from HEAD to REV with a filter to my-files
  • git my-diff-since-last-commit: The diff from HEAD to my-last-commit with the filter to my-files. This is the diff that is relevant for the user.
  • git personal-diff: Same as above, but in fzf.

The personal diff will open fzf with the files that are relevant to the user. In fzf the files diff is previewed.

  • enter: Open the diff
  • ctrl-f: Toggle view (vertical to horizontal)
  • ctrl-u, ctrl-d: Scrol up/down half a page in the preview
  • ctrl-n, ctrl-p: Scrol down/up in the file list
  • shift-up, shift-down: Scrol up/down in the preview
  • ctrl-y: Copy the filename to the clipboard (clip.exe, you might change that)
  • ctrl-o: Opens the file in VS Code (you might change that too)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment