-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_init.py
46 lines (35 loc) · 1.4 KB
/
git_init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import subprocess
import sys
from pathlib import Path
def run_command(command):
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
stdout, stderr = process.communicate()
return stdout.decode().strip(), stderr.decode().strip()
def init_github_repo():
# Get the parent folder name as repository name and GitHub username
repo_path = Path.cwd()
repo_name = repo_path.name
github_user = repo_path.parent.name
# Initialize local Git repository
print("Initializing local Git repository...")
run_command(f"git init")
# Add and commit all files
run_command(f"git add .")
run_command(f"git commit -m 'Initial commit'")
# Create remote GitHub repository using the GitHub CLI
print("Creating remote GitHub repository...")
stdout, stderr = run_command(f"gh repo create {github_user}/{repo_name} --public")
if stderr:
print("Error:")
print(stderr)
sys.exit(1)
# Set the remote origin for the local Git repository and push
print("Connecting local and remote repositories...")
run_command(f"git remote add origin [email protected]:{github_user}/{repo_name}.git")
run_command(f"git branch -M main")
run_command(f"git push -u origin main")
print(f"Repository setup complete: https://github.com/{github_user}/{repo_name}")
if __name__ == "__main__":
init_github_repo()