1. 程式人生 > >Git的使用--如何安裝和使用 github讓小白不在那麼白 (一)(超詳解)

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上的一致。