Git的使用--如何安装和使用 github,让小白不在那么白 (一)(超详解)

简介

  刚开始写了关于如何将本地代码上传到github上,但是有些小伙伴们不清楚如何安装Git,这一篇就给小伙伴们普及一下Git的安装和使用。适合刚开始用git的小白,大神或者大佬请绕道。

实际项目开发中,我们经常会用一些版本控制器来托管自己的代码,今天就来总结下Git的相关用法,废话不多说,直接开写。

目的:通过Git管理github托管项目代码

一、下载安装Git

1、下载Git  官方地址为:https://git-scm.com/download/win

2、下载完之后,双击安装,点击“运行”

Please read the following important information before continuing

继续之前,请阅读以下重要信息

3、选择安装目录

 

4、选择组件

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更新

5、开始菜单目录名设置

 

6、选择Git使用的默认编辑器

 

Use the Nano editor by default

默认使用 Nano 编辑器

Use Vim (The ubiquitous text editor) as Git's default editor

使用 Vim 作为 Git 的默认编辑器

Use Notepad++ as Git's default editor

使用 Notepad++ 作为 Git 的默认编辑器

Use Visual Studio Code as Git's default editor

使用 Visual Studio Code 作为 Git 的默认编辑器

7、点击“Next”,进入下一步,进入“调整Path环境变量”窗口,

 

配置PATH环境
Use Git from Git Bash only
This is the most cautious 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 命令行工具。

Git from the Command line and also from 3rd-part software
(Recommended)This option adds only 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 PowerShell as well as any third-party software looking for Git in PATH.
这个选项被认为是安全的,因为它只向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 ”。只有在了解其含义后才使用此选项。

8、点击“Next”进入下一步,选择HTTPS传输后端

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

 9、选择"Use the OpenSSL library",点击“Next”进入下一步,配置行结束符

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”)

10、点击“Next”进入下一步,配置终端模拟器

 

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之前,其窗口不能自由调整大小,并且只允许矩形文本选择。

11、点击“Next”进入下一步,配置额外选项

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权限)。请注意,现有存储库不受此设置的影响。

12、点击“next”按钮进入额外的配置选项(Configuring experimental options),默认设置(不选择)

13、使用默认配置,直接点击“Install”开始安装

 

14、安装完成

  

 

15、检验是否安装成功

回到电脑桌面,鼠标右击如果看到有两个git单词则安装成功

 

二、Git基本工作流程

1、Git工作流程

一般工作流程如下:

  • 克隆 Git 资源作为工作目录。
  • 在克隆的资源上添加或修改文件。
  • 如果其他人修改了,你可以更新资源。
  • 在提交前查看修改。
  • 提交修改。
  • 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。

2、下图展示了Git工作流程

3、Git工作区域

我们先来理解下Git 工作区、暂存区和版本库概念

  • 工作区:就是你在电脑里能看到的目录。
  • 暂存区:英文叫stage, 或index。一般存放在 ".git目录下" 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
  • 版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。

下面这个图展示了工作区、版本库中的暂存区和版本库之间的关系:

图中左侧为工作区,右侧为版本库。在版本库中标记为 "index" 的区域是暂存区(stage, index),标记为 "master" 的是 master 分支所代表的目录树。

图中我们可以看出此时 "HEAD" 实际是指向 master 分支的一个"游标"。所以图示的命令中出现 HEAD 的地方可以用 master 来替换。

图中的 objects 标识的区域为 Git 的对象库,实际位于 ".git/objects" 目录下,里面包含了创建的各种对象及内容。

当对工作区修改(或新增)的文件执行 "git add" 命令时,暂存区的目录树被更新,同时工作区修改(或新增)的文件内容被写入到对象库中的一个新的对象中,而该对象的ID被记录在暂存区的文件索引中。

当执行提交操作(git commit)时,暂存区的目录树写到版本库(对象库)中,master 分支会做相应的更新。即 master 指向的目录树就是提交时暂存区的目录树。

当执行 "git reset HEAD" 命令时,暂存区的目录树会被重写,被 master 分支指向的目录树所替换,但是工作区不受影响。

当执行 "git rm --cached <file>" 命令时,会直接从暂存区删除文件,工作区则不做出改变。

当执行 "git checkout ." 或者 "git checkout -- <file>" 命令时,会用暂存区全部或指定的文件替换工作区的文件。这个操作很危险,会清除工作区中未添加到暂存区的改动。

当执行 "git checkout HEAD ." 或者 "git checkout HEAD <file>" 命令时,会用 HEAD 指向的 master 分支中的全部或者部分文件替换暂存区和以及工作区中的文件。这个命令也是极具危险性的,因为不但会清除工作区中未提交的改动,也会清除暂存区中未提交的改动。

  

4、向仓库中添加文件流程

  

三、Git初始化及仓库创建和操作

1、Git安装之后需要进行一些基本信息设置

  a、设置用户名:git  config -- global  user.name  '你再github上注册的用户名';

  b、设置用户邮箱:git  config -- global  user.email  '注册时候的邮箱';

注意:该配置会在github主页上显示谁提交了该文件

   c、配置ok之后,我们用如下命令来看看是否配置成功

  git config --list

注意:git  config --global 参数,有了这个参数表示你这台机器上所有的git仓库都会使用这个配置,当然你也可以对某个仓库指定不同的用户名和邮箱

2、初始化一个新的git仓库

  a、创建文件夹

    方法一:可以鼠标右击-》点击新建文件夹test1

    方法二:使用git新建:$  mkdir test1

  b、在文件内初始化git(创建git仓库)

    方法一:直接输入 $ cd test1

    方法一:点击test1文件下进去之后-》鼠标右击选择Git Bash Here->输入$ git int

3、向仓库中添加文件  

  方法一用打开编辑器新建index.html文件

  方法二:使用git命令。$  touch '文件名',然后把文件通过$ git add '文件名'添加到暂存区,最后提交操作

4、修改仓库文件

  方法一用编辑器打开index.html进行修改

  方法二:使用git命令。$  vi  '文件名',然后在中间写内容,最后提交操作

5、删除仓库文件

  方法一:在编辑器中直接把要删除的文件删除掉

  方法二:使用git删除:$ git rm '文件名',然后提交操作

四、Git管理远程仓库

1、使用远程仓库的目的备份、实现代码共享集中化管理

Git远程仓库实际上就是保持在服务器上的git仓库文件

 

 

 

五、Git克隆操作

目的:将远程仓库(github上对应的项目)复制到本地

1、代码:git clone 仓库地址

仓库地址由来如下:

2、克隆项目

3、将本地仓库同步到git远程仓库中:git push

期间出现错误的情况有: 

 a、出现提交错误

  解决:这是通过Git GUI进行提交时发生的错误,由 .git 文件夹中的文件被设为“只读”所致,将 .git 文件夹下的所有文件、文件夹及其子文件的只读属性去掉即可。

 

 b、如果出现无法同步或没有权限,解决方法如下:

  用户名和密码一定要和github上的一致。

 

posted @ 2019-06-18 14:50  北京-宏哥  阅读(3224)  评论(1编辑  收藏  举报
停止精灵球