Last active
October 13, 2021 00:02
-
-
Save floscr/6981425 to your computer and use it in GitHub Desktop.
Create folder from git branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Git branches to folders | |
function branch_folders { | |
# Check if folder is a git repository | |
if [[ ! -d ".git" ]]; then | |
echo -e "\n\tThis is not a git repository!\n" | |
exit | |
fi | |
# Folder where the branch folders will be stored inside | |
# Don't put it in your repository folder or else git will commit those files again ;) | |
folder="../branchfolders" | |
# Check if there is a folder named "branchfolders" | |
# If not, create one | |
if [[ ! -d "${folder}" ]]; then | |
mkdir "${folder}" | |
fi | |
# List of branches without the asterisk and spaces | |
branches=( $(git branch | cut -c 2-) ) | |
# List through branches | |
for (( i = 0; i < ${#branches[@]}; i++ )); do | |
git checkout -q ${branches[$i]} | |
if [[ ! -d "${folder}/${branches[$i]}" ]]; then | |
mkdir "${folder}/${branches[$i]}" | |
fi | |
rsync -a --exclude=.git --exclude=.gitignore . "${folder}/${branches[$i]}" | |
echo -e "\tBranch '${branches[$i]}' copied!" | |
done | |
} | |
branch_folders |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment