Git 用法總結

2021-06-20 17:04:50 字數 4120 閱讀 1598

git 最基本使用方法

作為乙個git的基本使用者,從應用的角度對git的用法做個小結,這裡並不涉及git的工作原理,

1.git repo/branch的建立方法

1). 直接從server段copy乙個repo到本地,作為開發的code base,這是最常見最基本的用法

git clone

2). 在伺服器端建立乙個新的repo,方便多人合作開發。

step1.在server上建立乙個空的project :git init --bare project.git

step2.新增第乙個patch: git add readme.txt

git commit -m "first commit"

如果想把自己local的某個branch推送當前 remote server 上去: 

step1.將server的project git作為remote的source,命名為origin

git remote add origin

step2. 將當前repo的branch推送到server的repo中去,origin表示remote server的名稱,master表示remote server上的master branch

git push origin master

2. git repo基本配置:

git config --golbal user.name "your name"

git config --global user.email "your email"

git config --global core.editor vim              //預設編輯器

git config --global commit.template $home/.gitmessage.txt //commit message 預設格式,例如 signed-off-by: li, bala

git config --global color.ui true    //預設顏色

有些資訊在生成commit的過程中會用到,可以把這些寫到指令碼裡面,機器啟動後自行載入

3. branch常見操作:

0). 檢視local已有的branch

git branch,如果想看remote server端branch,加上 -r引數

1). 建立乙個新local branch: 

git branch new_branch, 從當前工作branch上複製出乙個新的branch,命名為new_branch。

2). 切換到另外乙個local branch

git checkout branch_name

1). 建立新的branch 去跟蹤remote server branch, 方便以後patch pull和push

git checkout -b local_branch_name origin/remote_branch_name  

2). 強制用本地分支覆蓋遠端分支(或者提交patch,不用-f)。

git push origin 本地分支名:遠端分支名 -f  

3). 刪除遠端分支

git push origin :遠端分支                  

4. patch操作

1). 生成commit過程

git add file.c (如果要去掉行尾換行,請在vim檔案時候執行:% s/\s\+$//g)

git commit -s //編寫commit 資訊

2).  修改commit message

git commit --amend

3). 儲存patch

git format-patch -i //i表示要dump的patch 個數

如果只是簡單臨時儲存和打回patch只需用:

git diff &> diff.patch

patch -p1 < diff.patch

4).應用patch

git am ***.patch 

如果打patch失敗了,繼續執行下面步驟:

這個過程會產生.rej檔案,按照該檔案去修改patch沒打上的檔案

edit files

git add file.c

git am --resolved

5. 修改做好的提交的patch

1). git rebase  -i comment_id

2). edit

3). git rebase --resolved

4). git rebase --continue

6. 檢視patch log

1). git log 檢視這個專案的log紀錄

1). git log --pretty=oneline file.c //檢視和某檔案相關的所有log資訊,每個只顯示一行

2). git blame file.c//顯示檔案的沒一行的最新的log

後面兩操作常用於追蹤檔案的修改過程

7. 傳送patch mail

先生成patch:

git format-patch --subject-prefix="version **" -1

把patch傳送到mailist, 所有訂閱該mailist都會受到這個patch,方便patch review

git send-email  --smtp-server=smtp.intel.com [email protected]  *.patch

8. 檢視已經新增但尚未commit的內容

git diff --cached

9. 檢視git歷史操作記錄

git reflog 可以檢視所有分支的所有操作記錄(包括(包括commit和reset的操作),包括已經被刪除的commit記錄,git log則不能察看已經刪除了的commit記錄

具體乙個例子,假設有三個commit, git st:

commit3: add test3.c

commit2: add test2.c

commit1: add test1.c

如果執行git reset --hard head~1則 刪除了commit3,如果發現刪除錯誤了,需要恢復commit3,這個時候就要使用git reflog

head@: head~1: updating head

63ee781 head@: commit: test3:q

紅色加粗的即是被刪除了的 commit3,執行git log則沒有這一行記錄

可以使用git reset --hard63ee781將紅色記錄刪除,則恢復了cmmit3,執行git log後可以看到:

commit3: add test3.c

commit2: add test2.c

commit1: add test1.c

這裡也可以使用另外一種方法來實現:git cherry-pick63ee78

10. 檢視已經新增但尚未commit的內容

git diff --cached

11. 在server和client端從零開始建立專案

伺服器端:

a) server端搭建http伺服器:/var/www/html (或者更加複雜的gitlab)

b) 建立空的repo:  cd /var/www/html/; git init --bare testrepo.git

客戶端:

a)建立空的repo和commit

mkdir testrepo

cd testrepo

git init

touch readme

git add readme

git commit -m 'first commit'

b) 繫結到遠端repo

git remote add origin

c) 提交分支到遠端repo

git push -u origin master

git用法總結

git的學習,有兩個很好的資源,乙個是廖雪峰的官網,這個真是淺顯易懂,另乙個是codecademy,想深入學習的可以請移步過去,這裡僅對常用的命令進行總結。git config global user.name 此處填寫你的使用者名稱 git config global user.email 你的e...

Git用法總結

git 是乙個開源的分布式版本控制系統,用以敏捷 高效地處理專案版本管理。git配置 安裝git sudo apt get install git 配置使用者資訊 git config global user.name name 配置郵箱資訊 git config global user.email...

Git 用法總結,持續更新

從當前分支切換到本地已有目標分支git chekcout local branch從當前分支建立新本地分支並切換到此分支git checkout b new branch從branch name中檢出file name到當前分支git checkout branch name file name從c...