Git创建本地远端仓库进行版本管理
Git创建本地远端仓库进行版本管理
Git创建本地远端仓库进行版本管理
00 前言
有些时候, 我们需要一个离线版本管理, 但并不想每次都强行Copy整个仓库. 这个时候就要用到本地裸仓库及网盘/U盘.
01 处理方法
利用OneDrive, 在OneDirve中创建一个目录, 比如.bare-repository- 本地创建bare仓库
- 进入
bare-repository目录, 开启命令行 - 输入
git init --bare, 回车
- 进入
- 初始化仓库
- 进入自己想要跟踪的目录, 比如
_docs - 输入
git init, 回车 - 输入
git remote add origin F:/bare-repository, 请把F:/bare-repository换成自己的路径, 回车- 注意, 直接在Windows中拷贝路径时, 要将其中的分隔符替换为
/
- 注意, 直接在Windows中拷贝路径时, 要将其中的分隔符替换为
- 输入
git push origin main
- 进入自己想要跟踪的目录, 比如
单独将裸仓库指定为某个仓库的远端
1
git remote add origin E:/bare-repository.git
- 将裸仓库进行远程备份
输入
git clone "F:\OneDrive\Git\jauved.docs"建议不直接使用OneDrive目录作为远端目录, Git使用状态下会对文件进行占用, 而OneDrive同步时会因为占用而导致写入或修改困难
可以先在本地磁盘上建立裸仓库, 然后用bundle的形式, 将裸仓库在OneDrive上进行备份.备份批处理文件参考如下. 需要将其中的目录(
E:\GitBareRepos, 裸仓库根目录, 注意, 不是裸仓库目录, 而是所有裸仓库的上级目录.F:\OneDrive\Git\GitBareReposBackups, 目标目录)替换为自己的目录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
@echo off setlocal enabledelayedexpansion REM ------------------------------------------------------------ REM backup.bat - Git bundle backup for multiple bare repositories REM Author: ChatGPT REM Usage: REM Double-click or run in terminal, input repo name or press Enter for all. REM ------------------------------------------------------------ REM === Configuration === set "BASEDIR=E:\GitBareRepos" set "BACKUPDIR=F:\OneDrive\Git\GitBareReposBackups" REM Ensure the backup directory exists if not exist "%BACKUPDIR%" ( mkdir "%BACKUPDIR%" ) REM === Get current date (yyyy-MM-dd) === for /f %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd"') do set "TODAY=%%I" REM === Prompt for repository name === set /p "REPO=Enter repository name to backup (press Enter for all): " REM === Prompt whether to overwrite existing bundles (default: Y) === set /p "OVERWRITE=Overwrite existing bundle without date? [Y/N] (default Y): " if /I "%OVERWRITE%"=="" set "OVERWRITE=Y" echo. if /I "%OVERWRITE%"=="Y" ( set "SUFFIX=" ) else ( set "SUFFIX=-%TODAY%" ) if "%REPO%"=="" ( REM Backup all repositories echo Backing up all repositories under %BASEDIR%... for /d %%D in ("%BASEDIR%\*") do ( set "NAME=%%~nxD" echo Creating bundle for !NAME!... git -C "%%D" bundle create "%BACKUPDIR%\!NAME!!SUFFIX!.bundle" --all ) ) else ( REM Backup single repository if exist "%BASEDIR%\%REPO%" ( echo Creating bundle for %REPO%... git -C "%BASEDIR%\%REPO%" bundle create "%BACKUPDIR%\%REPO%%SUFFIX%.bundle" --all ) else ( echo ERROR: Repository "%REPO%" not found under "%BASEDIR%". pause exit /b 1 ) ) echo. echo Backup operation completed. pause
- 然后正常的进行推送/同步即可
02 通过Bundle还原裸仓库并拉取
02.1a 直接还原
还原裸仓库
1
git clone --bare F:/OneDrive/Git/GitBareReposBackups/YourRepo.bundle E:/GitBareRepos/YourRepo.git
02.1b 先初始化后还原
你也可以先初始化一个裸仓库
1
git init --bare E:/GitBareRepos/YourRepo.git
将bundle拉入
1
2
cd E:/GitBareRepos/YourRepo.git
git pull F:/OneDrive/Git/GitBareReposBackups/YourRepo.bundle --all
02.2 拉取工作区
1
git clone E:/GitBareRepos/YourRepo.git D:/Projects/YourRepo
参考网页
本文由作者按照 CC BY 4.0 进行授权