git 使用git管理家目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21266948/
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
Use git to manage home directory
提问by Aurelien Navarre
I'd like to create one single git repoin my Linux $HOME directory. In this repo - obviously - I could add everything under version control, but this wouldn't make any sense. I'd like to only add the files and folders that are relevant to me, like .vimrc, .vim, .bashrc, etc. (probably only hidden files and folders, but maybe not)
我想在我的 Linux $HOME 目录中创建一个 git 存储库。在这个 repo 中 - 显然 - 我可以在版本控制下添加所有内容,但这没有任何意义。我只想添加与我相关的文件和文件夹,例如.vimrc、.vim、.bashrc等(可能只是隐藏的文件和文件夹,但也可能不是)
I know I could leverage .gitignoreto try and achieve such behavior but that would be painful and virtually un-maintainable. Instead, what I'd want to know is if there'd be any way to manually declare files and folders that I would want to manage, and only that.
我知道我可以利用.gitignore来尝试实现这种行为,但这会很痛苦,而且几乎无法维护。相反,我想知道的是是否有任何方法可以手动声明我想要管理的文件和文件夹,仅此而已。
回答by Niklas
.gitignore
.gitignore
# Ignore everything
*
# But not these files...
!*.vimrc
!*.vim
!*.bashrc
!.gitignore
# etc...
回答by Pete
I would setup an alias for use only with your home repo this way you can use the alias in place of git command and you always know the context you are working in
我会设置一个仅用于您的家庭仓库的别名,这样您就可以使用别名代替 git 命令,并且您始终知道您正在使用的上下文
# .bashrc
alias home='git --work-tree=$HOME --git-dir=$HOME/.home'
Issuing the comand
发出命令
$ home init
will create a repo in .home folder in your $HOME and irrelevant of current directory you can work with your home repo with the alias 'home' in place of 'git'
将在您的 $HOME 的 .home 文件夹中创建一个 repo 并且与当前目录无关,您可以使用别名“home”代替“git”来处理您的主存储库
This alongside a .gitignore should be all you need.
这与 .gitignore 一起应该是你所需要的。
# .gitignore
*
# keep track
!.bashrc
!.gitignore
NB. DO NOT KEEP .ssh/keys in repo as it presents file permission and security issues.
注意。不要在 repo 中保留 .ssh/keys,因为它存在文件权限和安全问题。
回答by Mike Monkiewicz
I would suggest notputting your $HOME directory under git version control. I did this, because I thought I was exceedingly clever and wanted my .dotfiles under version control. However, git is greedy and ravenous. I had to add tons of directories (or even *) to my $HOME/.gitignore. And do you have any git repositories elsewhere in your home directory? I did. But those git repositories will use your $HOME/.gitignore as your global .gitignore unless you set that elsewhere. So it's a real pain to try and add files in these sub-directory repositories.
我建议不要将您的 $HOME 目录置于 git 版本控制之下。我这样做是因为我认为我非常聪明并且希望我的 .dotfiles 处于版本控制之下。然而,git是贪婪和贪婪的。我不得不在 $HOME/.gitignore 中添加大量目录(甚至 *)。您的主目录中的其他地方是否有任何 git 存储库?我做到了。但是那些 git 存储库将使用您的 $HOME/.gitignore 作为您的全局 .gitignore 除非您在其他地方设置它。因此,尝试在这些子目录存储库中添加文件真的很痛苦。
Instead, put your important files in a separate directory. Say, ~/dotfiles. Now make that a git repository, and symlink all of your important .dotfiles to point at that new repository. Voila. Problem solved, albeit with some time invested in linking.
相反,将您的重要文件放在一个单独的目录中。说,~/dotfiles。现在将其设为 git 存储库,并将所有重要的 .dotfiles 符号链接到该新存储库。瞧。问题解决了,尽管在链接上花费了一些时间。
回答by Chan Kim
I think the best way is to set up a git repository in somewhere else (preferably in physically different disk) and adding your files in it.
You can creat empty git repo using doing
(at a safe file system, preferably different disk, like /home1)
我认为最好的方法是在其他地方(最好是在物理上不同的磁盘中)设置一个 git 存储库,并在其中添加您的文件。
您可以使用
do创建空的 git repo (在安全的文件系统,最好是不同的磁盘,如 /home1)
sudo git init --bare mynewrepos.git
sudo chown -fR myself mynewrepos.git
sudo chgrp -fR myself mynewrepos.git
now, go to your home directory, and do
现在,转到您的主目录,然后执行
git clone /home1/mynewrepos.git # now you are cloning from empty git repos.
then add your precious files to the git (git add, git commit, git push .. and all). I didn't try it for keeping my usual files under my home, but I use this method to manage my local project files (before uploading to official git repos.). But again, if you have many git managed files under your home, they might be affected by your .gitignorefiles under your home.
然后将您的宝贵文件添加到 git(git add、git commit、git push .. 等等)。我没有尝试将我常用的文件保存在我的家中,但是我使用这种方法来管理我的本地项目文件(在上传到官方 git repos 之前。)。但同样,如果你家下有很多 git 管理的文件,它们可能会受到你家下的 .gitignorefiles 的影响。
回答by MxLi
I created a solution to this by scripts that copy those files into a directory (say... $HOME/.home) and put that folder under git control:
https://github.com/MenxLi/HomeBackup
All you need to do is to call one command to backup or restore those files while using git to achieve version control.
我通过将这些文件复制到一个目录中的脚本(比如... $HOME/.home)创建了一个解决方案,并将该文件夹置于 git 控制之下:
https: //github.com/MenxLi/HomeBackup
所有你需要做的就是在使用git实现版本控制的同时调用一个命令来备份或恢复这些文件。