您的位置:首页 > 其它

git入门-----初始化命令(init、clone),Basic Snapshotting(add、status、commit、diff、reset、stash)

2017-04-23 16:00 495 查看
接下来就开始一点点的介绍一下入门的常用命令。如果你英文够好,那么强烈推荐看官方文档

https://git-scm.com/docs 这里面是最全、最权威的用法。
1、git仓库的创建和初始化相关的命令。

1.1 、git init。

从本地目录初始化git仓库,直接调用git init 。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e

$ mkdir test


Administrator@9GPBSPCCTFQXEUX MINGW64 /e

$ cd test


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/test

$ git init

Initialized empty Git repository in E:/test/.git/

[/code]

这样就在test目录下生成一个.git仓库,然后你就可以在当前目录下进行一系列的对文件的版本控制的操作.

git init --bare

创建一个裸库,其实后面不指定git仓库的文件夹的时候直接把.git中的所有目录生成到了当前目录下。暂时还不

清楚使用环境。

1.2、git clone

从远程库克隆一个一模一样的git仓库用于开发和管理。

Git能在许多协议下使用,所以Git URL可能以ssh://, http(s)://, git://。

git clone <版本库的网址> <本地目录名> 在第二个参数指定的目录中生成远程库的拷贝.

git clone <版本库的网址>:当不指定目录名字的时候会以最后的/后面的路径为目录(不包含.git)

Administrator@9GPBSPCCTFQXEUX MINGW64 /e

$ mkdir remote


Administrator@9GPBSPCCTFQXEUX MINGW64 /e

$ cd remote/


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/remote

$ git clone git@github.com:zhaoJoeyuan/TestTwo.git

Cloning into 'TestTwo'...

remote: Counting objects: 19, done.

remote: Compressing objects: 100% (17/17), done.

Receiving objects: 100% (19/19), done.

Resolving deltas: 100% (3/3), done.

remote: Total 19 (delta 3), reused 0 (delta 0), pack-reused 0

Checking connectivity... done.


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/remote

$ ls -a

./  ../  TestTwo/


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/remote

$ cd TestTwo/


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/remote/TestTwo (master)

$ ls -a

./  ../  .git/  README.md  第四个

[/code]

克隆完毕后远端的url对应的别名默认为origin.如下查看:

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/remote/TestTwo (master)

$ git remote -v

origin  git@github.com:zhaoJoeyuan/TestTwo.git (fetch)

origin  git@github.com:zhaoJoeyuan/TestTwo.git (push)

[/code]

git clone -b 远程库的某个分支名字 <版本库的网址> :直接克隆版本库的某个分支,这样克隆下来就不是默认的分支.

Administrator@9GPBSPCCTFQXEUX MINGW64 /e

$ git clone -b TestTTT git@github.com:zhaoJoeyuan/TestTwo.git TestTTT

Cloning into 'TestTTT'...

remote: Counting objects: 22, done.

remote: Compressing objects: 100% (19/19), done.

remote: Total 22 (delta 4), reused 0 (delta 0), pack-reused 0

Receiving objects: 100% (22/22), done.

Resolving deltas: 100% (4/4), done.

Checking connectivity... done.


Administrator@9GPBSPCCTFQXEUX MINGW64 /e

$ cd TestTTT/


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTTT (TestTTT)

$


[/code]

可以看到直接就是在TesTTT分之下了。

2、git基本的快照相关的命令。
2.1、git add命令.
我们新建三个文件夹,并且都处于untracked状态.

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/test (master)

$ ls

Fuck.txt  OMG.txt  Reamme.txt

[/code]

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/test (master)

$ git status

On branch master


Initialcommit


Untracked files:

(use "git add <file>..." to include in what will becommitted)


Fuck.txt

OMG.txt

Reamme.txt

[/code]

git add 文件名. 把当前文件添加到缓存区(Indes/Stage)当中.

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/test (master)

$ git add Reamme.txt

[/code]

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/test (master)

$ git status

On branch master


Initialcommit


Changes to becommitted:

(use "git rm --cached <file>..." to unstage)


new file:   Reamme.txt


Untracked files:

(use "git add <file>..." to include in what will becommitted)


Fuck.txt

OMG.txt

[/code]

git add -u 将目录下所有修改和删除存至缓存区,但不包扣新增就是新建的文件处于unteacked状态是不会add到缓存

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status

On branch master

Your branch is up-to-date with 'origin/master'.

Untracked files:

(use "git add <file>..." to include in what will becommitted)


Update.txt


nothing added tocommit but untracked files present (use "git add" to track)


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git add -u


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status

On branch master

Your branch is up-to-date with 'origin/master'.

Untracked files:

(use "git add <file>..." to include in what will becommitted)


Update.txt


nothing added tocommit but untracked files present (use "git add" to track)

[/code]

可以看到完全没什么作用,其实从-u也可以感觉出来,update吗。你以前没加入过缓存的就不管了,加入过的比如你
又修改或者删除了,那么重新add.

git add -A/--all和git add . 在git version 2.x以后是一模一样的,它们会添加项目中所有的文件到缓存区。而在 1.x的版
本git add . 是不会添加delete的文件的.网上的图:



2.2、git status

查看当前仓库的状态:列出了(修改过的、新创建的、已经暂存但未提交的)文件的状态。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status

On branch master

Your branch is ahead of 'origin/master' by 3commits.

(use "git push" to publish your localcommits)

Changes to becommitted:

(use "git reset HEAD <file>..." to unstage)


deleted:    Update.txt

[/code]

git status -s 显示简短的信息:

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status -s

D  Update.txt

[/code]

git status -b/--branch 查看当前所在的分支的状态信息

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status -sb

## master...origin/master [ahead 3]

D  Update.txt

[/code]

2.3、gitcommit :将缓存区内容添加到仓库中. 这个是把缓存区的-----版本库。还要注意只是提交缓存区中的内容。

基本用法 gitcommit :会把当前目录下的缓存区中所有的文件commit到版本库当中.此时会自动弹出默认的编辑器
让你编写提交的message信息。编写完毕后退出,自动完成提交。编辑器可以通过 core.editor=xx来进行修改.
gitcommit -m "message(提交信息)" :这样可以快速的编写提交信息。不会再额外的弹出提示框,但是这样的话
mesage信息就很难控制格式了,一般实际开发中不建议这样。
gitcommit -a :这个是把所有的已经通过git add 到缓存区的内容都提交到版本库,其实就是相当于自动帮助你add
你前面可能commit一次了,这次又修改了文件,此时你还需要git add xxx gitcommit 。执行这个
命令就相当于帮你执行git add了,但它不会影响untracked的文件,也就是未加入缓存区的.

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status

On branch master

Your branch is ahead of 'origin/master' by 6commits.

(use "git push" to publish your localcommits)

Changes not staged forcommit:

(use "git add <file>..." to update what will becommitted)

(use "git checkout -- <file>..." to discard changes in working directory)


modified:   one.txt


no changes added tocommit (use "git add" and/or "gitcommit -a")


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ gitcommit -m "teste"

On branch master

Your branch is ahead of 'origin/master' by 6commits.

(use "git push" to publish your localcommits)

Changes not staged forcommit:

modified:   one.txt


no changes added tocommit


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ gitcommit -am "haha"

......

The file will have its original line endings in your working directory.

1 file changed, 1 insertion(+), 1 deletion(-)

[/code]

gitcommit --amend :用于修改最后一次提交的message信息。当你发现错误的时候就可以重新进行编写。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git log -1

commit ab720bc81ac4f877beb79c80a41b935e7688835b

Author: Joey <zhaojoeyuan@163.com>

Date:   Sun Apr 16 16:13:47 2017 +0800


haha


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ gitcommit -amend

error: did you mean `--amend` (with two dashes ?)


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ gitcommit --amend

......
The file will have its original line endings in your working directory.

1 file changed, 1 insertion(+), 1 deletion(-)


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git log -1

commit 0b4e212189ba63e27d14559ed4894e4710874202

Author: Joey <zhaojoeyuan@163.com>

Date:   Sun Apr 16 16:13:47 2017 +0800


s改正的aha

[/code]

gitcommit xxxx(文件路径):当你在commit的命令行加上文件路径的时候。那么就会把所有你加上的文件的中的
内容提交到版本库,无论当前文件的内容有没有被加入到缓存区,(就是和缓存区add进去的内容就无
关了)并且如果没有被加入到缓存区,还会自动的加入到缓存区。


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status

On branch master

Your branch is ahead of 'origin/master' by 8commits.

(use "git push" to publish your localcommits)

Changes not staged forcommit:

(use "git add <file>..." to update what will becommitted)

(use "git checkout -- <file>..." to discard changes in working directory)


modified:   one.txt


no changes added tocommit (use "git add" and/or "gitcommit -a")


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ gitcommit -m "one.txt" one.txt

.......
The file will have its original line endings in your working directory.

1 file changed, 1 insertion(+)


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status

On branch master

Your branch is ahead of 'origin/master' by 9commits.

(use "git push" to publish your localcommits)

nothing tocommit, working directory clean

[/code]

可以看到即使处于modify状态的文件也被自动的加入缓存并且提交到了版本库.
2.4、git diff
显示工作区和索引区、索引区和版本库、版本库内的两次提交、两个blob对象、两个文件等等之间的区别。更具
不同的参数来显示不同的diff的内容,用的最多的是前三个内容的diff。
git diff :不加任何参数的时候是比较的工作区和暂存区的差异。就是你已经修改但是还未add到暂存区。
git diff --cached :是暂存区和版本库之间的差异。就是你已经add了但是还未commit。
git diff HEAD : 查看当前的所有的差异,包括已缓存和未缓存的。HEAD其实就是限制了path,这个path下的所有
差异都要显示。此时我改成当前分支比如当前是master也是一样的。
其实就是工作区和版本库之间的区别。HEAD当前分支的版本库。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status

......
Changes to becommitted:

(use "git reset HEAD <file>..." to unstage)


modified:   one.txt


Changes not staged forcommit:

(use "git add <file>..." to update what will becommitted)

(use "git checkout -- <file>..." to discard changes in working directory)


modified:   Two.txt



Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git diff HEAD

warning: LF will be replaced by CRLF in Two.txt.

The file will have its original line endings in your working directory.

diff --git a/Two.txt b/Two.txt

index f22c573..6f359b7 100644

--- a/Two.txt

+++ b/Two.txt

@@ -1,2 +1,3 @@

aasaasdadadczxczc

commit

+asdasdada

diff --git a/one.txt b/one.txt

index 502265c..a571112 100644

--- a/one.txt

+++ b/one.txt

@@ -2,3 +2,4 @@ asdasdadadzkkads

产生了快捷查看了

asadasd看

ttttttttest

+ssssss

warning: LF will be replaced by CRLF in one.txt.

The file will have its original line endings in your working directory.

[/code]

git diff HEAD^ HEAD :其实就是 git diffcommitId1 commitId2 :比较这两次提交之间的差异。
以上的任何命令加上 --stat:表示只查看概要,这个应用也很多的。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git diff master --stat

Two.txt | 1 +

one.txt | 1 +

2 files changed, 2 insertions(+)

[/code]

额外的小知识点:
git diff 比较的时候的输出格式:
+++:表示目标文件,+开头的行,是只出现在目标文件中的行。
---:表示源文件,-开头的行,是只出现在源文件中的行。
空格开头的行,是源文件和目标文件中都出现的行。

(以前一直当成了增加内容减少内容,理解的有偏差)
通常工作区域的文件都是被当作目标文件来看待。

例如:当我们在工作区中为one.txt添加一行以后

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git diff

diff --git a/one.txt b/one.txt

index 502265c..ca105df 100644

--- a/one.txt

+++ b/one.txt

@@ -2,3 +2,4 @@ asdasdadadzkkads

产生了快捷查看了

asadasd看

ttttttttest

+Test diff

[/code]

此时b就是目标文件:工作区域。a就是源文件:缓存区。此时前面带+的表示只有工作区域中有,前面是空格的证明
两个都有。
而当我们调用git add one.txt把它加入到缓存区时,在调用git diff --cached

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git diff --cached

diff --git a/one.txt b/one.txt

index 502265c..ca105df 100644

--- a/one.txt

+++ b/one.txt

@@ -2,3 +2,4 @@ asdasdadadzkkads

产生了快捷查看了

asadasd看

ttttttttest

+Test diff

[/code]

此时缓存区时目标文件、版本库是源文件。

当有两个参数的时候git diff 源文件/版本 目标文件/版本。
2.5、git reset
此命名根据包含不包含路径有两种用法:
第一种带路径的格式:git reset [-q] [<commit>] [--] <path>。
用指定的commit所指向的目录树中的path路径下文件替换暂存区中的文件。

如:git reset HEAD Hello.txt HEAD也就是上次提交的版本中的Hello.txt文件替换当前暂存区的,其实就是
实现了回退已添加到缓存区的path路径对应的文件。此处的HEAD理解的还不是很好。
注意从上面看HEAD是可省略的.git reset Hello.txt效果是一样的.

git reset . :注意这个命令会回退掉所有add到缓存区中的修改。

这个命令只能回退未commit到版本库中的文件。只是用某一次提交的文件提交暂存区的文件

第二种用法不带路径格式:git reset [--soft|--hard|--mixed|--merge|--keep] [-q] [<commit>]。

重置分支引用的指向所以很快.

参数不同决定是否覆盖暂存区和工作区对,主要的三种参数进行学习:

--soft :回退到commit版本。但工作区和暂存区都不会用commit版本的内容进行覆盖。它只是把版本库回退

到commit的版本。

--hard:暂存区,工作区全部用指定提交版本的目录树替换掉。注意是非常危险的,会导致工作区和缓存区

文件全部丢失(加入你是在commitid版本之后才加的这些文件)。

--mixed:默认的也是这个参数(不写的时候)。这个会用提交的版本目录树来覆盖当前的版本库和暂存区,但

是不会覆盖工作区域的修改。会保留对应的文件自动变为相应的状态。

返回到上一个提交或者上上个提交有较为简单的写法:

git reset --hard HEAD^ git reset --hard HEAD^^ 当然往上100个版本写100个^比较容易数不过来,所

以写成HEAD~100。

此时有个问题假如你回退过头了,此时git log又没有commitid了,你可以通过git refflog:会显示任何的一次操

作id。然后调用如git reset --hardcommitid即可。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git reflog

b9a6a15 HEAD@{0}: reset: moving to b9a6a15

136e495 HEAD@{1}: reset: moving to 136e495

b9a6a15 HEAD@{2}: reset: moving to b9a6a15

136e495 HEAD@{3}: reset: moving to 136e495

582f179 HEAD@{4}: reset: moving to 582f1794756f1a6bb933b88c3dd50bfbee4ef720

136e495 HEAD@{5}:commit: ALL

c38f77d HEAD@{6}: reset: moving to c38f77d0563846a4099d7af16e1cd7fe50f21a3b

7d20fa7 HEAD@{7}:commit: HAHHA

d0d0f14 HEAD@{8}: reset: moving to HEAD^

e053fa1 HEAD@{9}:commit: ALL

d0d0f14 HEAD@{10}:commit: RRRRR

1aa03f3 HEAD@{11}: checkout: moving from test to master

b9a6a15 HEAD@{12}: checkout: moving from master to test

1aa03f3 HEAD@{13}: checkout: moving from test to master

b9a6a15 HEAD@{14}:commit: AA

1aa03f3 HEAD@{15}: checkout: moving from master to test

1aa03f3 HEAD@{16}:commit: one.txt

[/code]

2.6 、git rm 删除操作用于把文件从git版本控制中删除。
git rm xxx(文件名字) 把文件从git仓库中删除。此时文件处于delete的状态,并且已从硬盘上,但你此时还可以
恢复。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git rm Two.txt

rm 'Two.txt'


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git status

On branch master

Your branch is ahead of 'origin/master' by 11commits.

(use "git push" to publish your localcommits)

Changes to becommitted:

(use "git reset HEAD <file>..." to unstage)


deleted:    Two.txt



Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ ls

AHAHH.txt  Hello.txt  one.txt  README.md  Three.txt  第四个

[/code]

一旦你提交以后就彻底没了,只能通过版本回退来恢复了。

git rm -r xxx(文件夹的名字) :用于把指定的文件目录从git中移除。

git rm --cached Hello.txt :只是把文件从git中删除,但是文件本身还是会存在于系统当中。只是不参与版本控制。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ git rm --cached Hello.txt

rm 'Hello.txt'


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (master)

$ ls

AHAHH.txt  Hello.txt  README.md  Three.txt  第四个

[/code]

rm:就是直接删除命令,是文件处于delete状态还是在版本控制下,可以add或者别的命令继续控制。

2.7、git stash:保存工作现场。
使用的场景:当我们正在一个分支上做任务时,此时又来一个非常紧急的任务,而当前的任务我还不想提交。那么
怎么办呐?就用git stash来把当前的环境保存下来。如果你不提交也不用stash的时候会有如下提示,并且不能切
换分支。
2.4.2里面可以,冲突好像就不可以

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git checkout master

error: Your local changes to the following files would be overwritten by checkout:

AHAHH.txt

Please,commit your changes or stash them before you can switch branches.

Aborting

[/code]

调用git status以后:

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git stash

Saved working directory and index state WIP on test: 630e8e9 saa

HEAD is now at 630e8e9 saa

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git status

On branch test

nothing tocommit, working directory clean


[/code]

工作区域就干净了。当然也可以保存多个stash如下:我们队one.txt进行修改再进行git stash进行保存

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ vi one.txt


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git status

On branch test

Changes not staged forcommit:

(use "git add <file>..." to update what will becommitted)

(use "git checkout -- <file>..." to discard changes in working directory)


modified:   one.txt


no changes added tocommit (use "git add" and/or "gitcommit -a")


Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git stash

Saved working directory and index state WIP on test: 630e8e9 saa

HEAD is now at 630e8e9 saa

[/code]

然后切换到master分支来进行解决紧急任务。解决完闭后再回到本分支。然后调用

git stash list :列出所有的已经加入到git栈中的工作环境。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git stash list

stash@{0}: WIP on test: 630e8e9 saa

stash@{1}: WIP on test: 630e8e9 saa

[/code]

此时你想恢复那个就恢复那个:git stash apply id(保存的那个)

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git stash apply stash@{0}

On branch test

Changes not staged forcommit:

(use "git add <file>..." to update what will becommitted)

(use "git checkout -- <file>..." to discard changes in working directory)


modified:   one.txt


no changes added tocommit (use "git add" and/or "gitcommit -a")

[/code]

可以看到是恢复的我们第二次保存的状态,因为git使用栈的方式进行管理的,第二次的就跑到了栈顶部。需要注意的
是恢复以后并不会自动删除stash。需要调用git stash drop id

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git stash drop stash@{0}

Dropped stash@{0} (e8938b383f8e6bae350c146d9ce829b9df68ca69)

[/code]

注意其实是把栈顶部的给弹出来了,所以下面的会再次变成栈顶。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git stash list

stash@{0}: WIP on test: 630e8e9 saa

[/code]

所以还有一种方式 git stash pop :恢复最近保存的状态,并且自动删除这个状态。就是处理栈顶嘛。

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git stash pop

On branch test

Changes not staged forcommit:

(use "git add <file>..." to update what will becommitted)

(use "git checkout -- <file>..." to discard changes in working directory)


modified:   AHAHH.txt

modified:   one.txt


no changes added tocommit (use "git add" and/or "gitcommit -a")

Dropped refs/stash@{0} (d265c0b84ac2cdc3116df1e3491bd6b7b6fdbfd7)

[/code]

Administrator@9GPBSPCCTFQXEUX MINGW64 /e/TestTwo (test)

$ git stash list

[/code]

可与看出自动删除了。

还有一个清空栈的命令:git stash list.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git