Android_版本控制_Git命令行介绍和使用说明
一、命令“git”或者“git help”查詢常用命令
【add】:
“git add”——不但是用來添加不在版本控制中的新文件,也用于添加已在版本控制中但是剛修改過的文件;?在這兩種情況下, Git都會(huì)獲得當(dāng)前文件的快照并且把內(nèi)容暫存(stage)到索引中,為下一次commit做好準(zhǔn)備。Git跟蹤的是內(nèi)容不是文件。
【bisect】
【branch】:
“git branch”——會(huì)得到當(dāng)前倉庫中存在的所有分支列表。其中,“master”分支是Git系統(tǒng)默認(rèn)創(chuàng)建的主分支。星號(hào)(“*”)標(biāo)識(shí)了你當(dāng)工作在哪個(gè)分支下。
“git branch -d branchname”——git branch -d只能刪除那些已經(jīng)被當(dāng)前分支的合并的分支。
“git branch –D branchname”——git branch –D可以強(qiáng)制刪除某個(gè)分支的話就用。
”git branch -人“——查看所有分支。
【checkout】:?
“git checkout brancename”——切換到“brancename”分支。
【clone】:
注:Clone一個(gè)倉庫。為了得一個(gè)項(xiàng)目的拷貝(copy),我們需要知道這個(gè)項(xiàng)目倉庫的地址(Git URL). Git能在許多協(xié)議下使用,所以Git URL可能以ssh://, http(s)://, git://,或是只是以一個(gè)用戶名(git?會(huì)認(rèn)為這是一個(gè)ssh?地址)為前輟.?有些倉庫可以通過不只一種協(xié)議來訪問,例如,Git本身的源代碼你既可以用?git://?協(xié)議來訪問:git clone git://git.kernel.org/pub/scm/git/git.git
也可以通過http?協(xié)議來訪問:git clone http://www.kernel.org/pub/scm/git/git.git
git://協(xié)議較為快速和有效,但是有時(shí)必須使用http協(xié)議,比如你公司的防火墻阻止了你的非http訪問請(qǐng)求.如果你執(zhí)行了上面兩行命令中的任意一個(gè),你會(huì)看到一個(gè)新目錄: 'git',它包含所有的Git源代碼和歷史記錄.
在默認(rèn)情況下,Git會(huì)把"Git URL"里目錄名的'.git'的后輟去掉,做為新克隆(clone)項(xiàng)目的目錄名:?(例如.?git clone
http://git.kernel.org/linux/kernel/git/torvalds/linux-2.6.git?會(huì)建立一個(gè)目錄叫'linux-2.6')
?
“git clone –bare test test.git”——選項(xiàng)bare的意思就是用現(xiàn)有的被控制倉庫創(chuàng)建一個(gè)GIT倉庫。取代了創(chuàng)建目錄和初始化兩步,而且使得新生成的目錄自身就是倉庫目錄,而這個(gè)倉庫目錄中是沒有像工作目錄中的源文件的,只有鏡像文件(即跟原來的.git目錄下的內(nèi)容差不多)。這樣的目錄一般就是服務(wù)器存儲(chǔ)的內(nèi)容,可以稱之為鏡像目錄。bare后的參數(shù)一是本地目錄,參數(shù)二是遠(yuǎn)程(也可是本地)鏡像目錄,這樣就可以是想將本地目錄test推送到服務(wù)器目錄。
--bare
Make a?bare?GIT repository. That is, instead of creating?<directory>?and placing the administrative files in?<directory>/.git, make the<directory>?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”的過程相反,就是利用遠(yuǎn)程鏡像來恢復(fù)本地源文件。例子的意思就是從遠(yuǎn)程鏡像test.git中克隆出源文件到本地目錄test中。
?
?
【commit】:
?
“git commit”——把暫存區(qū)中記錄的要提交的內(nèi)容提交到Git的對(duì)象數(shù)據(jù)庫。這會(huì)提示你輸入本次修改的注釋,完成后就會(huì)記錄一個(gè)新的項(xiàng)目版本。
?
“git commit –a”這會(huì)自動(dòng)把所有內(nèi)容被修改的文件(不包括新創(chuàng)建的文件)都添加到索引中,并且同時(shí)把它們提交。
這里有一個(gè)關(guān)于寫commit注釋的技巧和大家分享:commit注釋最好以一行短句子作為開頭,來簡要描述一下這次commit所作的修改(最好不要超過50個(gè)字符);然后空一行再把詳細(xì)的注釋寫清楚。這樣就可以很方便的用工具把commit注釋變成email通知,第一行作為標(biāo)題,剩下的部分就作email的正文。
?
“git commit -m”——后跟參數(shù)表示要接提交內(nèi)容說明的信息。
?
【config】:
“git config –username?”——
“git config --get remote.origin.url”——查看本地配置的遠(yuǎn)程倉庫地址。
“git config --global core.editor "notepad"”——將Git默認(rèn)的Vi編輯器換成記事本,如果想再改成Vim則使用下面命令行:
git config --global core.editor "\"C:/Program Files (x86)/GitExtensions/GitExtensions.exe\" fileeditor"
?
【diff】:
?
“git diff”?——比較自己項(xiàng)目中任意兩個(gè)版本的差異,或是用來查看別人提交進(jìn)來的新分支。顯示在當(dāng)前的工作目錄里的,沒有staged(添加到索引中),且在下次提交時(shí)不會(huì)被提交的修改。如有合并的文件有沖突,輸入此命令就可以查看當(dāng)前有哪些文件產(chǎn)生了沖突。
“git diff test”——這會(huì)顯示你當(dāng)前工作目錄與另外一個(gè)叫'test'分支的差別。你也以加上路徑限定符,來只比較某一個(gè)文件或目錄。
“git diff master..test”——這條命令只顯示兩個(gè)分支間的差異,如果你想找出?“git diff master...test”——這條命令顯示‘master’,‘test’的共有父分支和'test'分支之間的差異,用3個(gè)‘.'來取代前面的兩個(gè)'.'?。
“git diff --cached”——看看哪些文件將被提交(commit)。顯示你當(dāng)前的索引和上次提交間的差異;這些內(nèi)容在不帶"-a"參數(shù)運(yùn)行?"git commit"命令時(shí)就會(huì)被提交。
“git diff HEAD”——上面這條命令會(huì)顯示你工作目錄與上次提交時(shí)之間的所有差別,這條命令所顯示的?內(nèi)容都會(huì)在執(zhí)行"git commit -a"命令時(shí)被提交。
“git diff HEAD -- ./lib”——上面這條命令會(huì)顯示你當(dāng)前工作目錄下的lib目錄與上次提交之間的差別(或者更準(zhǔn)確的?說是在當(dāng)前分支)。
“git diff --stat”——不查看每個(gè)文件的詳細(xì)差別,只是統(tǒng)計(jì)一下有哪些文件被改動(dòng),有多少行被改動(dòng)。
?
?
【fetch】
【grep】
【init】:
?
“git init”——初始化一個(gè)新的倉庫,將它置于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
當(dāng)然你也可以組合上面的命令選項(xiàng);下面的命令就是找出所有從"v2.5“開始在fs目錄下的所有Makefile的修改。
$ git log v2.5.. Makefile fs/
Git會(huì)根據(jù)git log命令的參數(shù),按時(shí)間順序顯示相關(guān)的提交(commit)。
?
“git log -p”——顯示補(bǔ)丁(patchs)。
“git log --stat”——日志統(tǒng)計(jì)。如果用--stat選項(xiàng)使用“git log”,它會(huì)顯示在每個(gè)提交(commit)中哪些文件被修改了,?這些文件分別添加或刪除了多少行內(nèi)容。
?
“git log --pretty”——格式化日志。你可以按你的要求來格式化日志輸出。“—pretty”參數(shù)可以使用若干表現(xiàn)格式,如“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'參數(shù)(參見:git log)來創(chuàng)建你自己的"格式“.
$ 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:
另一個(gè)有趣的事是:你可以用'--graph'選項(xiàng)來可視化你的提交圖(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
它會(huì)用ASCII字符來畫出一個(gè)很漂亮的提交歷史(commit history)線。
?
日志排序
你也可以把日志記錄按一些不同的順序來顯示。注意,git日志從最近的提交(commit)開始,并且從這里開始向它們父分支回溯。然而git歷史可能包括多個(gè)互不關(guān)聯(lián)的開發(fā)線路,這樣有時(shí)提交(commits)顯示出來就有點(diǎn)雜亂。
如果你要指定一個(gè)特定的順序,可以為git log命令添加順序參數(shù)(ordering option).
按默認(rèn)情況,提交(commits)會(huì)按逆時(shí)間(reverse chronological)順序顯示。
但是你也可以指定‘--topo-order'參數(shù),這就會(huì)讓提交(commits)按拓樸順序來顯示(就是子提交在它們的父提交前顯示).?如果你用git log命令按拓樸順序來顯示git倉庫的提交日志,你會(huì)看到“開發(fā)線"(development lines)都會(huì)集合在一起。
$ 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'參數(shù),這樣顯示提交日志的順序主要按提交日期來排序。這個(gè)參數(shù)和'--topo-order'有一點(diǎn)像,沒有父分支會(huì)在它們的子分支前顯示,但是其它的東東還是按交時(shí)間來排序顯示。你會(huì)看到"開發(fā)線"(development lines)沒有集合一起,它們會(huì)像并行開發(fā)(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'參數(shù)來逆向顯示所有日志。
?
【merge】:
“git merge branchname”——這個(gè)命令把分支"branchname"合并到了當(dāng)前分支里面。如有沖突(沖突--同一個(gè)文件在遠(yuǎn)程分支和本地分支里按不同的方式被修改了);那么命令的執(zhí)行輸出就像下面一樣:
$?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.
在有問題的文件上會(huì)有沖突標(biāo)記,在你手動(dòng)解決完沖突后就可以把此文件添加到索引(index)中去,用“git commit”命令來提交,就像平時(shí)修改了一個(gè)文件一樣。
如果你用gitk來查看commit的結(jié)果,你會(huì)看到它有兩個(gè)父分支:一個(gè)指向當(dāng)前的分支,另外一個(gè)指向剛才合并進(jìn)來的分支。
解決合并中的沖突
如果執(zhí)行自動(dòng)合并沒有成功的話,git會(huì)在索引和工作樹里設(shè)置一個(gè)特殊的狀態(tài),?提示你如何解決合并中出現(xiàn)的沖突。
有沖突(conflicts)的文件會(huì)保存在索引中,除非你解決了問題并且更新了索引,否則執(zhí)行git commit都會(huì)失敗:
$ git commit
file.txt: needs merge
如果執(zhí)行git status會(huì)顯示這些文件沒有合并(unmerged),這些有沖突的文件里面會(huì)添加像下面的沖突標(biāo)識(shí)符:
<<<<<<< HEAD:file.txt
Hello world
=======
Goodbye
>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
你所需要的做是就是編輯解決沖突,(接著把沖突標(biāo)識(shí)符刪掉),再執(zhí)行下面的命令:
$ git add file.txt
$ git commit
注意:提交注釋里已經(jīng)有一些關(guān)于合并的信息了,通常是用這些默認(rèn)信息,但是你可以添加一些你想要的注釋。
上面這些就是你要做一個(gè)簡單合并所要知道的,但是git提供更多的一些信息來?幫助解決沖突。
快速向前合并
還有一種需要特殊對(duì)待的情況,在前面沒有提到。通常,一個(gè)合并會(huì)產(chǎn)生一個(gè)合并提交(commit),?把兩個(gè)父分支里的每一行內(nèi)容都合并進(jìn)來。
但是,如果當(dāng)前的分支和另一個(gè)分支沒有內(nèi)容上的差異,就是說當(dāng)前分支的每一個(gè)提交(commit)都已經(jīng)存在另一個(gè)分支里了,git?就會(huì)執(zhí)行一個(gè)“快速向前"(fast forward)操作;git不創(chuàng)建任何新的提交(commit),只是將當(dāng)前分支指向合并進(jìn)來的分支。
?
【mv】
【pull】
【push】
【rebase】
【remote】
【reset】:
?
“git reset --hard HEAD”——撤銷當(dāng)前合并。如果你覺得你合并后的狀態(tài)是一團(tuán)亂麻,想把當(dāng)前的修改都放棄,你可以用下面的命令回到合并之前的狀態(tài)。
“git reset --hard ORIG_HEAD”——撤銷已提交(合并后)代碼的合并。但是這條命令在某些情況會(huì)很危險(xiǎn),如果你把一個(gè)已經(jīng)被另一個(gè)分支合并的分支給刪了,那么以后在合并相關(guān)的分支時(shí)會(huì)出錯(cuò)。
【rm】
【show】
【status】:
?
“git status”——獲得當(dāng)前項(xiàng)目的一個(gè)狀況。
使用“git status”?命令是查看索引內(nèi)容的最簡單辦法.?你運(yùn)行?git status命令,?就可以看到:?哪些文件被暫存了(就是在你的Git索引中),?哪些文件被修改了但是沒有暫存,?還有哪些文件沒有被跟蹤(untracked).
【tag】
“git tag”——不帶任何參數(shù)創(chuàng)建一個(gè)標(biāo)簽(tag)指定某個(gè)提交(commit)。
$ git tag stable-1 1b2e1d63ff
這樣,我們可以用stable-1?作為提交(commit) "1b2e1d63ff"?的代稱(refer)。前面這樣創(chuàng)建的是一個(gè)“輕量級(jí)標(biāo)簽",這種分支通常是從來不移動(dòng)的。如果你想為一個(gè)標(biāo)簽(tag)添加注釋,或是為它添加一個(gè)簽名(sign it cryptographically),?那么我們就需要?jiǎng)?chuàng)建一個(gè)?”標(biāo)簽對(duì)象"。
?
?
?
?
【gitk】:
執(zhí)行了gitk后會(huì)有一個(gè)很漂亮的圖形的顯示項(xiàng)目的歷史。
總結(jié)
以上是生活随笔為你收集整理的Android_版本控制_Git命令行介绍和使用说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机开机时间不对,电脑每次开机时间都不
- 下一篇: 沉降预测算法-双曲线法