Git命令行介绍和使用说明(持续更新)

Git命令行介绍和使用说明(持续更新)

文档名称

Git命令行介绍和使用说明会

创建时间

2012/8/23

修改时间

2012/8/23

创建人

Baifx

简介(收获)

参见:《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 <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”的过程相反,就是利用远程镜像来恢复本地源文件。例子的意思就是从远程镜像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), 那么我们就需要创建一个标签对象"

gitk】:

执行了gitk后会有一个很漂亮的图形的显示项目的历史。

来源: http://blog.chinaunix.net/uid-25806493-id-3328322.html

原文地址:https://www.cnblogs.com/zfying/p/2855083.html