簡介
實際項目開發中,我們經常會用一些版本控制器來托管自己的代碼,今天就來總結下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上的一致。