windows GIT 克隆到外部驱动器进行备份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1887364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:34:42  来源:igfitidea点击:

GIT clone to external drive for backup

windowsgitbackup

提问by Paddy

We have GIT set up within our windows network (using msysgit & GitExtensions). We each have our own repositories and we push to a remote 'bare' repository on one of our servers. All good.

我们在 Windows 网络中设置了 GIT(使用 msysgit 和 GitExtensions)。我们每个人都有自己的存储库,我们推送到我们其中一台服务器上的远程“裸”存储库。都好。

I'm trying to set up a scheduled job on the server, which will clone a repository from the C drive to an external drive (on F) - having some difficulty getting this to work. I can do this in GIT bash relatively easily, but I'm not sure how to save this into a batch file that I can then scehdule.

我正在尝试在服务器上设置计划作业,这会将存储库从 C 驱动器克隆到外部驱动器(在 F 上) - 使其工作有一些困难。我可以在 GIT bash 中相对容易地做到这一点,但我不知道如何将它保存到一个批处理文件中,然后我可以安排。

What I have so far:

到目前为止我所拥有的:

rmdir F:\GitClone /s /q
mkdir F:\GitClone
mkdir F:\GitClone\Repo1
CD /D F:\GitClone\Repo1\
GIT CLONE /c/GIT/Repo1/

I've also tried the following for the last line:

我还为最后一行尝试了以下内容:

GIT CLONE C:\GIT\Repo1\

But this doesn't work either... I'm a little stumped and would appreciate some help. The C drive contains our bare repositories and the F drive being our external drive that we swap out daily...

但这也不起作用......我有点难住,希望得到一些帮助。C 驱动器包含我们的裸存储库,F 驱动器是我们每天更换的外部驱动器...



Several answers here that have been very useful, thanks. My resulting answer is probably a combination of these, so points for pointing out how to run a bash script and how to script the pull/push.

这里有几个非常有用的答案,谢谢。我得到的答案可能是这些的组合,因此指出如何运行 bash 脚本以及如何编写拉/推脚本的要点。

Need to bring these together to work so that it's happy when various drives are swapped in and out (i.e. clone a repository if it doesn't exist on the external drive and then only pull the differences otherwise), but that should be doable. Thanks to all.

需要将这些组合在一起工作,以便在换入和换出各种驱动器时很高兴(即,如果外部驱动器上不存在则克隆一个存储库,否则只提取差异),但这应该是可行的。谢谢大家。

回答by nes1983

Please note that git itself is excellent at copying only the needed changes to a cloned repository.

请注意,git 本身非常擅长仅将所需的更改复制到克隆的存储库。

If you want a copy of your repo to be regularly updated, do this: You create a bare repository as a backup repository, and then repeatedly push all new changes there (no need to delete the old backup).

如果您希望定期更新存储库的副本,请执行以下操作:创建一个裸存储库作为备份存储库,然后重复推送所有新更改(无需删除旧备份)。

Ok, let's start by creating your repo

好的,让我们从创建您的仓库开始

$ cd /tmp
$ mkdir myrepo && cd myrepo
$ touch hi && git add . && git commit -m "bla" 

So, this is your repository. Now we create the clone:

所以,这是您的存储库。现在我们创建克隆:

$ cd /tmp
$ mkdir backup && cd backup 
$ git --bare init
Initialized empty Git repository in /tmp/backup/

Now, let's set up your repo for regular backups …

现在,让我们为定期备份设置您的存储库……

$ cd /tmp/myrepo
$ git remote add backup /tmp/backup
$ git config remote.backup.mirror true

Then copy everything to the backup:

然后将所有内容复制到备份:

$ git push backup
Counting objects: 3, done.
Writing objects: 100% (3/3), 206 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /tmp/backup
 * °new branch§      master -> master

And see if it worked:

看看它是否有效:

$ cd /tmp/backup
$ git log
commit d027b125166ff3a5be2d7f7416893a012f218f82
Author: Niko Schwarz <niko.schwarzàgmail.com>
Date:   Fri Dec 11 12:24:03 2009 +0100

    hi

Tada, you're set. Therefore, all your script needs to do is to issue git push backup. There's exactly no need to repeatedly throw away the old backup.

多田,你准备好了。因此,您的脚本所需要做的就是发出git push backup. 完全没有必要反复丢弃旧的备份。

The alternative is you can have rsyncdo it all for you:

另一种选择是你可以让rsync为你做这一切:

rsync -av rsync://rsync.samba.org/ftp/unpacked/rsync /dest/dir/

User Offby adds: Since version 1.5.4, "git remote add" takes a "--mirror" option, which saves you both from having to "git config remote.origin.mirror true", and from having to pass --mirror to "git push".

用户 Offby 补充说:从 1.5.4 版开始,“git remote add”采用“--mirror”选项,这使您不必“git config remote.origin.mirror true”,也不必通过 --mirror到“git push”。

回答by Pod

You could always just schedule bash.exe mybashbackupscript.sh

你总是可以只安排 bash.exe mybashbackupscript.sh

Anyway, in windows terms:

无论如何,在 Windows 方面:

rmdir F:\GitClone /s /q
mkdir -p F:\GitClone\Repo1
copy c:\GIT\Repo1\.git F:\GitClone\Repo1\.git

git clonedoesn't really do anything fancier than that.

git clone没有比这更有趣的事情了。

edit: as someone else pointed out, it's probably best to just make a new backup repo and just pull/fetch into that. You'll avoid any accesing issues with the .git :)

编辑:正如其他人指出的那样,最好只制作一个新的备份存储库,然后将其拉入/获取。您将避免 .git 的任何访问问题:)

回答by Igor Zevaka

Because gitcommand is little bit weird you have to use callto execute any git commands from a batch file:

因为git命令有点奇怪,所以你必须使用它call来从批处理文件中执行任何 git 命令:

rmdir F:\GitClone /s /q
mkdir F:\GitClone
CD /D F:\GitClone\
call GIT CLONE c/GIT/Repo1/

回答by Tryum

Sorry, I can't comment posts, but I was thinking too about copying the .git, but what happens if the .git is copyed during a pull ?

抱歉,我无法评论帖子,但我也在考虑复制 .git,但是如果在 pull 期间复制 .git 会发生什么?

Anyway, why copying the whole stuff as you could fetch the deltas from time to time ?

无论如何,为什么要复制整个内容,因为您可以不时获取增量?

Initialization of backup : (in F:\GitClone\Repo1 empty)

备份的初始化:(在 F:\GitClone\Repo1 中为空)

git init
git add remote origin /c/GIT/Repo1

Then your "delta backup script" would just do :

然后您的“增量备份脚本”将执行以下操作:

cd /f/GitClone/Repo1
git fetch origin

回答by Aaron Digulla

Why do you delete the clone all the time? What is the point of cloning an existing repository when you just want to copy the file?

为什么你总是删除克隆?当您只想复制文件时,克隆现有存储库有什么意义?

Just create the repo on the external drive (using git cloneonce), and then run git pullon it regularly.

只需在外部驱动器上创建存储库(使用git clone一次),然后git pull定期在其上运行。

回答by Dmitry Ponyatov

Problem solved:

问题解决了:

h: (flash drive)
cd \
mkdir YourProject
cd YourProject
git init
git remote add origin [email protected]:user/YourProject.git
git remote add local C:\w\YourProject
git pull origin master
git push local master