Skip to content

Instantly share code, notes, and snippets.

@rikonor
Last active January 24, 2025 17:16
Show Gist options
  • Save rikonor/272238ec211674a229cff64c6e1c64e1 to your computer and use it in GitHub Desktop.
Save rikonor/272238ec211674a229cff64c6e1c64e1 to your computer and use it in GitHub Desktop.
Gist Directory

This can be used to create a gist with the contents of a directory, using a custom "ignore" file to filter out unwanted files.

The reason someone might want to do this is if they want to snapshot a directory for future reference but doesn't want to go through the hassle of setting up a full-fledged repository for it.

To use this, in a git repo create your .gistignore file, then run the commands in snapshot.sh.

To download a gist that was created in this way, run the commands in clone.sh.

.gistignore
# rust
target/
Cargo.lock
bindings.rs
# wasm
*.wasm
NAME='<NAME>'
NAME=$(echo $NAME | tr '[:upper:]' '[:lower:]' | tr ' ' '_')
DESCRIPTION=$(cat << EOF
<DESCRIPTION>
EOF
)
# create staging dir
TMP=$(mktemp -d)
# stage files
git ls-files --exclude-from=.gistignore --others | while read -r file; do
cp $file $TMP/$(echo $file | sed 's|/|__|g')
done
# create top-namespace
cat > $TMP/.snapshot__$NAME.md << EOF
# Description
$DESCRIPTION
EOF
# create gist
gh gist create $TMP/.* $TMP/*
# clean up
rm -r $TMP
GIST_ID='272238ec211674a229cff64c6e1c64e1'
# clone gist
gh gist clone $GIST_ID
# unstage files
for file in $GIST_ID/*; do
p=$(echo $file | sed 's|__|\/|g')
mkdir -p $(dirname $p)
mv $file $p
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment