Git 设置最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9282998/
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-10 12:59:16  来源:igfitidea点击:

Git Setup Best practices

git

提问by rpophessagr

I was tasked with setting up Git for my office. We have several sites. I just set up a network drive for us to push our changes to. My question is: Where do I init the Git repository?

我的任务是为我的办公室设置 Git。我们有几个站点。我只是为我们设置了一个网络驱动器来推送我们的更改。我的问题是:我在哪里初始化 Git 存储库?

  1. New dir+init for each site?
  2. One init in the clean & new drive and each dir a different site?
  3. Something else better that I am missing?
  1. 每个站点的新目录+初始化?
  2. 清洁和新驱动器中的一个 init 和每个目录一个不同的站点?
  3. 我缺少的其他更好的东西?

I seek advice from any and all, especially if you've cursed out the first guy to set it up asking "WHY"?

我向所有人寻求建议,特别是如果你诅咒第一个设置它的人问“为什么”?

回答by eckes

The structure of repos is not a matter if sites(whatever you want to tell with that) but a matter of projects.

存储库的结构不是站点的问题(无论你想用它说什么),而是项目的问题

As a rule of thumb:

根据经验:

  • use ONE (bare, blessed) repo for each independent project
  • if common modules are shared, realize with submodules
  • 为每个独立项目使用一个(bare,blessed)repo
  • 如果公共模块是共享的,用子模块实现

Within each repo, structure the work with branches, and do notconfuse branches with means of organizing different software stacks: branches are used to organize the work in one repo (i.e. different development strings of ONE software). One branching model (that seems to be quite popular here at SO) is this one:

在每个回购,结构,工作树枝,做与组织不同的软件栈的手段迷惑分支:分支用于组织在一个回购的工作之一(即软件开发不同的字符串)。一种分支模型(在 SO 似乎很流行)是这样的:

enter image description here

在此处输入图片说明

Confused? Curious? Read the explanation...

使困惑?好奇的?阅读说明...

回答by jbowes

It's a matter of preference if you'd rather have all code bases in one git repo, or each in their own. That said, my preference would be one git repo per code base/site. That way you can work on a single site without having to check out the others, and you won't have to worry about changes to the other sites getting in the way of any commits you might push to any given site.

如果您希望将所有代码库都放在一个 git 存储库中,或者每个都拥有自己的代码库,这是一个偏好问题。也就是说,我的偏好是每个代码库/站点一个 git repo。这样您就可以在一个站点上工作而不必检查其他站点,而且您不必担心其他站点的更改会妨碍您可能推送到任何给定站点的任何提交。

回答by Adam Dymitruk

I would recommend gitolite if you host the repo internally. You add a central repo by simply changing the confusion file.

如果您在内部托管 repo,我会推荐 gitolite。您只需更改混淆文件即可添加中央存储库。

Unless you are doing cross-platform development, set auto crlf to false.

除非你在做跨平台开发,否则将 auto crlf 设置为 false。

Manage your branches this way:

以这种方式管理您的分支机构:

http://dymitruk.com/blog/2012/02/05/branch-per-feature/

http://dymitruk.com/blog/2012/02/05/branch-per-feature/

回答by Ivan

Basic Setup

基本设置

I would suggest you create a directory devoted to git on a server.

我建议您在服务器上创建一个专门用于 git 的目录。

I used the directory name /giton a server run by my company for work related things, and for personal use I also created a directory called /giton a private web server.

我使用/git我公司运行的服务器上的目录名称来处理工作相关的事情,为了个人使用,我还在/git私人 Web 服务器上创建了一个目录。

Inside your /git directory you can then create bare repositories for your projects

在您的 /git 目录中,您可以为您的项目创建裸存储库

ssh git@myserver
cd /git
mkdir Project7sName.git
cd Project7sName.git
git init --bare
echo "Take over the world project" > description

Add a viewer

添加查看器

Then you can install something like GitList on the server to allow software changes to be nicely view-able, e.g. deletions in red and insertions in green.

然后,您可以在服务器上安装类似 GitList 的东西,以便可以很好地查看软件更改,例如红色的删除和绿色的插入。

GitList example image

GitList 示例图像

Distributed Version Control?

分布式版本控制?

Git is a distributed version control system. So you might want to do this at several sites. The advantages are then that if at any time you lose your connection to other sites, you can continue to work uninterrupted. Also, if you have hard drive failures or any sort of similar problems, you have multiple copies of the data.

Git 是一个分布式版本控制系统。因此,您可能希望在多个站点执行此操作。这样做的好处是,如果您在任何时候失去与其他站点的连接,您都可以不间断地继续工作。此外,如果您遇到硬盘驱动器故障或任何类似问题,您将拥有多个数据副本。

You can nominate a site as being the main site if you like that people should always push to, or your working practices could be that people push to all your server sites.

如果您喜欢人们应该始终推送到的站点,您可以指定一个站点作为主站点,或者您的工作实践可能是人们推送到您的所有服务器站点。

Make use of commands like

使用类似的命令

git remote add siteA ssh://git@siteA/git/repositoryX.git

so you can then do things like

所以你可以做这样的事情

git push siteA master
git push siteB master

I have a backup script which runs daily, pulling from github, from our own gitserver, a NAS drive, a USB drive, and pushing to each of them too.

我有一个每天运行的备份脚本,它从 github、我们自己的 gitserver、一个 NAS 驱动器、一个 USB 驱动器中提取,并推送到它们中的每一个。

Doing that, there are two methods of ensuring there are multiple copies of a software change.

这样做,有两种方法可以确保软件更改有多个副本。

  1. By the user (pushing to multiple sites) and
  2. By administration (copying changes between sites).
  1. 由用户(推送到多个站点)和
  2. 通过管理(在站点之间复制更改)。

Start by setting up Git on a server and adding something like GitList to view changes nicely.

首先在服务器上设置 Git 并添加类似 GitList 的内容以很好地查看更改。

Once you properly understand the steps involved, you can repeat them at another site if you like, as and when you choose.

一旦您正确理解了所涉及的步骤,您可以根据需要在另一个站点重复这些步骤。

Import old projects?

导入旧项目?

As system administrator, you may want to import old projects into Git.

作为系统管理员,您可能希望将旧项目导入 Git。

You can do this by committing an empty repository.

您可以通过提交一个空的存储库来做到这一点。

git touch .gitignore
git add .gitignore
git commit -m Empty

And then repeatedly unzipping your archived versions of a project into a project directory (emptied apart from the git files) and committing the archived state.

然后反复将项目的存档版本解压缩到项目目录中(与 git 文件分开清空)并提交存档状态。

rm *
unzip ...
git add *
git commit -m "Archived state 2013 week 18"
git tag ArchivedState2013week18

If you commit the archived states in chronological order then your viewing software (GitList or whatever) is then likely to start showing you the software changes for historical fixes, albeit mixed together at times and incomplete at others.

如果您按时间顺序提交存档状态,那么您的查看软件(GitList 或其他任何东西)可能会开始向您显示历史修复的软件更改,尽管有时会混合在一起,有时会不完整。

Also, you will be able to use git blameto have an idea when some lines of code were introduced.

此外,您将能够git blame在引入某些代码行时有一个想法。

回答by Seth Robertson

You probably want to have a centralized server which you push changes to and clone from. This server may have one repository or many. I recommend reading http://sethrobertson.github.com/GitBestPractices/which has several sections of direct interest. Specifically picking a local workflow, a distributed workflow, and how to divide your work up into repositories.

您可能希望拥有一个集中式服务器,您可以将更改推送到该服务器并从中进行克隆。该服务器可能有一个或多个存储库。我建议阅读http://sethrobertson.github.com/GitBestPractices/,其中有几个直接感兴趣的部分。具体选择本地工作流、分布式工作流,以及如何将您的工作划分到存储库中。