Skip to content

Instantly share code, notes, and snippets.

@Turysaz
Last active March 27, 2023 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Turysaz/4331163d4cbece6526047109b9cedf08 to your computer and use it in GitHub Desktop.
Save Turysaz/4331163d4cbece6526047109b9cedf08 to your computer and use it in GitHub Desktop.
Easy git merge conflict resolving
#!/bin/bash
# Git conflict resolving helper
# Show a numbered list of all conflicts with `conflicts-list`.
# Edit any of the conflicting files by `conflicts-edit <index-number>`.
# resolve init
__conflicts-init() {
# read the names of all unmerged files into the array _CONFLICTS
readarray -t _CONFLICTS < <(git diff --name-only --diff-filter=U)
}
# list all conflicts
conflicts-list() {
__conflicts-init
max=${#_CONFLICTS[@]}
if [[ $max -le 0 ]]
then
echo "No conflicting files known."
return
fi
for i in `seq 0 $(($max - 1))`;
do
printf '%i: %s\n' $i ${_CONFLICTS[$i]}
done
}
# open file with conflicts
conflicts-edit() {
re='^[0-9]+$'
__conflicts-init
if [[ ${#_CONFLICTS[@]} -le 0 ]]
then
echo "No conflicting files known."
return
fi
if [[ -z $1 ]]
then
echo "Please specify the file's index."
echo "(Run 'conflicts-list' to get a list.)"
return
fi
if [[ ! $1 =~ $re || $1 -ge ${#_CONFLICTS[@]} || $1 -lt 0 ]]
then
echo "Invalid index: '$1'"
return
fi
vim "${_CONFLICTS[$1]}"
}
# open file with conflicts
conflicts-add() {
re='^[0-9]+$'
__conflicts-init
if [[ ${#_CONFLICTS[@]} -le 0 ]]
then
echo "No conflicting files known."
return
fi
if [[ -z $1 ]]
then
echo "Please specify the file's index."
echo "(Run 'conflicts-list' to get a list.)"
return
fi
if [[ ! $1 =~ $re || $1 -ge ${#_CONFLICTS[@]} || $1 -lt 0 ]]
then
echo "Invalid index: '$1'"
return
fi
read -p "Stage '${_CONFLICTS[$1]}'? [yN]" _answer
[[ $_answer != "y" ]] && return
git add "${_CONFLICTS[$1]}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment