Skip to content

Instantly share code, notes, and snippets.

@np
Created May 7, 2012 05:42
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 np/2626131 to your computer and use it in GitHub Desktop.
Save np/2626131 to your computer and use it in GitHub Desktop.
Move files to a trash branch
#!/bin/bash
set -e
error(){
echo "$@" >>/dev/stderr
exit 1
}
GIT_TRASH_FILE=.git/trash.idx
remotes=(origin)
files=("$@")
[ -e .git ] || error ".git does not exists"
[ ! -e "$GIT_TRASH_FILE" ] ||
{ cat <<EOF
$GIT_TRASH_FILE already exists. Check that not other instance of git-trash is
running. It might also be the case that this file is used by another program
or that git-trash died without cleaning this file away.
EOF
exit 1
}
intrash(){
GIT_INDEX_FILE="$GIT_TRASH_FILE" "$@"
}
# Create an empty branch called 'trash' (no files except one called README.git-trash)
# if no such branch exists.
if git rev-parse -q --verify refs/heads/trash; then
:
else
for r in "${remotes[@]}"; do
if git rev-parse -q --verify "$r"/trash; then
git update-ref refs/heads/trash "$r"/trash
break
fi
done
if git rev-parse -q --verify refs/heads/trash; then
:
else
# Creating a new trash branch.
intrash git read-tree --empty
tree=$(intrash git write-tree)
head=$(echo "Init trash" | intrash git commit-tree "$tree")
git update-ref refs/heads/trash "$head"
fi
fi
msg='Put some files to the trash branch'
intrash git read-tree refs/heads/trash
intrash git add "${files[@]}"
tree=$(intrash git write-tree)
head=$(echo "$msg" | intrash git commit-tree "$tree" -p refs/heads/trash)
rm $GIT_TRASH_FILE
git update-ref refs/heads/trash "$head"
git rm "${files[@]}"
git commit -m"$msg" "${files[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment