在onedrive上搭建git"中央仓库"
参考和这么做的原因
这么做的原因:如果项目代码直接存放在onedrive上,会导致每次构建项目都会触发onedrive的同步工作,同步很多不必要的文件,所以考虑使用git的版本控制,在onedrive做仓库,并在本地建立仓库从onedrive clone
思路来源:有给 OneDrive 设置 .ignore 的方法吗? - V2EX #7
本地仓库搭建方法参考:How to create a git remote? - Stack Overflow
执行步骤
- 安装git
- 在onedrive创建文件夹作为远程仓库地址:
my-remote-repository
- 在
my-remote-repository
执行git裸仓库初始化命令:git init --bare
- 在本地文件夹clone
my-remote-repository
作为远程仓库:git clone file://E:\OneDrive\my-remote-repository
完成!
之后就可以在VSCode安装gitLens爽爽pull/push了
快捷化操作
编写powershell脚本(适用于win11)(需要先安装git)
- 需要先安装git
- 请按照需要修改
$oneDrivePath
和$repositoryPath
[System.Environment]::GetFolderPath("Personal") + "\OneDrive"
仅适用于未修改OneDrive同步位置的情况- 将上述代码保存为.ps1文件即可作为powershell脚本
- powershell脚本默认不能双击运行,需要右键使用 PoweShell 运行,或者右键文件夹在终端打开,然后输入
./
+脚本名
再回车运行
1. 在onedrive创建空白远程仓库并克隆到本地
- 在目录 /my/local/dir 运行,输入folderName,会生成folderName存储库,克隆出 /my/local/dir/folderName文件夹
# 存储当前路径
$localpath = $PWD.Path
# OneDrive目录
#$oneDrivePath = [System.Environment]::GetFolderPath("Personal") + "\OneDrive"
$oneDrivePath = "E:\OneDrive"
# 设定库目录
$repositoryPath = "\Studio\Code\Cpp"
echo 存储库路径为:$($oneDrivePath+$repositoryPath)
# 输入文件夹名
$folderName = Read-Host "-请输入文件夹名"
# 在OneDrive目录下创建文件夹
$remotepath = Join-Path -Path $($oneDrivePath+$repositoryPath) -ChildPath $folderName
# 检查是否已经存在同名文件夹
if (Test-Path $remotepath) {
$overwrite = Read-Host "文件夹 $folderName 已存在于 OneDrive 目录下。是否覆盖文件夹?(y/n)"
if ($overwrite -eq "n") {
Write-Host "操作已取消。请重新运行脚本。"
return
}
elseif ($overwrite -ne "y") {
Write-Host "无效的输入。操作已取消。请重新运行脚本。"
return
}
else {
Remove-Item -Path $remotepath -Recurse -Force -Confirm:$false | Out-Null
}
}
# 创建新的文件夹
New-Item -ItemType Directory -Path $remotepath | Out-Null
# 在$remotepath中调用git init --bare
Set-Location $remotepath
git init --bare
echo 远程存储库已生成:$remotepath
# 切换回localpath
Set-Location $localpath
# 运行命令git clone file://${remotepath}
git clone file://$remotepath
# 输出结果
Write-Host "操作完成!已在OneDrive目录下创建文件夹 $folderName,并成功设置远程仓库。"
2. 在onedrive创建同名空白远程仓库并推送本地内容
- 要求脚本所在文件夹已经使用git进行版本管理
- 在目录 /my/local/dir/myproject 运行,会生成名为myproject的远程仓库
# 存储当前路径
$localpath = $PWD.Path
# OneDrive目录
#$oneDrivePath = [System.Environment]::GetFolderPath("Personal") + "\OneDrive"
$oneDrivePath = "E:\OneDrive"
# 设定库目录
$repositoryPath = "\Studio\Code\Cpp"
echo 存储库路径为 $($oneDrivePath+$repositoryPath)
$currentFolder = Split-Path -Path $PWD.Path -Leaf
# 在OneDrive目录下创建文件夹
$remotepath = Join-Path -Path $($oneDrivePath+$repositoryPath) -ChildPath $currentFolder
# 检查是否已经存在同名文件夹
if (Test-Path $remotepath) {
$overwrite = Read-Host "文件夹 $currentFolder 已存在于 OneDrive 目录下。是否覆盖文件夹?(y/n)"
if ($overwrite -eq "n") {
Write-Host "操作已取消。请重新运行脚本。"
return
}
elseif ($overwrite -ne "y") {
Write-Host "无效的输入。操作已取消。请重新运行脚本。"
return
}
else {
Remove-Item -Path $remotepath -Recurse -Force -Confirm:$false | Out-Null
}
}
# 创建新的文件夹
New-Item -ItemType Directory -Path $remotepath | Out-Null
# 在$remotepath中调用git init --bare
Set-Location $remotepath
git init --bare
echo 远程存储库已生成:$remotepath
# 切换回localpath
Set-Location $localpath
# 运行命令git remote add origin file://${remotepath}
git remote add origin file://$remotepath
git push --set-upstream origin master
# 输出结果
Write-Host "操作完成!已在OneDrive目录下创建文件夹 $currentFolder,并成功设置远程仓库。"
相关解释
git init --bare
from chatgpt
git init --bare
是一个用于创建一个裸仓库(bare repository)的Git命令。
在Git中,一个裸仓库是一个没有工作目录的仓库。它只包含Git版本控制系统所需的文件和元数据,没有实际项目文件。裸仓库通常用作共享和中央仓库,用于多人协作和代码共享。
具体来说,git init --bare
命令在当前目录下创建一个新的裸仓库。这个仓库不包含项目文件,只包含用于版本控制的.git目录和相关的文件。
创建裸仓库后,其他开发人员可以通过克隆(clone)这个裸仓库来获取代码的副本,并向其中推送(push)自己的更改。裸仓库没有工作目录,因此不支持直接在其上进行修改和编辑文件,而是用作中央代码存储的中转站。
裸仓库的典型用途包括:
- 作为团队中央仓库:团队成员可以从裸仓库克隆代码,将自己的更改推送到裸仓库,并从中获取最新的更改。
- 用于部署:在服务器上创建一个裸仓库,用于部署应用程序或网站的更新。开发人员可以将新的代码推送到裸仓库,然后服务器上的工作目录会自动更新。
总之,git init --bare
是创建一个裸仓库的命令,用于共享代码、多人协作和中央存储。