Skip to content

Instantly share code, notes, and snippets.

@jarkkosyrjala
Created December 2, 2024 11:27
Show Gist options
  • Save jarkkosyrjala/ab7f0fa5dbc2ec1fcce9481f29a1b33d to your computer and use it in GitHub Desktop.
Save jarkkosyrjala/ab7f0fa5dbc2ec1fcce9481f29a1b33d to your computer and use it in GitHub Desktop.
Git Commit Move with Clipboard Trick: This Bash script automates a Git workflow for moving a file and committing the changes. It handles scenarios where a modified copy exists and the original file should be deleted. The script leverages the system clipboard to preserve the original content of the new file during the move.
#!/bin/bash
old_file="$1"
new_file="$2"
# Check if both arguments are provided
if [ $# -ne 2 ]; then
echo "Usage: $0 old_file new_file"
exit 1
fi
if [ ! -f "$old_file" ]; then
echo "Error: $old_file does not exist."
return 1
fi
if [ ! -f "$new_file" ]; then
echo "Error: $new_file does not exist."
return 1
fi
# Copy the contents of the new file to the clipboard
pbcopy < "$new_file"
# Force move the old file to the new file
mv -f "$old_file" "$new_file"
# Paste the clipboard contents into the new file
pbpaste > "$new_file"
# Stage the changes
git add "$old_file" "$new_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment