Skip to content

Instantly share code, notes, and snippets.

@peabnuts123
Last active September 30, 2018 19:49
Show Gist options
  • Save peabnuts123/d7471906bc21ea4c19dc01a8001d2eeb to your computer and use it in GitHub Desktop.
Save peabnuts123/d7471906bc21ea4c19dc01a8001d2eeb to your computer and use it in GitHub Desktop.
A small collection of tools for using a git workflow in collaboration with others.

Git Utility Scripts

This is a small collection of utility scripts i've written to aid in a git workflow, collaborating with others on a project. I would recommend putting them somewhere in $PATH so you can just call smart-rebase, etc. from CLI. Happy gitting 👍

#!/bin/bash
#
# Detect Conflicts
# Author: Jeff (peabnuts123)
# Purpose: Compare current working changes to changes between remote and HEAD
# and identify any files that both you and the remote have changed since that
# commit. These files may potentially cause merge conflicts when rebasing/merging.
#
# Usage: detect-conflicts remote [branch]
# remote - the remote to use when comparing against
# branch - remote branch name to use when comparing against.
# If no branch is specified, the current branch is used
#
# Get remote arg
remote=$1
if [ -z "$1" ]
then
echo "No remote specified."
echo "Usage: smart-rebase \$remote \$branch"
exit 1
fi
# Get branch arg
branch=$2
if [ -z "$2" ]
then
echo "No branch specified. Using current git branch"
branch=$(git rev-parse --abbrev-ref HEAD)
fi
# Update remote branches locally
git fetch $remote
# Find which files are common between local git diff and git diff between HEAD and remote branch
conflictFiles=$({ git diff HEAD $remote/$branch --name-only & git diff --name-only; } | sort | uniq -d)
# Count how many files are common
numConflicts=$(echo -n "$conflictFiles" | wc -l)
# If no files are common: display message, exit safely
if (( numConflicts == 0 ))
then
echo "✅ No changed files in common with remote"
exit 0
fi
# Otherwise: display message, display all files that may conflict
echo "❌ $numConflicts files changed both locally and remotely."
echo "The following files may cause merge conflicts: "
echo
for file in $conflictFiles
do
echo "|- $file"
done
#!/bin/bash
#
# "Smart" Rebase
# Author: Jeff (peabnuts123)
# Purpose: Perform a rebase against remote/branch in a safe manner, allowing
# for the working tree to be dirty by using `--autostash` and preserving merge commits
# by using `-p`. This is slightly more verbose than something like `git pull` and
# is therefor more convenient to wrap into a small script.
#
# Usage: smart-rebase remote [branch]
# remote - the remote to use when comparing against
# branch - remote branch name to use when comparing against.
# If no branch is specified, the current branch is used
#
# Get remote arg
remote=$1
if [ -z "$1" ]
then
echo "No remote specified."
echo "Usage: smart-rebase \$remote \$branch"
exit 1
fi
# Get branch arg
branch=$2
if [ -z "$2" ]
then
echo "No branch specified. Using current git branch"
branch=$(git rev-parse --abbrev-ref HEAD)
fi
# Update remote branches locally
git fetch $remote
# Perform rebase specifying `--autostash` and `--preserve-merges`
git rebase --autostash --preserve-merges $remote/$branch
#!/bin/bash
#
# "Smart" Rebase
# Author: Jeff (peabnuts123)
# Purpose: Perform a rebase against remote/branch in a safe manner, allowing
# for the working tree to be dirty by using `--autostash` and preserving merge commits
# by using `-p`. This is slightly more verbose than something like `git pull` and
# is therefor more convenient to wrap into a small script.
#
# Usage: smart-rebase remote [branch]
# remote - the remote to use when comparing against
# branch - remote branch name to use when comparing against.
# If no branch is specified, the current branch is used
#
# Get remote arg
remote=$1
if [ -z "$1" ]
then
echo "No remote specified."
echo "Usage: smart-rebase \$remote \$branch"
exit 1
fi
# Get branch arg
branch=$2
if [ -z "$2" ]
then
echo "No branch specified. Using current git branch"
branch=$(git rev-parse --abbrev-ref HEAD)
fi
# Update remote branches locally
git fetch $remote
# Perform rebase specifying `--autostash` and `--preserve-merges`
git rebase --autostash --preserve-merges $remote/$branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment