1. 程式人生 > 遊戲 >科幻RPG《Mechajammer》1.1版本更新25日上線

科幻RPG《Mechajammer》1.1版本更新25日上線

Git

git 檔案的三種狀態(區)

工作區 叫 workArea 又叫work Directory

暫存區 叫 Stage 又叫 Index

graph LR subgraph 本地倉庫 Repository end subgraph 暫存區 Stage-->|git commit|Repository end subgraph 工作區 workArea-->|git add|Stage Stage-->|git rm --cache|workArea Stage-->|git reset HEAD|workArea end

git 常用命令

// 建立或獲得版本庫
git init //建立初始化一個庫
git clone //從遠端克隆一個庫到本地

// 版本管理
git add //將工作區中修改的檔案推入暫存區
git commit // 將暫存區的檔案推入本地版本庫中
git rm //

// 檢視資訊
git help
git log
git diff

// 遠端協作
git pull
git push

配置名稱和郵箱

// 有三個地方可以配置
1. /etc/gitconfig (幾乎不會使用,全域性的,針對計算機所有使用者)
	git config --system
2. ~/.gitconfig(針對當前使用者,常用)
	git config --global
3. .git/config(針對當前倉庫,常用)
	git config --local
	
//優先順序 3>2>1

例子

git config --local user.name "張三"
git config --local user.email "[email protected]"

git checkout 用法

//放棄工作區中這個檔案的修改,(丟棄了就找不回來了)
git checkout -- <file>...
//將 暫存區 的這個檔案的修改推回到 工作區
git rm --cache <file>

//從 本地庫 刪除一個檔案,並將訊息推到 暫存區,等待使用者處理(commit或reset)
git rm <file> //和直接使用 rm 刪除十分類似,只是後續流程會不一樣

// 將暫存區的這個檔案的修改推回到工作區
// 這裡是將這個檔案在本地庫的Head位置和Stage區做對比,將差異推回工作區
// 讓Stage區與本地庫Head位置保持一致
git reset HEAD <file>

git mv 相當於 mv,但簡化了一些流程

修改上次提交的commit message

git commit --amend -m "修正的提交訊息"

commit -id 是SHA1演算法算出來的值

分支操作

//檢視本地分支
git branch
//檢視所有分支,包括遠端分支
git branch -a

// 刪除指定分支(當前分支不能刪除,有未提交內容的分支不能刪除)
git branch -d
//強行刪除(未合併的分支刪除需要強行刪)
git branch -D

// 建立分支但不切換到分支
git branch branch_name
// 建立並切換到分支 
git checkout -b branch_name

//分支重新命名
git checkout -m master master_newName

//將xx_branch的變更合入到本分支
git merge xx_branch

每一個commit都有一個屬於自己的commit-id,同時還有一個parent成員,parent成員指向上一個commit,這樣一個commit的鏈組成了一個branch

HEAD始終指向分支,可以在.git/HEAD檔案中檢視到當前說指向的分支

// 目錄地址 .git
➜  .git git:(master) cat HEAD
ref: refs/heads/master

get merge

git merge

git 在merge時,預設使用fast forward

Fast Forward 就是指標移動

  1. 兩個分支歸於一個commit
  2. 沒有分支資訊

==不使用fast-forward

git merge --no-ff

會多建立一個合併分支的commit資訊(多一個commit)

衝突樣例

<<<<<<<(也能表示是本地內容), HEAD代表當前分支的內容;後面的(>>>>>>>)代表將要合併進來的內容

<<<<<<< HEAD
hello master
=======
hello dev
>>>>>>> dev

解決完衝突 先 git add 衝突檔案,再commit

這裡會合併其他分支的commit 和一個新的解決衝突的commit

git log

// 顯示commit日誌
git log

// 圖形式顯示commit日誌
git log --graph

// 簡要圖形式顯示日誌 
// --abbrev-commit 指 僅顯示 SHA-1 的前幾個字元,而非所有的 40 個字元
git log --graph --pretty=online --abbrev-commit

版本穿梭 (即在多個commit之間來回遊走【回退/前進】)

git add . + git commit -m "" 等於 git commmit -am ""

回退命令

  1. 回退到上兩次commit
git reset --hard HEAD^^
  1. 回退到上N次commit
git reset --hard HEAD~n
  1. 到commit-id處【可前進/可後退命令】
git reset --hard SHA1(前五位及以上)

檢視所有的提交commit記錄

git reflog

reflog 檢視所有記錄操作(最新的操作在最上面,最老的操作在最下面),可以幫助我們實現”後悔“操作

//撤銷工作區file中的修改
git checkout file

//撤銷暫存區file的修改,並推到工作區中
git checkout -- file

checkout 遊離(時光穿梭)

git checkout SHA1值

樣例:

➜  MyCPPProject git:(master) git checkout 8d8a85e6
Note: checking out '8d8a85e6'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

//翻譯:你現在處於遊離狀態,你可以四處看看,做一些修改和提交,且可以取消任何你在當前狀態所做的提交(在不影響其他分支的情況下)

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

//翻譯:如果你想建立一個分支來保留你建立的提交,你可以再吃將-b 和checkout 一起執行

  git checkout -b <new-branch-name>

HEAD is now at 8d8a85e 修復一個bug,改變subString成員函式

此時如果檢視commit日誌,發現日誌最高處為8d8a85e

且檢視.git/HEAD檔案發現

➜  MyCPPProject git:(8d8a85e) cat .git/HEAD
8d8a85e6f122e96b16b80b55148b01fda45ec19f

說明HEAD離開了指向分支的位置,不再指向分支了,而是指向某個commit

此狀態即為遊離狀態

想回到某分支時

git checkout 分支名

git stash 用法

儲存現場

如果某個功能還沒有開發完畢,可以 git stash

git stash 
git stash list
gitt stash pop