-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_to_github.sh
More file actions
executable file
·237 lines (199 loc) · 5.56 KB
/
push_to_github.sh
File metadata and controls
executable file
·237 lines (199 loc) · 5.56 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/bash
# GitHub Auto-Push Script
# This script automates the process of pushing a project to GitHub
echo "=========================================="
echo " GitHub Auto-Push Script"
echo "=========================================="
echo ""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored messages
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Check if we're in a git repository
if [ ! -d ".git" ]; then
print_info "Initializing git repository..."
git init
if [ $? -ne 0 ]; then
print_error "Failed to initialize git repository"
exit 1
fi
print_success "Git repository initialized"
else
print_info "Git repository already exists"
fi
# Get GitHub username
echo ""
read -p "Enter your GitHub username: " GITHUB_USERNAME
if [ -z "$GITHUB_USERNAME" ]; then
print_error "Username cannot be empty"
exit 1
fi
# Get GitHub password (for authentication, though token is preferred)
echo ""
read -sp "Enter your GitHub password (optional, press Enter to skip): " GITHUB_PASSWORD
echo ""
# Get GitHub Personal Access Token
echo ""
print_info "GitHub Personal Access Token is required for authentication"
print_info "Create a token at: https://github.com/settings/tokens"
print_info "Required scope: 'repo' (full control of private repositories)"
echo ""
read -sp "Enter your GitHub Personal Access Token: " GITHUB_TOKEN
echo ""
if [ -z "$GITHUB_TOKEN" ]; then
print_error "Token cannot be empty"
exit 1
fi
# Get repository name
echo ""
read -p "Enter repository name (e.g., my-project): " REPO_NAME
if [ -z "$REPO_NAME" ]; then
print_error "Repository name cannot be empty"
exit 1
fi
# Ask if repository should be private
echo ""
read -p "Should the repository be private? (y/n) [default: y]: " IS_PRIVATE
IS_PRIVATE=${IS_PRIVATE:-y}
if [[ "$IS_PRIVATE" =~ ^[Yy]$ ]]; then
PRIVATE_FLAG="true"
print_info "Repository will be created as PRIVATE"
else
PRIVATE_FLAG="false"
print_info "Repository will be created as PUBLIC"
fi
# Get commit message
echo ""
read -p "Enter commit message [default: Initial commit]: " COMMIT_MSG
COMMIT_MSG=${COMMIT_MSG:-"Initial commit"}
# Get project description (optional)
echo ""
read -p "Enter repository description (optional, press Enter to skip): " REPO_DESCRIPTION
echo ""
print_info "Creating repository on GitHub..."
# Create repository using GitHub API
if [ -z "$REPO_DESCRIPTION" ]; then
API_DATA="{\"name\":\"$REPO_NAME\",\"private\":$PRIVATE_FLAG}"
else
API_DATA="{\"name\":\"$REPO_NAME\",\"private\":$PRIVATE_FLAG,\"description\":\"$REPO_DESCRIPTION\"}"
fi
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/user/repos \
-d "$API_DATA")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" -eq 201 ]; then
print_success "Repository '$REPO_NAME' created successfully on GitHub!"
elif [ "$HTTP_CODE" -eq 422 ]; then
print_warning "Repository '$REPO_NAME' already exists, continuing..."
elif [ "$HTTP_CODE" -eq 401 ]; then
print_error "Authentication failed. Please check your GitHub token."
print_info "Make sure your token has the 'repo' scope enabled"
exit 1
else
print_error "Failed to create repository. HTTP Code: $HTTP_CODE"
echo "Response: $BODY"
exit 1
fi
# Create .gitignore if it doesn't exist
if [ ! -f ".gitignore" ]; then
print_info "Creating default .gitignore file..."
cat > .gitignore << 'EOF'
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
venv/
env/
ENV/
*.egg-info/
dist/
build/
# Logs
*.log
# Environment variables
.env
.env.local
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Large binary files
*.deb
*.rpm
*.dmg
EOF
print_success ".gitignore created"
fi
# Add all files
print_info "Adding files to git..."
git add .
if [ $? -ne 0 ]; then
print_error "Failed to add files"
exit 1
fi
# Check if there are changes to commit
if git diff --staged --quiet; then
print_warning "No changes to commit"
else
# Commit changes
print_info "Committing changes..."
git commit -m "$COMMIT_MSG"
if [ $? -ne 0 ]; then
print_error "Failed to commit changes"
exit 1
fi
print_success "Changes committed"
fi
# Set branch to main
print_info "Setting branch to 'main'..."
git branch -M main 2>/dev/null || true
# Remove existing remote if it exists
git remote remove origin 2>/dev/null || true
# Add remote with token for authentication
print_info "Configuring remote repository..."
git remote add origin "https://${GITHUB_TOKEN}@github.com/${GITHUB_USERNAME}/${REPO_NAME}.git"
# Push to GitHub
print_info "Pushing to GitHub..."
git push -u origin main
if [ $? -eq 0 ]; then
print_success "Code successfully pushed to GitHub!"
echo ""
print_info "Repository URL: https://github.com/$GITHUB_USERNAME/$REPO_NAME"
echo ""
# Update remote to use SSH for future pushes (more secure)
print_info "Updating remote to use SSH for future pushes..."
git remote set-url origin "git@github.com:${GITHUB_USERNAME}/${REPO_NAME}.git"
print_success "Remote updated to SSH"
print_info "Future pushes can use: git push"
else
print_error "Failed to push to GitHub"
exit 1
fi
echo ""
print_success "All done! Your project is now on GitHub."
echo ""