Skip to content

Commit

Permalink
Improved Git config
Browse files Browse the repository at this point in the history
* New utility aliases (e.g. to cope with `main`/`master` branch difference)
* Prefer `switch` over `checkout`
* New aliases for working with remote `HEAD`
* Configure mergetools and difftools
* Other Git config improvements
  • Loading branch information
freemanjp committed Sep 17, 2023
1 parent 32ded50 commit 464d891
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 65 deletions.
6 changes: 5 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ trim_trailing_whitespace = true
indent_size = 2

# Shell scripts can be fussy about line endings
[*.sh]
[*.{sh,zsh}]
end_of_line = lf

# Match shfmt
[*.sh]
indent_style = tab
60 changes: 44 additions & 16 deletions docs/_docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: >
Features provided by the GantSign EnV development environment.
numbered_headings: yes
date: 2017-01-18T16:35:52+00:00
modified: 2023-09-01T21:38:26+01:00
modified: 2023-09-17T12:02:25+01:00
---

There are a lot of well known projects, and hidden gems, which aid in your
Expand Down Expand Up @@ -370,29 +370,49 @@ project.
Git aliases save you a lot of typing and make it easy to execute more complex
Git commands:

* Print the main branch (searches for `main`, `trunk`, `mainline`, `default` and `master`)

`git main` = `git-main-branch` (shell script)

* Print the develop branch (searches for `dev`, `devel`, `development` and `develop`)

`git dev` = `git-develop-branch` (shell script)

* Print the remote` HEAD` branch

`git rhead` = `git remote show origin | grep -Po 'HEAD branch: \K.*'`

* Print the repository root directory

`git root` = `git rev-parse --show-toplevel`

* Execute a command in the repository root

`git exec` = `f() { exec "$@"; }; f` (shell function)

* View summary lines for recent commits

`git ls``git log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate`
`git ls``git log --pretty=format:"%C(yellow)%h%Cred%d\ %Creset%s%Cblue\ [%an]" --decorate`

* View summary lines and list changed files for recent commits

`git ll``git log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat`
`git ll``git log --pretty=format:"%C(yellow)%h%Cred%d\ %Creset%s%Cblue\ [%an]" --decorate --numstat`

* View summary lines and dates for recent commits

`git lds``git log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short`
`git lds``git log --pretty=format:"%C(yellow)%h\ %ad%Cred%d\ %Creset%s%Cblue\ [%an]" --decorate --date=short`

* View tree of recent commits (all branches)

`git lt``git log --oneline --graph --decorate --all`

* Checkout an existing branch
* Switch to an existing branch

`git co``git checkout`
`git sw``git switch`

* Create a new branch

`git cb``git checkout -b`
`git cb``git switch -c`

* Amend the last commit and change the commit message

Expand All @@ -402,13 +422,17 @@ Git commands:

`git cane``git commit --amend --no-edit`

* Rebase the current branch onto `origin/master`
* Rebase the current branch onto `origin/main`

`git rom``git rebase origin/master`
`git rom``git fetch -p && git rebase origin/$(git main)`

* Rebase the current branch onto `origin/develop`

`git rod``git rebase origin/develop`
`git rod``git fetch -p && git rebase origin/$(git dev)`

* Rebase the current branch onto `origin/HEAD`

`git roh``!git fetch -p && git rebase origin/$(git rhead)`

* Push the current branch to `origin HEAD`

Expand All @@ -418,25 +442,29 @@ Git commands:

`git pof``git push origin HEAD --force`

* Switch to the `master` branch pull changes and prune remote branches
* Switch to the `main` branch, pull changes and prune remote branches

`git smp``git switch $(git main) && git pull -p`

* Switch to the `develop` branch, pull changes and prune remote branches

`git cmp``git checkout master && git pull -p`
`git sdp``git switch $(git dev) && git pull -p`

* Switch to the `develop` branch pull changes and prune remote branches
* Switch to the local branch with the same name as the remote head, pull changes and prune remote branches

`git cdp``git checkout develop && git pull -p`
`git shp``git fetch -p && git switch $(git rhead) && git pull -p`

* Pop the most recent stash

`git pop``git stash pop`

* List the most recently checked-out branches

`git lb``!git reflog show --pretty=format:'%gs ~ %gd' --date=relative | grep 'checkout:' | grep -oE '[^ ]+ ~ .*' | awk -F~ '!seen[$1]++' | head -n 10 | awk -F' ~ HEAD@{' '{printf(\" \\033[33m%s: \\033[37m %s\\033[0m\\n\", substr($2, 1, length($2)-1), $1)}'`
`git lb``git reflog show --pretty=format:'%gs ~ %gd' --date=relative | grep 'checkout:' | grep -oE '[^ ]+ ~ .*' | awk -F~ '!seen[$1]++' | head -n 10 | awk -F' ~ HEAD@{' '{printf(" \033[33m%s: \033[37m %s\033[0m\n", substr($2, 1, length($2)-1), $1)}'`

* Reformat the recent changes as Markdown release notes

`git release-notes``git log --color --pretty=format:'%s%Cred%d%Creset' --decorate | sed -E 's/(.*) \\((\\#[0-9]+)\\)/* \\2: \\1/' | tac -`
`git release-notes``git log --color --pretty=format:'%s%Cred%d%Creset' --decorate | sed -E 's/(.*) \((\#[0-9]+)\)/* \2: \1/' | tac -`

### Git-GUI

Expand Down
151 changes: 126 additions & 25 deletions provisioning/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,133 @@
tags:
- homebrew

# Install and configure Git version control
- role: weareinteractive.git
# Install Git helper scripts
- role: git_helpers

# Configure Git version control
- role: git_config
become_user: '{{ my_user }}'
git_config:
core.autocrlf: input # Force Linux line endings
diff.algorithm: histogram # For improved diff
diff.indentHeuristic: 'true' # For improved diff
diff.mnemonicPrefix: 'true' # For more readable diff
difftool.prompt: 'false' # Suppress annoying prompt launching difftool
init.defaultBranch: main
log.date: 'format:%a %Y-%m-%d %H:%M:%S %z' # Use ISO format
mergetool.prompt: 'false' # Suppress annoying prompt launching mergetool
push.autoSetupRemote: 'true' # So you can pull the branch later
push.default: simple # Use same branch names for local and remote
rerere.autoUpdate: 'true' # Avoid having to redo a merge
rerere.enabled: 'true' # Avoid having to redo a merge
user.useConfigOnly: 'true' # Don't guess username / email

# utility aliases
alias.dev: '!git-develop-branch'
alias.main: '!git-main-branch'
alias.rhead: >-
!git remote show origin | grep -Po 'HEAD branch: \K.*'
alias.root: 'rev-parse --show-toplevel'
alias.lb: >-
!git reflog show --pretty=format:'%gs ~ %gd' --date=relative
| grep 'checkout:'
| grep -oE '[^ ]+ ~ .*'
| awk -F~ '!seen[$1]++'
| head -n 10
| awk -F' ~ HEAD@{' '{printf(" \033[33m%s: \033[37m %s\033[0m\n", substr($2, 1, length($2)-1), $1)}'
alias.exec: '!f() { exec "$@"; }; f'
# log output
alias.lds: 'log --pretty=format:"%C(yellow)%h\ %ad%Cred%d\ %Creset%s%Cblue\ [%an]" --decorate --date=short'
alias.ll: 'log --pretty=format:"%C(yellow)%h%Cred%d\ %Creset%s%Cblue\ [%an]" --decorate --numstat'
alias.ls: 'log --pretty=format:"%C(yellow)%h%Cred%d\ %Creset%s%Cblue\ [%an]" --decorate'
alias.lt: 'log --oneline --graph --decorate --all'
alias.release-notes: >-
!git log --color --pretty=format:'%s%Cred%d%Creset' --decorate | sed -E 's/(.*) \((\#[0-9]+)\)/* \2: \1/' | tac -
# switching branch
alias.sw: 'switch'
alias.cb: 'switch -c'
alias.sdp: '!git switch $(git dev) && git pull -p'
alias.shp: '!git fetch -p && git switch $(git rhead) && git pull -p'
alias.smp: '!git switch $(git main) && git pull -p'
# amending commits
alias.ca: 'commit --amend'
alias.cane: 'commit --amend --no-edit'
# pushing
alias.po: 'push origin HEAD'
alias.pof: 'push origin HEAD --force'
# rebasing
alias.rod: '!git fetch -p && git rebase origin/$(git dev)'
alias.roh: '!git fetch -p && git rebase origin/$(git rhead)'
alias.rom: '!git fetch -p && git rebase origin/$(git main)'
# other
alias.pop: 'stash pop'

# Configure Git version control for Delta
- role: git_config
become_user: '{{ my_user }}'
git_config:
# Delta configuration
pager.diff: delta
pager.log: delta
pager.reflog: delta
pager.show: delta
delta.line-numbers: 'true'
# Adjust appearance to match bat theme
delta.line-numbers-left-style: '#619DD7'
delta.line-numbers-right-style: '#619DD7'
delta.file-style: white bold
delta.file-decoration-style: white ol
delta.hunk-header-line-number-style: '#619DD7'
delta.hunk-header-decoration-style: '#619DD7 ul'
# Improve contrast between background and foreground colors
delta.minus-style: 'syntax #6A1212'
delta.minus-non-emph-style: 'syntax #481818'
delta.minus-emph-style: 'syntax #6A1212'
delta.line-numbers-minus-style: '#8C1717'
delta.plus-style: 'syntax #4C5631'
delta.plus-non-emph-style: 'syntax #383D28'
delta.plus-emph-style: 'syntax #4C5631'
delta.line-numbers-plus-style: '#7C8C4F'
tags:
- homebrew

# Configure Git version control for GIRT
- role: git_config
become_user: '{{ my_user }}'
git_config:
sequence.editor: interactive-rebase-tool
tags:
- homebrew

# Configure Git version control for Meld
- role: git_config
become_user: '{{ my_user }}'
git_config:
core:
autocrlf: input
alias:
ls: 'log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate'
ll: 'log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat'
lds: 'log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short'
lt: 'log --oneline --graph --decorate --all'
co: 'checkout'
cb: 'checkout -b'
ca: 'commit --amend'
cane: 'commit --amend --no-edit'
rom: 'rebase origin/master'
rod: 'rebase origin/develop'
po: 'push origin HEAD'
pof: 'push origin HEAD --force'
cmp: '!git checkout master && git pull -p'
cdp: '!git checkout develop && git pull -p'
pop: 'stash pop'
# git needs the following commands below to be quoted so we use > to preserve quotes
lb: >
"!git reflog show --pretty=format:'%gs ~ %gd' --date=relative | grep 'checkout:' | grep -oE '[^ ]+ ~ .*' | awk -F~ '!seen[$1]++' | head -n 10 | awk -F' ~ HEAD@{' '{printf(\" \\033[33m%s: \\033[37m %s\\033[0m\\n\", substr($2, 1, length($2)-1), $1)}'"
release-notes: >
"!git log --color --pretty=format:'%s%Cred%d%Creset' --decorate | sed -E 's/(.*) \\((\\#[0-9]+)\\)/* \\2: \\1/' | tac -"
difftool.meld.cmd: 'meld "$LOCAL" "$REMOTE"'
merge.tool: meld
mergetool.meld.cmd: 'meld "$LOCAL" "$MERGED" "$REMOTE"'
tags:
- gui

# Configure Git version control for VS Code
- role: git_config
become_user: '{{ my_user }}'
git_config:
difftool.code.cmd: 'code --reuse-window --wait --diff "$LOCAL" "$REMOTE"'
mergetool.code.cmd: 'code --reuse-window --wait "$MERGED"'
tags:
- gui
- vscode

# Configure Git version control for IntelliJ
- role: git_config
become_user: '{{ my_user }}'
git_config:
difftool.idea.cmd: 'idea diff "$LOCAL" "$REMOTE"'
mergetool.idea.cmd: 'idea merge "$LOCAL" "$REMOTE" "$BASE" "$MERGED"'
tags:
- gui
- intellij

# Configure Git user name and email
- role: gantsign.git_user
Expand Down
2 changes: 0 additions & 2 deletions provisioning/requirements.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
- src: geerlingguy.docker
version: '6.2.0'
- src: weareinteractive.git
version: '1.3.3'
- src: weareinteractive.environment
version: '1.5.0'
- src: geerlingguy.nodejs
Expand Down
2 changes: 2 additions & 0 deletions provisioning/roles/git_config/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
git_config: {}
9 changes: 9 additions & 0 deletions provisioning/roles/git_config/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Configure Git
community.general.git_config:
scope: global
name: '{{ item.key }}'
value: '{{ item.value }}'
loop_control:
label: "{{ item.key }}={{ item.value }}"
loop: '{{ git_config | dict2items }}'
10 changes: 10 additions & 0 deletions provisioning/roles/git_helpers/files/git-develop-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

command git rev-parse --git-dir &>/dev/null || exit
for branch in dev devel development; do
if command git show-ref -q --verify refs/heads/$branch; then
echo $branch
exit
fi
done
echo develop
10 changes: 10 additions & 0 deletions provisioning/roles/git_helpers/files/git-main-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

command git rev-parse --git-dir &>/dev/null || exit
for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk,mainline,default}; do
if command git show-ref -q --verify $ref; then
echo ${ref##*/}
exit
fi
done
echo master
11 changes: 11 additions & 0 deletions provisioning/roles/git_helpers/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Copy Git helper scripts
ansible.builtin.copy:
src: '{{ item }}.sh'
dest: '/usr/local/bin/{{ item }}'
mode: 'u=rwx,go=rx'
owner: root
group: root
loop:
- git-develop-branch
- git-main-branch
1 change: 1 addition & 0 deletions provisioning/roles/more_cli_tools/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- dust
- gh
- git-delta
- git-interactive-rebase-tool
- lsd
- procs
- sd
Expand Down
21 changes: 0 additions & 21 deletions provisioning/roles/zsh/files/custom.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,6 @@ alias -s tar.zsd='tar -I zstd -tf'
alias -s ace='unace l'
alias -s 7z='7z l'

# Git branch helpers
if [ ! -f ~/.local/bin/git-main-branch ]; then
mkdir -p "$HOME/.local/bin"
echo "\
#!/usr/bin/bash
$(which git_main_branch)
git_main_branch" > ~/.local/bin/git-main-branch
chmod 'u=rwx,go=r' ~/.local/bin/git-main-branch
fi
if [ ! -f ~/.local/bin/git-develop-branch ]; then
echo "\
#!/usr/bin/bash
$(which git_develop_branch)
git_develop_branch" > ~/.local/bin/git-develop-branch
chmod 'u=rwx,go=r' ~/.local/bin/git-develop-branch
fi

# Git clone and cd
git-clone-cd() {
local arg
Expand Down

0 comments on commit 464d891

Please sign in to comment.