Git环境搭建

文章来源:Git环境搭建
1 Git介绍
​ Git是一个免费的开源的 分布式版本控制系统,旨在快速,高效地处理从小型到大型项目的所有事务。Git 易于学习, 占地面积小,具有闪电般快速的性能。它超越了Subversion,CVS,Perforce和ClearCase等SCM工具,具有廉价本地分支,便捷的临时区域和 多个工作流程等功能。

2 安装
2.1 下载
Git支持Mac、windows、Linux/Unix环境运行,进入下载页面:https://git-scm.com/downloads

1543363728007

2.2 windows版本安装
​ 下载windows版本安装程序,本教程下载 Git-2.19.2-64-bit.exe版本。

第一步:阅读重要信息
1543364618237

第二步:选择安装路径
1543368001808

第三步:选择组件
默认即可,下一步。

1543364699351

选项如下:

Additional icons  附加图标

​ On the Desktop  在桌面上

Windows Explorer integration  Windows资源管理器集成(集成鼠标右键菜单)

Git Bash Here 命令行

Git GUI Here 图形界面
Git LFS (Large File Support) 大文件支持

Associate .git* configuration files with the default text editor将 .git 配置文件与默认文本编辑器相关联

Associate .sh files to be run with Bash将.sh文件关联到Bash运行

Use a TrueType font in all console windows在所有控制台窗口中使用TrueType字体

Check daily for Git for Windows updates每天检查Git是否有Windows更新

第四步:选择Git使用的默认编辑器
1543365473701

Use Vim (The ubiquitous text editor) as Git’s default editor使用 Vim 作为 Git 的默认编辑器

第五步:配置PATH环境
默认选择。

1543365456474

Use Git from Git Bash only

This is the safest choice as your PATH will not be modified at all.You will only be able to use the Git command line tools form Git Bash.

这是最安全的选择,因为您的PATH根本不会被修改。您只能使用 Git Bash 的 Git 命令行工具。

Use Git from the Windows Command Prompt

This option is considered safe as it only adds some minimal Git wrappers to your PATH to avoid cluttering your environment with optional Unix tools . You will be able to use Git from both Git Bash and the Windows Command Prompt.

这个选项被认为是安全的,因为它只向PATH添加一些最小的 Git包,以避免使用可选的Unix工具混淆环境。 您将能够从 Git Bash 和 Windows 命令提示符中使用 Git。

Use Git and optional Unix tools from the Windows Command Prompt

从Windows命令提示符使用Git和可选的Unix工具

Both Git and the optional Unix tools will be added to you PATH

Git和可选的Unix工具都将添加到您计算机的 PATH 中

Warning:This will override Windows tools like “find and sort”.Only use this option if you understand the implications.

警告:这将覆盖Windows工具,如 “ find 和 sort ”。只有在了解其含义后才使用此选项。

第六步:选择HTTPS传输后端
默认选择

1543365485145

Use the OpenSSL library

使用 OpenSSL 库

Server certificates will be validated using the ca-bundle.crt file.

服务器证书将使用ca-bundle.crt文件进行验证。

Use the native Windows Secure Channel library

使用本地 Windows 安全通道库

Server certificates will be validated using Windows Certificate Stores.This option also allows you to use your company’s internal Root CA certificates distributed e.g. via Active Directory Domain Services.

服务器证书将使用Windows证书存储验证。此选项还允许您使用公司的内部根CA证书,例如, 通过Active Directory Domain Services 。

第七步:配置行结束转换
默认选择

1543365499622

Checkout Windows-style,commit Unix-style line endings

Git will convert LF to CRLF when checking out text files.When committing text files,CRLF will be converted to LF .For cross-pltform projects,this is the recommended setting on Windows (“core.autocrlf” is set to “true”)

在检出文本文件时,Git会将LF转换为CRLF。当提交文本文件时,CRLF将转换为LF。 对于跨平台项目,这是Windows上推荐的设置(“core.autocrlf”设置为“true”)

Checkout as-is , commit Unix-style line endings

Git will not perform any conversion when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects,this is the recommended setting on Unix (“core.autocrlf” is set to “input”)

在检出文本文件时,Git不会执行任何转换。 提交文本文件时,CRLF将转换为LF。 对于跨平台项目,这是Unix上的推荐设置 (“core.autocrlf”设置为“input”)

Checkout as-is,commit as-is

Git will not perform any conversions when checking out or committing text files.Choosing this option is not recommended for cross-platform projects (“core.autocrlf”is set to “false”)

在检出或提交文本文件时,Git不会执行任何转换。对于跨平台项目,不推荐使用此选项(“core.autocrlf”设置为“false”)

第八步:配置终端模拟器以与 Git Bash 一起使用
默认选择

1543365513378

Use MinTTY (the default terminal of MSYS2)

Git Bash will use MinTTY as terminal emulator,which sports a resizable window,non-rectangular selections and a Unicode font. Windows console programs (such as interactive Python) must be launched via ‘winpty’ to work in MinTTY.

Git Bash将使用MinTTY作为终端模拟器,该模拟器具有可调整大小的窗口,非矩形选区和Unicode字体。 Windows控制台程序(如交互式Python)必须通过’winpty’启动才能在MinTTY中运行。

Use Windows’ default console window

Git will use the default console window of Windows (“cmd.exe”),which works well with Win32 console programs such as interactive Python or node.js , but has a very limited default scroll-back,needs to be configured to use aUnicode font in order to display non-ASCII characters correctly,and prior to Windows 10 its windows was not freely resizable and it only allowed rectangular text selections.

Git将使用Windows的默认控制台窗口(“cmd.exe”),该窗口可以与Win32控制台程序(如交互式Python或node.js)一起使用,但默认的回滚非常有限,需要配置为使用unicode 字体以正确显示非ASCII字符,并且在Windows 10之前,其窗口不能自由调整大小,并且只允许矩形文本选择。

第九步:配置额外的选项
1543365528633

Enable file system caching

启用文件系统缓存

File system data will be read in bulk and cached in memory for certain operations (“core.fscache” is set to “true”). This provides a significant performance boost.

文件系统数据将被批量读取并缓存在内存中用于某些操作(“core.fscache”设置为“true”)。 这提供了显着的性能提升。

Enable Git Credential Manager

启用Git凭证管理器

The Git Credential Manager for Windows provides secure Git credential storage for Windows,most notably multi-factor authentication support for Visual Studio Team Services and GitHub. (requires .NET framework v4.5.1 or or later).

Windows的Git凭证管理器为Windows提供安全的Git凭证存储,最显着的是对Visual Studio Team Services和GitHub的多因素身份验证支持。 (需要.NET Framework v4.5.1或更高版本)。

Enable symbolic links

启用符号链接

Enable symbolic links (requires the SeCreateSymbolicLink permission).Please note that existing repositories are unaffected by this setting.

启用符号链接(需要SeCreateSymbolicLink权限)。请注意,现有存储库不受此设置的影响。

​ 完成安装之后,可看到邮件菜单:

1543366059329

可以使用命令行的 git 工具(已经自带了 ssh 客户端)了,另外还有一个图形界面的 Git 项目管理工具。

3 Sourcetree
3.1 介绍
​ Sourcetree是一个免费的可视化界面的项目版本控制软件,适用于git项目管理,它简化了您与Git存储库交互的方式,因此您可以专注于编码。通过Sourcetree的简单Git GUI可视化和管理您的存储库。

官方网站:https://www.sourcetreeapp.com/

3.2 安装
下载windows版本:

https://product-downloads.atlassian.com/software/sourcetree/windows/ga/SourceTreeSetup-3.0.8.exe

第一步:安装.NET 4.5
Sourcetree运行依赖.NET 4.5环境,需要先安装此环境。

1543368150847

如果报错如下,需要手动下载,进入https://www.microsoft.com/zh-CN/download/confirmation.aspx?id=42642

1543368882215

第二步:安装Sourcetree
​ 选择安装路径安装Sourcetree

​ 完成需要注册Bitbucket账号,Bitbucket是一个和Github类似的Git服务平台。

第三步:SSH公钥
Sourcetree相当于一个客户端,它连接Git服务器完成代码管理。

通过在Git服务器配置SSH公钥可以实现Sourcetree客户端无需输入密码即可与Git服务器连接。

1、在开发者电脑上生成SSH公钥

git命令窗口(linux),执行下边命令

ssh-keygen -t rsa -C ‘xxxxx@company.com’
找到生成的公钥和私钥

C:\Users\Administrator.ssh\id_rsa.pub
C:\Users\Administrator.ssh\id_rsa

2、查询到公钥内容填充到gitLab或gitee。

下边以gitee举例子

登录gitee,配置SSH公钥

1543379607042

公钥与提交者
1、SSH公钥仅适用于通过ssh协议连接git服务,ssh公钥是客户端生成,将公钥配置在git服务上,用于客户端请求git服务验证身份。

2、前边配置的git用户名称和用户email则是标识提交者的名称,提交者的名称和生成公钥指定的名称不同也可以提交的(在GitHub和gitee上测试通过)。

3.3 基本应用
3.3.1 创建本地仓库
1、拿到Git服务端ssh仓库地址

git@gitee.com:mrt_0607/xc-govern-center.git
2、创建本地仓库

打开Sourcetree,创建本地仓库

1543380146909

注意:F:\develop\git\xc-govern-center 目录当前不存在

点击“创建”Sourcetree创建 F:\develop\git\xc-govern-center目录。

创建成功进入控制台

1543380237489

此时查看F:\develop\git\xc-govern-center目录,本地仓库创建成功:

1543380274355

3.3.2 拉取代码
点击拉取,将远程仓库的代码拉取到本地仓库

1543380329010

1543380617109

点击刷新,选择远端的分支,点击确定。

1543380653878

拉取成功

1543380691317

3.3.3 提交代码
设置远程仓库:

将第一步获取Git服务器远程仓库地址设置在Sourcetree中。

点击“设置”:

1543380426097

1543380458573

点击添加

1543380500238

设置成功:

1543380525107

设置用户名和邮箱地址

可以使用全局也可以定义该仓库的邮箱地址

1543380565447

这里使用SSH 协议提交代码,前边已经在Git服务器设置了SSH公钥。

如果采用HTTP/HTTPS协议提交代码,则需要输入账号和密码

1543391275278

提交后账号和密码将保存在Sourcetree,如果要更换账号和密码则需要删除保存的账号和密码

1543391317412

3.4 分支
3.4.1 常用分支
1543399605302

master分支:

​ 即主分支,记录着项目的全部生命周期,代码测试通过发布新版本需在此分支上打上标签。 develop分支:

​ 即开发分支,开发人员基于此分支创建feature分支,团队中所有人都在这个分支上开发,需要与远程同步。

feature分支:

​ 开发分支,由开发人员在本地创建自己的开发分支,如果此功能只有自己一个人开发则不需要推送到远程,否则需要推送到远程由多人协作开发。此分支开发完毕需要合并到develop上。 release分支:

​ 即发布分布分支,从develop创建,发布分支名称为release-1.1,测试和修改bug在此分支进行,避免进行大功能开发。测试完毕将此分支合并到master,同时在master添加标签,标记版本号。

​ 注意:此分支一定要实时合并到develop。

hotfixes分支:

​ 即bug分支,此分支工作属于计划外的工作,用于解决生产环境上的棘手问题,由开发人员修改bug后合并到master上和develop上,一般不需要推送远程仓库中。

3.4.2 新建分支
​ 新建分支需要基于某个现有分支创建,可基于现有分支当前的commit创建也可选择一个commit创建。

1543401756327

​ 问题:

​ 如上图,如果要基于远程的develop分支创建本地develop分支,可以如下操作。

​ 1、先基于master创建develop分支

​ 2、再拉取远程分支develop到本地develop分支。

3.4.3 删除分支
使用Sourcetree删除分支,移动master分支,删除其它分支。

3.4.4 合并分支
案例:将feature分支合并到develop

1、选中目标分支develop分支

2、选择要合并的分支feature分支

3.5 标签
1、切换到master分支

2、在历史记录上点击“标签”,输入标签名称

1543402546720

点击“添加标签” ,将推荐标签及master更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值