├── git-privatize └── README.md /git-privatize: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # A script for batch privatization of GitHub repositories 3 | 4 | # Get user's GitHub username 5 | # Ask for confirmation 6 | confirmation="n" 7 | while [ "$confirmation" != "${confirmation#[Nn]}" ];do 8 | printf "\nEnter your GitHub username: " 9 | read username 10 | printf "\nYou entered: $username" 11 | printf "\nIs this correct (y/n)? " 12 | read confirmation 13 | done 14 | 15 | # Get root folder's path 16 | parent_path="${PWD}" 17 | cd "$parent_path" 18 | 19 | # Get total number of repos to privatize 20 | total_repos=$(find $parent_path -type d -maxdepth 1 | wc -l) 21 | 22 | # Bold/normal font formatting helpers 23 | bold=$(tput bold) 24 | normal=$(tput sgr0) 25 | 26 | # Initialize count 27 | count=1 28 | # For each file in the current directory... 29 | for file in *; do 30 | # If the file is a directory... 31 | if [ -d "$file" ]; then 32 | # cd into the directory 33 | cd "$file" 34 | 35 | message="Starting privatizing repo $count of $total_repos..." 36 | printf "\n${bold}$message${normal}\n" 37 | 38 | message="Deleting remote repo $file on GitHub..." 39 | printf "\n${bold}$message${normal}\n" 40 | hub delete -y $file # hub command to delete the directory, -y flag negates asking for confirmation 41 | 42 | message="Creating new private GitHub repo with name $file..." 43 | printf "\n${bold}$message${normal}\n" 44 | hub create -p $file # hub command to create a new remote repo, -p flag sets the repo to private 45 | 46 | message="Setting this local repo's remote URL to the recently created private repo's URL..." 47 | printf "\n${bold}$message${normal}\n" 48 | git remote set-url origin git@github.com:$username/$file.git # git command to set the remote url 49 | printf "Set new remote url.\n" 50 | 51 | message="Pushing to GitHub..." 52 | printf "\n${bold}$message${normal}\n" 53 | git push 54 | 55 | message="Finished privatizing repo $count of $total_repos" 56 | printf "\n${bold}$message${normal}\n" 57 | 58 | # Increment the count 59 | ((count++)) 60 | 61 | # cd back to the root folder 62 | cd .. 63 | fi 64 | done 65 | 66 | message="All done 🤗 🤗 🤗" 67 | printf "\n${bold}$message${normal}\n" 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-privatize 2 | A bash script for bulk privatization of GitHub repositories 3 | 4 | ## Why does this exist? 5 | GitHub doesn't allow you to make multiple repositories private at one time through their website. Their web app forces you to navigate to each repository's settings page and type in the repository name to make it private. 6 | 7 | ![Alt Text](https://media.giphy.com/media/bWM2eWYfN3r20/giphy.gif) 8 | 9 | ## How does it work? 10 | The script is very simple. The script... 11 | 12 | 1. Assumes all the repositories you want to make private are in 1 root folder. 13 | 2. `cd`s into each folder. 14 | 3. Deletes the remote repository on GitHub. Your local repository will remain intact. 15 | 4. Creates a new remote repository on GitHub using the same name as the local repository. **The new repository is automatically set to private.** 16 | 5. Sets the local repository's remote URL to the new repository's remote URL. 17 | 6. Pushes the local repository's contents to GitHub. 18 | 7. `cd`s back to the root folder. 19 | 8. Repeats steps 2-7 for all remaining repositories in the root folder. 20 | 21 | ## Getting Started 22 | Follow these instructions: 23 | 24 | ### 1. Install Hub 25 | You need `Hub` to run special git commands from the command line. 26 | 27 | If you have `homebrew`, you can install by running this command: 28 | 29 | ``` 30 | $ brew install hub 31 | $ hub version 32 | git version 1.7.6 33 | hub version 2.2.3 34 | ``` 35 | 36 | Otherwise, please follow [Hub's Official Installation Instructions](https://github.com/github/hub) 37 | 38 | ### 2. Enable the 'delete_repo' scope 39 | 1. Navigate to [github.com/settings/tokens](https://github.com/settings/tokens) 40 | 2. Find the token for `hub` 41 | 3. Check the box for "delete_repo" 42 | 4. Confirm the change by clicking the "Update token" button at the bottom 43 | 44 | ### 3. Set up SSH agent 45 | If GitHub currently asks you to enter your username and password everytime you push or pull, you're not using SSH agent. 46 | 47 | As [this tutorial](https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) states: 48 | > If you don't want to reenter your passphrase every time you use your SSH key, you can add your key to the SSH agent, which manages your SSH keys and remembers your passphrase. 49 | 50 | Since we're going to be doing git commands in bulk, I highly recommend you set this up, or else Git may ask you for your credentials repeatedly. 51 | 52 | Please follow [the directions to set up SSH agent](https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) carefully. 53 | 54 | ### 4. Check that SSH agent is working 55 | Try push/pulling and see if GitHub asks you for your credentials. 56 | 57 | ### 5. Move the repositories you want to make private into a single root folder 58 | The folder name does not matter. 59 | 60 | ### 6. Install git-privatize 61 | 62 | One-line installation with `curl` ⬇️. Enter your computer password if prompted. 63 | 64 | ```bash 65 | curl -L -O https://raw.githubusercontent.com/ethannam/git-privatize/master/git-privatize && sudo mv git-privatize /usr/local/bin/ && sudo chmod +x /usr/local/bin/git-privatize 66 | ``` 67 | 68 | If you prefer to clone down this repo, make sure you move the git-privatize executable to your /usr/local/bin/ folder and set its +x permission. You can do that by running this line: 69 | 70 | ``` 71 | sudo mv git-privatize /usr/local/bin/ && sudo chmod +x /usr/local/bin/git-privatize 72 | ``` 73 | 74 | ## Usage Example 75 | `cd` into the root folder that contains the repositories you want to make private. 76 | 77 | For example: 78 | ``` 79 | $ pwd 80 | /Users/ethannam/workspace 81 | $ cd root-folder 82 | $ pwd 83 | /Users/ethannam/workspace/root-folder 84 | ``` 85 | 86 | From this root folder, if you run the `ls` command, you should see a list of the repos to make private. 87 | 88 | ``` 89 | $ ls -1d */ 90 | repo-name-1/ 91 | repo-name-2/ 92 | repo-name-3/ 93 | repo-name-4/ 94 | repo-name-5/ 95 | ``` 96 | 97 | From this root folder, run the command below. **You do not have to `cd` into each repo individually. The command below takes care of that for you.** 98 | ``` 99 | git privatize 100 | ``` 101 | 102 | ## Enjoy! 103 | ![Alt Text](https://media.giphy.com/media/6brH8dM3zeMyA/giphy.gif) 104 | --------------------------------------------------------------------------------