Skip to content

Instantly share code, notes, and snippets.

@dkubb
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save dkubb/fbb28209884ce70de1cc to your computer and use it in GitHub Desktop.

Select an option

Save dkubb/fbb28209884ce70de1cc to your computer and use it in GitHub Desktop.
Git tools
#!/usr/bin/env bash
# Prune all merged local and remote branches
set -euo pipefail
IFS=$'\n\t'
branch=$(git rev-parse --abbrev-ref HEAD)
remote=$(git config "branch.${branch}.remote")
git remote update > /dev/null
git remote prune "$remote"
git branch --remotes --merged "$remote/$branch" \
| awk -F\"/\" "!/(>|$branch)/ {print \$2}" \
| xargs --no-run-if-empty --max-lines 1 git push "$remote" --delete
#!/usr/bin/env bash
# Push commits individually to trigger CI builds for each
set -euo pipefail
IFS=$'\n\t'
parent=${1-master}
branch=$(git rev-parse --abbrev-ref HEAD)
remote=$(git config "branch.${branch}.remote")
# Commits in the local branch not in the remote branch
commits=$(
comm -13 \
<(git rev-list "$remote/$parent..$remote/$branch" --reverse) \
<(git rev-list "$parent..$branch" --reverse)
)
# Push all commits not in the parent branch
for commit in $commits
do
# Trigger CI for each commit by pushing them individually
git push --force "$remote" "$commit:$branch"
done
#!/usr/bin/env bash
# Rebase a branch on a parent branch, preserving commit metadata
set -euo pipefail
IFS=$'\n\t'
parent=${1-master}
# The command to execute for each commit
read -r -d '' command <<-'COMMAND' || true
export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
COMMAND
# Rebase the current branch on top of the parent
git rebase $parent
# Rewrite all commits against the current commit
git filter-branch --force --env-filter "$command" -- "$parent..HEAD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment