Skip to content

Instantly share code, notes, and snippets.

View samredai's full-sized avatar

Samuel Redai samredai

View GitHub Profile
@samredai
samredai / md5sum_diff.sh
Created February 28, 2020 21:40
Linux: Check if the MD5 hashes are the same between two files
#!/usr/bin/env bash
# Usage: ./md5sum.sh file1 file2
if [ "$#" -ne 2 ]; then
echo -e "Too many arguments,\n\nUsage:\n$ ./check.sh file1 file2"
fi
validate_files() {
status=0
@samredai
samredai / recover.sh
Created January 16, 2020 14:40
Git: Recover Uncommitted Files Removed From git rm
git prune -n
# Returns a list of hashes
git cat-file -p HASH1 > recovered_file1.txt
git cat-file -p HASH2 > recovered_file2.txt
git cat-file -p HASH3 > recovered_file3.txt
@samredai
samredai / move_file_with_history.sh
Created January 9, 2020 00:43
Git: Copy One File From One Repo To Another Repo While Preserving Commit History [Alternative Solution For Large Repositories]
# For this example, assume REPO_A has one file that you want to move into REPO_B along with it's commit history
# Clone REPO_A
git clone REPO_A
# Extract only the commit hashes for your target file and place them in a text file
# See this gist for more info on this line: https://gist.github.com/samsetegne/7768efe9394714c7fb5085de14f03516
git log -p path/to/target/file/filename.py | grep -n "^commit" | grep -Eo "[^ ]+$" > ~/commit_hashes.txt
# Clone REPO_B
@samredai
samredai / single_file_commit_hashes.sh
Last active January 9, 2020 00:40
Git: Print All Commit Hashes For a Single File
# The one-liner below does the following:
# - 1) Outputs a git log for a specific file
# - 2) Filters to all lines that start with commit
# - 3) Filters each line to only the commit hash at the end of the line
git log -p path/to/target/file/filename.py | grep -n "^commit" | grep -Eo "[^ ]+$"
##########
# Extras #
##########
@samredai
samredai / move_file_with_history.sh
Created January 8, 2020 22:54
Git: Copy One or More Files From One Repo To Another Repo While Preserving Commit History (note: very slow for extremely large repos)
# For this example, assume REPO_A has one or more files that you want to move into REPO_B along with their commit history
# Clone REPO_A
git clone REPO_A
# Remove all remotes
git remote | xargs -n1 git remote remove
# List all of the files (using their filepath from root) that you plan to move to REPO_B
FILES='filename1 filename2 folder/subfolder/filename3'
@samredai
samredai / random_incoherent_sentence.py
Created November 25, 2019 19:59
Python: Generate a random, and probably incoherent, sentence
import random
# http://www.desiquintans.com/downloads/nounlist/nounlist.txt
nouns = open('nouns.txt').read().split('\n')
# https://raw.githubusercontent.com/janester/mad_libs/master/List%20of%20Proper%20Nouns.txt
proper_nouns = open('propernouns.txt').read().split('\n')
# https://raw.githubusercontent.com/aaronbassett/Pass-phrase/master/verbs.txt
verbs = open('verbs.txt').read().split('\n')
@samredai
samredai / remove_latest_commit_in_github.sh
Last active January 24, 2020 18:30
Git: Remove the latest commit in GitHub
git push -f origin HEAD^:master
# Note that you if you just want to remove a commit locally and...
# ...keep the change as uncommited modifications to the local files, use the line below:
git reset --soft HEAD~1
@samredai
samredai / list_all_subfolders_and_files.py
Created November 21, 2019 18:25
Python: Recursively list all subfolders and files in a directory
import os
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
@samredai
samredai / compgen.sh
Created November 7, 2019 18:50
Linux: compgen tool to list all available commands
compgen -a # All available aliases
compgen -b # All available built-in commands
compgen -c # All available commands
compgen -k # All available keywords
compgen -A function # All available functions
compgen -A function -abck # Everything available that you can run
@samredai
samredai / fix_mysqlclient.sh
Last active September 1, 2019 06:24
Python: Fix 'OSError: mysql_config not found' When Pip Installing mysqlclient
# Worked on Linux Mint 19.1 (Tessa)
sudo apt-get install python3-dev
sudo apt-get install libmysqlclient-dev
sudo apt-get install libssl-dev