Logo
AI Engineer at

GitHub Setup and Encryption Guide

April 18th, 2025 - Tags: Python, Git, GitHub, and git-crypt

1 🌟 GitHub Setup and Encryption Guide 🌟

Welcome to this friendly guide! πŸŽ‰ We’ll walk you through setting up Git, configuring GitHub credentials, managing repositories, and securing files with git-crypt in a Linux environment. Let’s make your GitHub workflow smooth and secure! πŸš€

1.1 πŸ› οΈ Install Dependencies on Linux

First, let’s install the tools we need to manage repositories and encrypt files. Run these commands to get git, git-lfs, git-crypt, and gnupg ready! πŸ”§

apt-get install git git-lfs
git lfs install
curl -s https://packagecloud.io/install/repositories/github/git-crypt/script.deb.sh | bash
apt-get install git-crypt
sudo apt-get install gnupg

1.2 πŸ‘€ Configure Username and Email

Set up your Git global configuration with your GitHub username, email, and personal access token. This ensures GitHub knows it’s you! 😊

username = "your_github_username"
email = "your_github_username_email"
token = "your_github_token"
git config --global user.name "your_github_username"
git config --global user.email "your_github_username_email"
git config --global credential.helper store
git config --global url.https://your_github_token@github.com/.insteadOf "https://github.com/"
git config --global core.editor "nano"

2 πŸ“€ Upload to GitHub

2.1 πŸ“‚ Clone an Existing Repository

Got an existing repo? Let’s clone it and make your first commit! πŸ—‚οΈ

repo_name = "your_github_repo"
git clone https://github.com/your_github_username/your_github_repo.git
cd your_github_repo
echo "# your_github_repo" >> README.md
git add README.md
git commit -m "first commit"
git push -u origin main

2.2 ✨ Create a New Repository

Want to start fresh? Create a new GitHub repository using the GitHub API and set it up locally. 🌱

import requests

repo_name = "new-repo-from-colab"
url = "https://api.github.com/user/repos"
headers = {
    'Authorization': f'token {token}',
    'Content-Type': 'application/json'
}
data = {
    'name': repo_name,
    'description': 'A repository created from Google Colab',
    'private': False
}

response = requests.post(url, headers=headers, json=data)
if response.status_code == 201:
    print(f"Repository '{repo_name}' created successfully.")
else:
    print(f"Failed to create repository. Status code: {response.status_code}")
    print(response.text)

Now, initialize the local repository and push it to GitHub! πŸš€

mkdir new-repo-from-colab
cd new-repo-from-colab
git init
git branch -M main
import subprocess

remote_url = f"https://{token}@github.com/{username}/{repo_name}.git"
subprocess.run(['git', 'remote', 'add', 'origin', remote_url], check=True, capture_output=True, text=True)
echo -e "# H1\n## H2\n### H3\n- a\n- b" >> README.md
git add .
git commit -m "my commit"
git push -u origin main

3 πŸ”’ Encrypt Files or Folders Using git-crypt

Keep your sensitive files safe with git-crypt! Let’s see how to encrypt files and folders. πŸ›‘οΈ

3.1 πŸ”‘ Initialize git-crypt

Set up git-crypt in your repository to enable encryption.

git-crypt init

3.2 πŸ“œ Encrypt Individual Files

Encrypt a specific file, like abc.txt, so only authorized users can read it. πŸ”

echo "this is abc.txt, which has been encrypt" >> abc.txt
echo "abc.txt filter=git-crypt diff=git-crypt" > .gitattributes
git add .gitattributes
git commit -m "Add git-crypt configuration for abc.txt"
git add .
git commit -m "Add encrypted abc.txt"
git push -u origin main

3.3 πŸ“ Encrypt Folders

Encrypt an entire folder, such as do-not-touch, and all its contents. πŸ—‚οΈπŸ”’

mkdir do-not-touch
mkdir do-not-touch/abc-f
echo "this is abc.txt, which has been encrypt" >> do-not-touch/abc.txt
echo "hello 2" >> do-not-touch/abc-f/nhb.txt
echo -e "abc.txt filter=git-crypt diff=git-crypt\ndo-not-touch/** filter=git-crypt diff=git-crypt" >> .gitattributes
git-crypt status -f
git add .gitattributes
git add .
git commit -m "Add git-crypt configuration for abc.txt"
git push -u origin main

4 βœ… Verify Encryption and Export Key

Check which files are encrypted and export the git-crypt key to share with collaborators or back up securely. πŸ”

git-crypt status
git-crypt export-key "your_key_file_location"

5 πŸ”“ Decrypt Files

Clone the encrypted repository and unlock the files using the exported key. πŸ—οΈ

sudo apt-get install git-crypt gnupg
git clone https://github.com/your_github_username/your_github_repo.git
cd your_github_repo
gpg --import "your_key_file_location"
git-crypt status
git-crypt unlock "your_key_file_location"

6 πŸ“ Helpful Tips

Happy coding and stay secure! πŸŽ‰