Skip to content

Instantly share code, notes, and snippets.

@AlbertProfe
Last active November 8, 2024 12:31
Show Gist options
  • Save AlbertProfe/262beaa9b9965df80241049923a77dee to your computer and use it in GitHub Desktop.
Save AlbertProfe/262beaa9b9965df80241049923a77dee to your computer and use it in GitHub Desktop.
Create local/remote repo test
#!/bin/bash
# Function to log operations
log_operation() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Function to log operations
log_operation() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Default Git configuration
default_username="albertprofe"
default_email="albert@gmail.com"
log_operation "Starting repository setup script"
# Ask user if they want to change the default Git configuration
echo "Default Git username is '$default_username'. Do you want to change it? (y/n)"
read change_username
if [[ $change_username == "y" || $change_username == "Y" ]]; then
echo "Enter your Git username:"
read git_username
log_operation "Git username changed to $git_username"
else
git_username=$default_username
log_operation "Using default Git username: $git_username"
fi
echo "Default Git email is '$default_email'. Do you want to change it? (y/n)"
read change_email
if [[ $change_email == "y" || $change_email == "Y" ]]; then
echo "Enter your Git email:"
read git_email
log_operation "Git email changed to $git_email"
else
git_email=$default_email
log_operation "Using default Git email: $git_email"
fi
# Ask user for local/remote folder/repo name
echo "Enter the name for your local and remote repository:"
read repo_name
log_operation "Repository name set to: $repo_name"
# Create local directory and initialize git
mkdir $repo_name
cd $repo_name
git init
log_operation "Local directory created and Git initialized in $repo_name"
# Set up user and email
git config user.name "$git_username"
git config user.email "$git_email"
log_operation "Git user.name and user.email configured"
# Create remote GitHub repository
gh repo create $repo_name --public
log_operation "Remote GitHub repository created: $repo_name"
# Add remote origin
remote_url="https://github.com/$git_username/$repo_name.git"
git remote add origin $remote_url
log_operation "Remote origin added: $remote_url"
# Create initial files
echo "Lorem ipsum dolor sit amet" > file1.txt
echo "Lorem ipsum dolor sit amet" >> file2.txt
echo "Lorem ipsum dolor sit amet" > file3.txt
log_operation "Created initial files: file1.txt, file2.txt, file3.txt"
# Commit and push initial files
git add .
git commit -m "Initial commit with three files"
git push -u origin master
log_operation "Initial files committed and pushed to master branch"
# Create /data directory
mkdir -p data
log_operation "Created /data directory"
# Create 3 more files in /data
echo "Consectetur adipiscing elit" > data/file4.txt
echo "Sed do eiusmod tempor incididunt" >> data/file5.txt
echo "Ut labore et dolore magna aliqua" > data/file6.txt
log_operation "Created additional files in /data: file4.txt, file5.txt, file6.txt"
# Commit and push new files
git add .
git commit -m "Add three more files in /data directory"
git push origin master
log_operation "Additional files in /data committed and pushed to master branch"
# Create and switch to a new branch named test-branch
git checkout -b test-branch
log_operation "Created and switched to new branch: test-branch"
# Create 3 text files in test-branch
echo "Test file 1 content" > test1.txt
echo "Test file 2 content" > test2.txt
echo "Test file 3 content" > test3.txt
log_operation "Created 3 new text files in test-branch"
# Commit and push the new files in test-branch
git add .
git commit -m "Add three test files in test-branch"
git push origin test-branch
log_operation "Committed and pushed 3 new files to test-branch"
# Switch back to master branch
git checkout master
log_operation "Switched back to master branch"
# Create 4 commits with additional content to all files
for i in {1..4}
do
echo "New content for commit $i" >> file1.txt
echo "New content for commit $i" >> file2.txt
echo "New content for commit $i" >> file3.txt
echo "New content for commit $i" >> data/file4.txt
echo "New content for commit $i" >> data/file5.txt
echo "New content for commit $i" >> data/file6.txt
git add .
git commit -m "Commit $i: Add new content to all files"
log_operation "Created local commit $i with new content in all files"
done
echo "Repository setup and additional operations complete!"
log_operation "Repository setup and additional operations completed successfully"
# Ask user to press any key to exit
read -n 1 -s -r -p "Press any key to exit..."
echo ""
log_operation "Script execution ended"
#!/bin/bash
# Function to log operations
log_operation() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Default Git configuration
default_username="albertprofe"
default_email="albert@gmail.com"
log_operation "Starting repository setup script"
# Ask user if they want to change the default Git configuration
echo "Default Git username is '$default_username'. Do you want to change it? (y/n)"
read change_username
if [[ $change_username == "y" || $change_username == "Y" ]]; then
echo "Enter your Git username:"
read git_username
log_operation "Git username changed to $git_username"
else
git_username=$default_username
log_operation "Using default Git username: $git_username"
fi
echo "Default Git email is '$default_email'. Do you want to change it? (y/n)"
read change_email
if [[ $change_email == "y" || $change_email == "Y" ]]; then
echo "Enter your Git email:"
read git_email
log_operation "Git email changed to $git_email"
else
git_email=$default_email
log_operation "Using default Git email: $git_email"
fi
# Ask user for local/remote folder/repo name
echo "Enter the name for your local and remote repository:"
read repo_name
log_operation "Repository name set to: $repo_name"
# Create local directory and initialize git
mkdir $repo_name
cd $repo_name
git init
log_operation "Local directory created and Git initialized in $repo_name"
# Set up user and email
git config user.name "$git_username"
git config user.email "$git_email"
log_operation "Git user.name and user.email configured"
# Create remote GitHub repository
gh repo create $repo_name --public
log_operation "Remote GitHub repository created: $repo_name"
# Add remote origin
remote_url="https://github.com/$git_username/$repo_name.git"
git remote add origin $remote_url
log_operation "Remote origin added: $remote_url"
# Create initial files
echo "Lorem ipsum dolor sit amet" > file1.txt
echo "Lorem ipsum dolor sit amet" >> file2.txt
echo "Lorem ipsum dolor sit amet" > file3.txt
log_operation "Created initial files: file1.txt, file2.txt, file3.txt"
# Commit and push initial files
git add .
git commit -m "Initial commit with three files"
git push -u origin master
log_operation "Initial files committed and pushed to master branch"
# Create /data directory
mkdir -p data
log_operation "Created /data directory"
# Create 3 more files in /data
echo "Consectetur adipiscing elit" > data/file4.txt
echo "Sed do eiusmod tempor incididunt" >> data/file5.txt
echo "Ut labore et dolore magna aliqua" > data/file6.txt
log_operation "Created additional files in /data: file4.txt, file5.txt, file6.txt"
# Commit and push new files
git add .
git commit -m "Add three more files"
git push origin master
log_operation "Additional files committed and pushed to master branch"
echo "Repository setup complete!"
log_operation "Repository setup completed successfully"
# Ask user to press any key to exit
read -n 1 -s -r -p "Press any key to exit..."
echo ""
log_operation "Script execution ended"

Git Repository Management Scripts

Script Summaries

1. create-git-pra.sh

  • Creates a new local and remote Git repository
  • Configures Git user name and email
  • Initializes the repository with initial files
  • Creates a data directory with additional files
  • Commits and pushes changes to GitHub

2. create-git-pra-branch.sh

  • Performs all actions from create-git-pra.sh
  • Creates a new branch called 'test-branch'
  • Adds test files to the new branch
  • Switches back to master and creates multiple commits

3. remove-repo.sh

  • Deletes both local and remote Git repositories
  • Checks for existence of local and remote repositories
  • Logs all operations during the deletion process

4. remove-repo-revoke.sh

  • Deletes both local and remote Git repositories
  • Checks for existence of local and remote repositories
  • Logs all operations during the deletion process
  • Grant rigths to remove repo and then revoke

Prerequisites for Execution

To run these scripts, you need:

  1. Git installed on your system
  2. GitHub CLI (gh) installed
  3. A GitHub account
  4. GitHub CLI authenticated with your account (gh auth login)
  5. Proper permissions to delete repositories

Before running the remove-repo.sh script, you need to grant the delete_repo scope:

gh auth refresh -h github.com -s delete_repo
#!/bin/bash
# Function to log operations
log_operation() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Ask user for the repository name
echo "Enter the name of the repository you want to delete:"
read repo_name
log_operation "Starting deletion process for repository: $repo_name"
# Check if local repository exists
if [ -d "$repo_name" ]; then
log_operation "Local repository found: $repo_name"
# Delete local repository
rm -rf "$repo_name"
if [ $? -eq 0 ]; then
log_operation "Local repository deleted successfully"
else
log_operation "Error deleting local repository"
fi
else
log_operation "Local repository not found: $repo_name"
fi
# Check if GitHub CLI is installed
if ! command -v gh &> /dev/null; then
log_operation "GitHub CLI (gh) is not installed. Cannot delete remote repository."
else
# Refresh authentication to get delete_repo scope
gh auth refresh -h github.com -s delete_repo
log_operation "Refreshed GitHub authentication with delete_repo scope"
# Check if remote repository exists
if gh repo view "$repo_name" &> /dev/null; then
log_operation "Remote repository found on GitHub: $repo_name"
# Delete remote repository
gh repo delete "$repo_name" --confirm
if [ $? -eq 0 ]; then
log_operation "Remote repository deleted successfully"
else
log_operation "Error deleting remote repository"
fi
else
log_operation "Remote repository not found on GitHub: $repo_name"
fi
# Revoke permissions by logging out
# gh auth logout -h github.com --hostname github.com --all
gh auth refresh
log_operation "Revoked GitHub permissions by logging out"
fi
log_operation "Deletion process completed for repository: $repo_name"
echo "Repository deletion process finished. Check the logs for details."
# Ask user to press any key to exit
read -n 1 -s -r -p "Press any key to exit..."
echo ""
log_operation "Script execution ended"
#!/bin/bash
# Function to log operations
log_operation() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Ask user for the repository name
echo "Enter the name of the repository you want to delete:"
read repo_name
log_operation "Starting deletion process for repository: $repo_name"
# Check if local repository exists
if [ -d "$repo_name" ]; then
log_operation "Local repository found: $repo_name"
# Delete local repository
rm -rf "$repo_name"
if [ $? -eq 0 ]; then
log_operation "Local repository deleted successfully"
else
log_operation "Error deleting local repository"
fi
else
log_operation "Local repository not found: $repo_name"
fi
# Check if GitHub CLI is installed
if ! command -v gh &> /dev/null; then
log_operation "GitHub CLI (gh) is not installed. Cannot delete remote repository."
else
# Check if remote repository exists
if gh repo view "$repo_name" &> /dev/null; then
log_operation "Remote repository found on GitHub: $repo_name"
# Delete remote repository
gh repo delete "$repo_name" --confirm
if [ $? -eq 0 ]; then
log_operation "Remote repository deleted successfully"
else
log_operation "Error deleting remote repository"
fi
else
log_operation "Remote repository not found on GitHub: $repo_name"
fi
fi
log_operation "Deletion process completed for repository: $repo_name"
echo "Repository deletion process finished. Check the logs for details."
# Ask user to press any key to exit
read -n 1 -s -r -p "Press any key to exit..."
echo ""
log_operation "Script execution ended"
@AlbertProfe
Copy link
Author

image

How to revoke rights: login and logout

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment