文章出處

Git命令行介紹和使用說明(持續更新)

 


參見:《
Git 中文簡體教程》 

一、

命令“git”或者“git help”查詢常用命令

 

 

 

add】:

git add”——不但是用來添加不在版本控制中的新文件,也用于添加已在版本控制中但是剛修改過的文件在這兩種情況下, Git都會獲得當前文件的快照并且把內容暫存(stage)到索引中,為下一次commit做好準備。Git跟蹤的是內容不是文件。

bisect

branch】:
git branch”——會得到當前倉庫中存在的所有分支列表。其中,“master”分支是Git系統默認創建的主分支。星號(“*”)標識了你當工作在哪個分支下。

git branch -d branchname”——git branch -d只能刪除那些已經被當前分支的合并的分支。

git branch –D branchname”——git branch –D可以強制刪除某個分支的話就用。

 

”git branch -人“——查看所有分支。

 

checkout】:

 

git checkout brancename”——切換到“brancename”分支。

clone】:

 

注:Clone一個倉庫。為了得一個項目的拷貝(copy),我們需要知道這個項目倉庫的地址(Git URL). Git能在許多協議下使用,所以Git URL可能以ssh://, http(s)://, git://,或是只是以一個用戶名(git 會認為這是一個ssh 地址)為前輟有些倉庫可以通過不只一種協議來訪問,例如,Git本身的源代碼你既可以用 git:// 協議來訪問:git clone git://git.kernel.org/pub/scm/git/git.git

也可以通過http 協議來訪問:git clone http://www.kernel.org/pub/scm/git/git.git

git://協議較為快速和有效,但是有時必須使用http協議,比如你公司的防火墻阻止了你的非http訪問請求.如果你執行了上面兩行命令中的任意一個,你會看到一個新目錄: 'git',它包含所有的Git源代碼和歷史記錄.

在默認情況下,Git會把"Git URL"里目錄名的'.git'的后輟去掉,做為新克隆(clone)項目的目錄名: (例如git clone

http://git.kernel.org/linux/kernel/git/torvalds/linux-2.6.git 會建立一個目錄叫'linux-2.6')

 

“git clone –bare test test.git”——選項bare的意思就是用現有的被控制倉庫創建一個GIT倉庫。取代了創建目錄和初始化兩步,而且使得新生成的目錄自身就是倉庫目錄,而這個倉庫目錄中是沒有像工作目錄中的源文件的,只有鏡像文件(即跟原來的.git目錄下的內容差不多)。這樣的目錄一般就是服務器存儲的內容,可以稱之為鏡像目錄。bare后的參數一是本地目錄,參數二是遠程(也可是本地)鏡像目錄,這樣就可以是想將本地目錄test推送到服務器目錄。

--bare

Make a bare GIT repository. That is, instead of creating  and placing the administrative files in /.git, make the itself the $GIT_DIR. This obviously implies the -n because there is nowhere to check out the working tree. Also the branch heads at the remote are copied directly to corresponding local branch heads, without mapping them to refs/remotes/origin/. When this option is used, neither remote-tracking branches nor the related configuration variables are created.

 

“git clone test.git test”——與“git clone --bare”的過程相反,就是利用遠程鏡像來恢復本地源文件。例子的意思就是從遠程鏡像test.git中克隆出源文件到本地目錄test中。

 

 

commit】:

 

git commit”——把暫存區中記錄的要提交的內容提交到Git的對象數據庫。這會提示你輸入本次修改的注釋,完成后就會記錄一個新的項目版本。

 

git commit –a”這會自動把所有內容被修改的文件(不包括新創建的文件)都添加到索引中,并且同時把它們提交。

這里有一個關于寫commit注釋的技巧和大家分享:commit注釋最好以一行短句子作為開頭,來簡要描述一下這次commit所作的修改(最好不要超過50個字符);然后空一行再把詳細的注釋寫清楚。這樣就可以很方便的用工具把commit注釋變成email通知,第一行作為標題,剩下的部分就作email的正文。

 

“git commit -m”——后跟參數表示要接提交內容說明的信息。

 

config】:

git config –username ”——

git config --get remote.origin.url”——查看本地配置的遠程倉庫地址。

“git config --global core.editor "notepad"”——將Git默認的Vi編輯器換成記事本,如果想再改成Vim則使用下面命令行:

git config --global core.editor "\"C:/Program Files (x86)/GitExtensions/GitExtensions.exe\" fileeditor"

 

diff】:

 

git diff ——比較自己項目中任意兩個版本的差異,或是用來查看別人提交進來的新分支。顯示在當前的工作目錄里的,沒有staged(添加到索引中),且在下次提交時不會被提交的修改。如有合并的文件有沖突,輸入此命令就可以查看當前有哪些文件產生了沖突。

git diff test”——這會顯示你當前工作目錄與另外一個叫'test'分支的差別。你也以加上路徑限定符,來只比較某一個文件或目錄。

git diff master..test”——這條命令只顯示兩個分支間的差異,如果你想找出 git diff master...test”——這條命令顯示‘master’,‘test’的共有父分支和'test'分支之間的差異,用3‘.'來取代前面的兩個'.' 

git diff --cached”——看看哪些文件將被提交(commit)。顯示你當前的索引和上次提交間的差異;這些內容在不帶"-a"參數運行 "git commit"命令時就會被提交。

git diff HEAD”——上面這條命令會顯示你工作目錄與上次提交時之間的所有差別,這條命令所顯示的 內容都會在執行"git commit -a"命令時被提交。

git diff HEAD -- ./lib”——上面這條命令會顯示你當前工作目錄下的lib目錄與上次提交之間的差別(或者更準確的 說是在當前分支)

git diff --stat”——不查看每個文件的詳細差別,只是統計一下有哪些文件被改動,有多少行被改動。

 

 

fetch

grep

init】:

 

git init”——初始化一個新的倉庫,將它置于Git的版本控制管理之下。

 

log】:

git log”——可以顯示所有的提交。

$ git log test..master # commits reachable from master but not test

$ git log master..test # commits reachable from test but not master

$ git log master...test # commits reachable from either test or master, but not both

$ git log --since="2 weeks ago" # commits from the last 2 weeks

$ git log Makefile # commits that modify Makefile

$ git log fs/ # commits that modify any file under fs/

$ git log -S'foo()' # commits that add or remove any file data

# matching the string 'foo()'

$ git log --no-merges # dont show merge commits

當然你也可以組合上面的命令選項;下面的命令就是找出所有從"v2.5“開始在fs目錄下的所有Makefile的修改。

$ git log v2.5.. Makefile fs/

Git會根據git log命令的參數,按時間順序顯示相關的提交(commit)

 

git log -p”——顯示補丁(patchs)

git log --stat”——日志統計。如果用--stat選項使用“git log”,它會顯示在每個提交(commit)中哪些文件被修改了這些文件分別添加或刪除了多少行內容。

 

git log --pretty”——格式化日志。你可以按你的要求來格式化日志輸出。“—pretty”參數可以使用若干表現格式,如“oneline”:

$ git log --pretty=oneline

a6b444f570558a5f31ab508dc2a24dc34773825f dammit, this is the second time this has reverted

49d77f72783e4e9f12d1bbcacc45e7a15c800240 modified index to create refs/heads if it is not

9764edd90cf9a423c9698a2f1e814f16f0111238 Add diff-lcs dependency

e1ba1e3ca83d53a2f16b39c453fad33380f8d1cc Add dependency for Open4

0f87b4d9020fff756c18323106b3fd4e2f422135 merged recent changes: * accepts relative alt pat

f0ce7d5979dfb0f415799d086e14a8d2f9653300 updated the Manifest file

或者你也可以使用 'short' 格式:

$ git log --pretty=short

commit a6b444f570558a5f31ab508dc2a24dc34773825f

Author: Scott Chacon<schacon@gmail.com>

dammit, this is the second time this has reverted

commit 49d77f72783e4e9f12d1bbcacc45e7a15c800240

Author: Scott Chacon<schacon@gmail.com>

modified index to create refs/heads if it is not there

commit 9764edd90cf9a423c9698a2f1e814f16f0111238

Author: Hans Engel<engel@engel.uk.to>

Add diff-lcs dependency

你也可用‘medium','full','fuller','email' ‘raw'. 如果這些格式不完全符合你的相求, 你也可以用‘--pretty=format'參數(參見:git log)來創建你自己的"格式“.

$ git log --pretty=format:'%h was %an, %ar, message: %s'

a6b444f was Scott Chacon, 5 days ago, message: dammit, this is the second time this has re

49d77f7 was Scott Chacon, 8 days ago, message: modified index to create refs/heads if it i

9764edd was Hans Engel, 11 days ago, message: Add diff-lcs dependency

e1ba1e3 was Hans Engel, 11 days ago, message: Add dependency for Open4

0f87b4d was Scott Chacon, 12 days ago, message: merged recent changes:

另一個有趣的事是:你可以用'--graph'選項來可視化你的提交圖(commit graph),就像下面這樣:

$ git log --pretty=format:'%h : %s' --graph

* 2d3acf9 : ignore errors from SIGCHLD on trap

* 5e3ee11 : Merge branch 'master' of git://github.com/dustin/grit

|\

| * 420eac9 : Added a method for getting the current branch.

* | 30e367c : timeout code and tests

* | 5a09431 : add timeout protection to grit

* | e1193f8 : support for heads with slashes in them

|/

* d6016bc : require time for xmlschema

它會用ASCII字符來畫出一個很漂亮的提交歷史(commit history)線。

 

日志排序

你也可以把日志記錄按一些不同的順序來顯示。注意,git日志從最近的提交(commit)開始,并且從這里開始向它們父分支回溯。然而git歷史可能包括多個互不關聯的開發線路,這樣有時提交(commits)顯示出來就有點雜亂。

如果你要指定一個特定的順序,可以為git log命令添加順序參數(ordering option).

按默認情況,提交(commits)會按逆時間(reverse chronological)順序顯示。

但是你也可以指定‘--topo-order'參數,這就會讓提交(commits)按拓樸順序來顯示(就是子提交在它們的父提交前顯示). 如果你用git log命令按拓樸順序來顯示git倉庫的提交日志,你會看到開發線"(development lines)都會集合在一起。

$ git log --pretty=format:'%h : %s' --topo-order --graph

* 4a904d7 : Merge branch 'idx2'

|\

| * dfeffce : merged in bryces changes and fixed some testing issues

| |\

| | * 23f4ecf : Clarify how to get a full count out of Repo#commits

| | * 9d6d250 : Appropriate time-zone test fix from halorgium

| | |\

| | | * cec36f7 : Fix the to_hash test to run in US/Pacific time

| | * | decfe7b : fixed manifest and grit.rb to make correct gemspec

| | * | cd27d57 : added lib/grit/commit_stats.rb to the big list o' files

| | * | 823a9d9 : cleared out errors by adding in Grit::Git#run method

| | * | 4eb3bf0 : resolved merge conflicts, hopefully amicably

| | |\ \

| | | * | d065e76 : empty commit to push project to runcoderun

| | | * | 3fa3284 : whitespace

| | | * | d01cffd : whitespace

| | | * | 7c74272 : oops, update version here too

| | | * | 13f8cc3 : push 0.8.3

| | | * | 06bae5a : capture stderr and log it if debug is true when running commands

| | | * | 0b5bedf : update history

| | | * | d40e1f0 : some docs

| | | * | ef8a23c : update gemspec to include the newly added files to manifest

| | | * | 15dd347 : add missing files to manifest; add grit test

| | | * | 3dabb6a : allow sending debug messages to a user defined logger if provided; tes

| | | * | eac1c37 : pull out the date in this assertion and compare as xmlschemaw, to avoi

| | | * | 0a7d387 : Removed debug print.

| | | * | 4d6b69c : Fixed to close opened file description.

你也可以用'--date-order'參數,這樣顯示提交日志的順序主要按提交日期來排序。這個參數和'--topo-order'有一點像,沒有父分支會在它們的子分支前顯示,但是其它的東東還是按交時間來排序顯示。你會看到"開發線"(development lines)沒有集合一起,它們會像并行開發(parallel development)一樣跳來跳去的:

$ git log --pretty=format:'%h : %s' --date-order --graph

* 4a904d7 : Merge branch 'idx2'

|\

* | 81a3e0d : updated packfile code to recognize index v2

| * dfeffce : merged in bryces changes and fixed some testing issues

| |\

| * | c615d80 : fixed a log issue

|/ /

| * 23f4ecf : Clarify how to get a full count out of Repo#commits

| * 9d6d250 : Appropriate time-zone test fix from halorgium

| |\

| * | decfe7b : fixed manifest and grit.rb to make correct gemspec

| * | cd27d57 : added lib/grit/commit_stats.rb to the big list o' file

| * | 823a9d9 : cleared out errors by adding in Grit::Git#run method

| * | 4eb3bf0 : resolved merge conflicts, hopefully amicably

| |\ \

| * | | ba23640 : Fix CommitDb errors in test (was this the right fix?

| * | | 4d8873e : test_commit no longer fails if you're not in PDT

| * | | b3285ad : Use the appropriate method to find a first occurrenc

| * | | 44dda6c : more cleanly accept separate options for initializin

| * | | 839ba9f : needed to be able to ask Repo.new to work with a bar

| | * | d065e76 : empty commit to push project to runcoderun

* | | | 791ec6b : updated grit gemspec

* | | | 756a947 : including code from github updates

| | * | 3fa3284 : whitespace

| | * | d01cffd : whitespace

| * | | a0e4a3d : updated grit gemspec

| * | | 7569d0d : including code from github updates

最后,你也可以用 ‘--reverse'參數來逆向顯示所有日志。

 

merge】:

git merge branchname”——這個命令把分支"branchname"合并到了當前分支里面。如有沖突(沖突--同一個文件在遠程分支和本地分支里按不同的方式被修改了);那么命令的執行輸出就像下面一樣:

$ git merge next

100% (4/4) done

Auto-merged file.txt

CONFLICT (content): Merge conflict in file.txt

Automatic merge failed; fix conflicts and then commit the result.

在有問題的文件上會有沖突標記,在你手動解決完沖突后就可以把此文件添加到索引(index)中去,用“git commit”命令來提交,就像平時修改了一個文件一樣。

如果你用gitk來查看commit的結果,你會看到它有兩個父分支:一個指向當前的分支,另外一個指向剛才合并進來的分支。

解決合并中的沖突

如果執行自動合并沒有成功的話,git會在索引和工作樹里設置一個特殊的狀態, 提示你如何解決合并中出現的沖突。

有沖突(conflicts)的文件會保存在索引中,除非你解決了問題并且更新了索引,否則執行git commit都會失敗:

$ git commit

file.txt: needs merge

如果執行git status會顯示這些文件沒有合并(unmerged),這些有沖突的文件里面會添加像下面的沖突標識符:

<<<<<<< HEAD:file.txt

Hello world

=======

Goodbye

>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt

你所需要的做是就是編輯解決沖突,(接著把沖突標識符刪掉),再執行下面的命令:

$ git add file.txt

$ git commit

注意:提交注釋里已經有一些關于合并的信息了,通常是用這些默認信息,但是你可以添加一些你想要的注釋。

上面這些就是你要做一個簡單合并所要知道的,但是git提供更多的一些信息來 幫助解決沖突。

快速向前合并

還有一種需要特殊對待的情況,在前面沒有提到。通常,一個合并會產生一個合并提交(commit), 把兩個父分支里的每一行內容都合并進來。

但是,如果當前的分支和另一個分支沒有內容上的差異,就是說當前分支的每一個提交(commit)都已經存在另一個分支里了,git 就會執行一個快速向前"(fast forward)操作;git不創建任何新的提交(commit),只是將當前分支指向合并進來的分支。

 

mv

pull

push

rebase

remote

reset】:

 

git reset --hard HEAD”——撤銷當前合并。如果你覺得你合并后的狀態是一團亂麻,想把當前的修改都放棄,你可以用下面的命令回到合并之前的狀態。

git reset --hard ORIG_HEAD”——撤銷已提交(合并后)代碼的合并。但是這條命令在某些情況會很危險,如果你把一個已經被另一個分支合并的分支給刪了,那么以后在合并相關的分支時會出錯。

rm

show

status】:

 

git status”——獲得當前項目的一個狀況。

使用“git status 命令是查看索引內容的最簡單辦法你運行 git status命令就可以看到哪些文件被暫存了(就是在你的Git索引中), 哪些文件被修改了但是沒有暫存還有哪些文件沒有被跟蹤(untracked).

tag

git tag”——不帶任何參數創建一個標簽(tag)指定某個提交(commit)

$ git tag stable-1 1b2e1d63ff

這樣,我們可以用stable-1 作為提交(commit) "1b2e1d63ff" 的代稱(refer)。前面這樣創建的是一個輕量級標簽",這種分支通常是從來不移動的。如果你想為一個標簽(tag)添加注釋,或是為它添加一個簽名(sign it cryptographically), 那么我們就需要創建一個 ”標簽對象"


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()