51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
const simpleGit = require('simple-git')
|
|
const path = require('path')
|
|
|
|
const git = simpleGit(path.join(__dirname))
|
|
|
|
const run = async () => {
|
|
try {
|
|
console.log('=== 初始化Git仓库 ===')
|
|
await git.init()
|
|
|
|
console.log('=== 设置用户信息 ===')
|
|
await git.addConfig('user.name', 'jiapengyu')
|
|
await git.addConfig('user.email', 'jiapengyu@example.com')
|
|
|
|
console.log('=== 设置远程仓库 ===')
|
|
const remoteUrl = 'git@git.torchcloud.cn:jiapengyu/huishou.git'
|
|
await git.addRemote('origin', remoteUrl)
|
|
|
|
console.log('=== 添加所有文件 ===')
|
|
await git.add('.')
|
|
|
|
console.log('=== 提交代码 ===')
|
|
await git.commit('Initial commit - huishou project')
|
|
|
|
console.log('=== 推送代码 ===')
|
|
await git.push('origin', 'main', { '--set-upstream': null })
|
|
|
|
console.log('=== 推送成功 ===')
|
|
} catch (error) {
|
|
console.error('=== 推送失败 ===')
|
|
console.error(error.message)
|
|
|
|
// 如果main分支不存在,尝试创建
|
|
if (error.message.includes('fatal: A branch named')) {
|
|
try {
|
|
console.log('=== 创建main分支 ===')
|
|
await git.checkoutLocalBranch('main')
|
|
await git.add('.')
|
|
await git.commit('Initial commit - huishou project')
|
|
await git.push('origin', 'main', { '--set-upstream': null })
|
|
console.log('=== 推送成功 ===')
|
|
} catch (err) {
|
|
console.error('=== 仍然失败 ===')
|
|
console.error(err.message)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
run()
|