简体   繁体   English

如何设置本地Git存储库和本地备份目录?

[英]How do I set up a local Git repository and a local backup directory?

UPDATE UPDATE

I set up the two Git repositories following the directions from one of the answers below, but the back-up directory doesn't have copies of the files in the working directory. 我按照以下答案之一的说明设置了两个Git存储库,但备份目录没有工作目录中的文件副本。 This is what I see in the back-up directory... 这是我在备份目录中看到的...

$ ls
total 0
drwxr-xr-x  10 Hristo  staff  340 Feb 25 21:40 Kamma.git

... but I was expecting something like the following... ......但我期待以下的东西......

$ ls
total 16
drwxr-xr-x   6 Hristo  staff   204 Dec 19 19:51 css
drwxr-xr-x   3 Hristo  staff   102 Nov 13 18:00 images
-rw-r--r--@  1 Hristo  staff  4440 Feb 26 03:20 index.html
drwxr-xr-x  15 Hristo  staff   510 Feb 24 14:19 js

Again, I want my main working directory, /Users/Hristo/Sites/Kamma , to be the place where I make changes and do commits and reverts and such. 同样,我希望我的主要工作目录/Users/Hristo/Sites/Kamma成为我进行更改并进行提交和还原等的地方。

I want /Users/Hristo/Sites/Kamma_bak to be the place where I periodically push important changes, like a new version of my project, where everything is a copy of my working directory, just not the most up to date copy. 我想/Users/Hristo/Sites/Kamma_bak是我定期推送重要更改的地方,比如我的项目的新版本,其中所有内容都是我的工作目录的副本,而不是最新的副本。

I hope this makes sense. 我希望这是有道理的。


Original Post 原帖

I would like to set up a local Git repository. 我想建立一个本地Git存储库。 So for example, I would like my main location to be /Users/Hristo/Sites/Kamma which is where I would do all of my work. 所以,例如,我希望我的主要位置是/Users/Hristo/Sites/Kamma ,这是我将完成所有工作的地方。

I'm looking to be able to commit changes and revert back to previous versions, etc..., the way subversion works. 我希望能够提交更改并恢复到以前的版本等...,颠倒的工作方式。 But I would also like to have a back-up directory, /Users/Hristo/Sites/Kamma_bak , as a fail safe, where I would "push" versions every once in a while. 但我还希望有一个备份目录/Users/Hristo/Sites/Kamma_bak作为故障安全,我会/Users/Hristo/Sites/Kamma_bak “推送”版本。

In this back-up directory /Users/Hristo/Sites/Kamma_bak , I would like all the files and such to exist as duplicate copies, as back-up copies, of the working directory /Users/Hristo/Sites/Kamma 在这个备份目录/Users/Hristo/Sites/Kamma_bak ,我希望所有文件等作为工作目录/Users/Hristo/Sites/Kamma副本副本,作为备份副本存在

How would I do this with Git? 我如何用Git做到这一点? I have it already installed on my machine, running Snow Leopard. 我已经在我的机器上安装了它,运行Snow Leopard。

Yes you can absolutely do that. 是的,你绝对可以做到这一点。 Although I would recommend that your backup folder is on another computer. 虽然我建议您的备份文件夹在另一台计算机上。

Please read this thread, Using Git and Dropbox together effectively? 请阅读此主题, 有效地同时使用Git和Dropbox?

I think the instructions provide exactly what you are looking for; 我认为说明书提供了您正在寻找的内容; the "dropbox" part is of course optional (it's just a folder). “dropbox”部分当然是可选的(它只是一个文件夹)。

EDIT: Forget about the dropbox bit. 编辑:忘了Dropbox位。 Dropbox is just a local folder + a service that replicates it offsite. Dropbox只是一个本地文件夹+一个在异地复制它的服务。 These instructions will work for you too WITH A LOCAL FOLDER. 使用本地文件夹时,这些说明也适用于您。

The important bit it to create a (local) bare repo which you setup as a 'git remote' and push your changes to. 重要的是创建一个(本地)裸仓库,你设置为'git remote'并将你的更改推送到。

Let me copy and paste from quoted thread, and make the changes for you. 让我从引用的线程中复制并粘贴,并为您进行更改。

Something like this should work: 这样的事情应该有效:

~/Sites/Kamma $ git init
~/Sites/Kamma $ git add .
~/Sites/Kamma $ git commit -m "first commit"
~/Sites/Kamma $ cd ~/Sites/Kamma_bak

~/Sites/Kamma_bak $ mkdir Kamma.git
~/Sites/Kamma_bak $ cd Kamma.git
~/Sites/Kamma_bak $ git init --bare
~/Sites/Kamma_bak $ cd ~/Sites/Kamma

~/Sites/Kamma $ git remote add origin ~/Sites/Kamma_bak/Kamma.git
~/Sites/Kamma $ git push origin master

git init will create a git repo. git init将创建一个git repo。 To make the backup, do a git clone --no-hardlinks on the original repo to copy it. 要进行备份,请在原始git clone --no-hardlinks上执行git clone --no-hardlinks以复制它。 From there you can do a push from one repo to the other. 从那里你可以从一个回购推动到另一个回购。

Note: A “backup” copy on the same machine is not much of a backup (especially if it is on the same disk). 注意:同一台计算机上的“备份”副本不是备份(特别是如果它位于同一磁盘上)。 To be reliable you really need to copy your data to one (or more!) different machines/media, preferably in different locations. 为了可靠,您确实需要将数据复制到一个(或多个!)不同的机器/介质,最好是在不同的位置。

It sounds like your “backup” is a bare repository , but want it to be a non-bare repository (ie you want it to have its own working tree checked out). 听起来你的“备份”是一个裸存储库 ,但希望它是一个非裸存储库(即你希望它有自己的工作树签出)。

The problem is that pushing to a non-bare repository is usually not a good idea. 问题是推送到非裸存储库通常不是一个好主意。 Effectively, such pushes are likely to update the branch that HEAD points to without updating the index or working tree. 实际上,这样的推送可能会更新HEAD指向的分支而不更新索引或工作树。 This can lead to very confusing situations in the receiving repository (eg added files shown with a deleted status, etc.). 这可能导致接收存储库中非常混乱的情况(例如,添加的文件显示为已删除状态等)。 For this reason, Git versions 1.7.0 and later default to refusing to accept pushes to the currently checked out branch of non-bare repositories. 出于这个原因,Git版本1.7.0及更高版本默认拒绝接受推送到当前检出的非裸存储库分支。

Note: When you push to a bare repository for backup purposes, all the files contained in the pushed commits are there, they are just not checked out (they are compressed and kept in Git the object store as “loose objects” and “pack files”). 注意:当您推送到裸存储库进行备份时,推送提交中包含的所有文件都在那里,它们只是没有签出(它们被压缩并保存在Git中,对象存储为“松散对象”和“打包文件” “)。 The pushed data represents a full copy of history of the content that you committed and pushed. 推送的数据表示您提交和推送的内容的完整历史记录副本。 You just can not access the content directly. 您无法直接访问内容。 Instead, you must clone it to a non-bare repository repository (and thus checkout a commit) or use git archive to extract a set of files without an additional repository or full checkout. 相反,您必须将其克隆到非裸存储库(从而检出提交)或使用git archive提取一组文件而无需额外的存储库或完整的检出。


I am not really convinced that you need something like what you describe. 我真的不相信你需要像你描述的那样。

If you need to examine an old snapshot of your repository (and you do not feel like doing it in your normal working repository), then you should just clone a temporary copy and checkout the desired, old commit. 如果您需要检查存储库的旧快照(并且您不想在正常的工作存储库中执行此操作),那么您应该只是克隆一个临时副本并检查所需的旧提交。 Local clones are cheap since they can hardlink the object store files instead of copying them. 本地克隆很便宜,因为它们可以硬链接对象存储文件而不是复制它们。 The historical commit graph is generally all you need to “go back in time”. 历史提交图通常是“回到过去”所需要的。 While Git will let you rewrite the history graph at will, you are not really in much danger of making unrecoverable changes since it usually requires -f / --force switches and/or provides recovery mechanisms (reflogs, refs/original/, minimum age requirements before collecting unreferenced objects, etc.). 虽然Git会让你随意重写历史图,但你并没有真正做出不可恢复的更改,因为它通常需要-f / --force开关和/或提供恢复机制(reflogs,refs / original /,最小年龄)收集未引用对象之前的要求等)。

It is a good idea to have another repository (especially on another machine in another location) where you can push your commits for backup purposes (so that you can recover from (eg) rm -rf working_repo ), but a bare repository is usually entirely sufficient. 有另一个仓库(尤其是在另一个地方另一台计算机上),在那里你可以把你的提交用于备份(这样就可以从(例如)恢复一个好主意rm -rf working_repo ),但纯仓库通常是完全足够。 When you need to recover, you just make a clone. 当你需要恢复时,你只需要克隆。 When you want to examine some old snapshot without disturbing your normal working repository, you make a temporary clone somewhere. 如果要在不打扰正常工作存储库的情况下检查某些旧快照,可以在某处进行临时克隆。 With good commit hygiene, git diff , git log (especially the -p and -S options), and git show can often provide whatever “archeological” information you might want from old commits without needing to checkout anything at all (they even work in bare repositories). 有了良好的提交卫生, git diffgit log (特别是-p-S选项)和git show通常可以提供你可能想要的旧的提交的任何“考古”信息,而不需要检查任何东西(它们甚至可以在裸存储库)。


However, if you are willing to accept the risk, you can do exactly what you want. 但是,如果您愿意接受风险,您可以完全按照自己的意愿行事。

Git, like any other “sharp” tool, will let you risk “shooting yourself in the foot” (pardon the mixed metaphor). 像任何其他“尖锐”工具一样,Git会让你冒险“在脚下射击自己”(原谅混合比喻)。

  1. Create and configure your backup repository and add it as a remote in the working repository. 创建和配置备份存储库,并将其作为远程添加到工作存储库中。

     # paths to the repositories WORKING=/path/to/working BACKUP=/path/to/backup # name for the backup repository in the working repository REMOTE=backup ! test -d "$BACKUP" || (echo "error: $BACKUP already exists"; exit 1) && git clone --origin working "$WORKING" "$BACKUP" && ( cd "$BACKUP" && git config receive.denyCurrentBranch false && git remote rm working ) && ( cd "$WORKING" && git remote add "$REMOTE" "$BACKUP" && git config remote."$REMOTE".push 'refs/heads/*:refs/heads/*' ) 
  2. In the backup repository, setup a post-receive or post-update hook that does a git reset --hard . 在备份存储库中,设置一个post-receivepost-update钩子,它执行git reset --hard This will keep the index and working tree up to date with the currently checked out branch. 这将使索引和工作树与当前签出的分支保持同步。

    The Git FAQ “Why won't I see changes in the remote repo after "git push"?” points to an example post-update script that can do this relatively safely (it saves a stash if the working tree or the index are dirty—this can still lose untracked files if newly added files with the same pathnames are pushed). Git FAQ “为什么我不会在”git push“之后看到远程仓库中的变化?”指向一个可以相对安全地执行此操作的示例post-update脚本 (如果工作树或索引是脏的,它会保存一个存储 - 如果推送了具有相同路径名的新添加文件,这仍然可以丢失未跟踪的文件)。

     ( H="$BACKUP"/.git/hooks/post-update && curl http://utsl.gen.nz/git/post-update >$H && chmod +x "$H" ) 
  3. Push to the backup repository when you want to update it. 如果要更新备份存储库,请将其推送到备份存储库。

     (cd "$WORKING" && git push "$REMOTE") 

If you use a setup like this, you should absolutely avoid working in the backup working tree. 如果您使用这样的设置,您绝对应该避免在备份工作树中工作。 Any commits you make there, any staged changes you leave there, and any untracked files you leave there are liable to be lost. 你在那里做的任何提交,你离开那里的任何阶段性更改,以及你留在那里的任何未跟踪的文件都可能丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置本地目录以镜像远程git存储库? - How to set up a local directory for mirroring a remote git repository? 如何备份本地 Git 存储库? - How to backup a local Git repository? 在Xcode 4中如何将项目与我设置的本地Git存储库相关联? - in Xcode 4 how to associate a project to the local Git repository I set up? 如何在已经存在的本地 git 存储库上设置 Visual Studio 2019? - How do I set up Visual Studio 2019 on a local git repository which already exists? 如何将本地裸git存储库备份到远程git存储库 - How to backup a local bare git repository to remote git repository 如何在不同的驱动器上设置工作目录和本地存储库? - How can I set up the working directory and the local repository on different drives? 如何从当前现有的本地git存储库中最佳设置“中央” git存储库? - How to best set up a “central” git repository from a current existing local git repository? 如何查找和备份本地GIT存储库以便以后还原? - How to find and backup your local GIT repository to be able to restore it later? 如何从本地 git 存储库和本地文件系统中删除文件,而不是从远程存储库中删除文件? - How do I remove a file from local git repository and local file system but not from remote repository? 如何将IntelliJ IDEA项目链接到本地​​git存储库? - How do I link an IntelliJ IDEA project to a local git repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM