git实战-常用命令详细讲解

初始化和建立项目

有两种方式,一种是 init, 另外一种是 clone

init 在上面的例子中已经用过了,也就是进入项目所在的目录,用 $ git init 即可。

Clone 一般是 从远程服务器克隆一个已有的版本仓库 到本机,命令如下:

$ git clone git://github.com/git/hello-world.git

Cloning into 'hello-world'...

remote: Counting objects: 158, done.

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

remote: Total 158 (delta 54), reused 154 (delta 54)

Receiving objects: 100% (158/158), 15.63 KiB, done.

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

error: unable to create file brainf*ck.bf (Invalid argument)

查看远程服务器:

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git remote

origin

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git remote -v

origin  git://github.com/git/hello-world.git (fetch)

origin  git://github.com/git/hello-world.git (push)

添加与提交

所用到的命令是 add commit status.

1.         创建一个名为 helloworld.naxsu 的文件 .

2.         git status 查看当前目录文件的提交状态

brainf*ck.bf 是刚才克隆的时候,没法克隆下来,这里显示是删除了

helloworld.naxsu 是刚创建的文件,提示用 "git add" 添加的缓冲区中或者用 "git commit -a" 添加并提交

3.         $ git add helloworld.naxsu 进行添加到缓冲区

         添加当前目录下的所有文件

         $ git add .

         添加以 .c 为后缀的文件

         $ git add *.c

         添加指定文件

         $ git add index.jsp

4.         $ git commit helloworld.naxsu -m "init helloworld.naxsu" 提交到本地仓库

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ echo "hello world" >> helloworld.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ ls *.naxsu

helloworld.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git status

# On branch master

# Changes not staged for commit:

#   (use "git add/rm ..." to update what will be committed)

#   (use "git checkout -- ..." to discard changes in working directory)

#

#       deleted:    brainf*ck.bf

#

# Untracked files:

#   (use "git add ..." to include in what will be committed)

#

#       helloworld.naxsu

no changes added to commit (use "git add" and/or "git commit -a")

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git add helloworld.naxsu

warning: LF will be replaced by CRLF in helloworld.naxsu.

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

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git status

# On branch master

# Changes to be committed:

#   (use "git reset HEAD ..." to unstage)

#

#       new file:   helloworld.naxsu

#

# Changes not staged for commit:

#   (use "git add/rm ..." to update what will be committed)

#   (use "git checkout -- ..." to discard changes in working directory)

#

#       deleted:    brainf*ck.bf

#

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git commit helloworld.naxsu -m "init helloworld.naxsu"

warning: LF will be replaced by CRLF in helloworld.naxsu.

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

[master 8c17395] init helloworld.naxsu

warning: LF will be replaced by CRLF in helloworld.naxsu.

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

 1 file changed, 1 insertion(+)

 create mode 100644 helloworld.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git status

# On branch master

# Your branch is ahead of 'origin/master' by 1 commit.

#

# Changes not staged for commit:

#   (use "git add/rm ..." to update what will be committed)

#   (use "git checkout -- ..." to discard changes in working directory)

#

#       deleted:    brainf*ck.bf

#

no changes added to commit (use "git add" and/or "git commit -a")

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$

忽略某些文件

Java 文件编译成 .class 文件,他是自动生成的,我们没必要用版本控制它,所以提交的时候可以用忽略。

创建文件 class1.class java1.java ,创建 .gitignore ,并把 class1.class 添加到 .gitignore , 同时用 vim 编辑 .gitignore ,把他自己也添加到里面,用 $ cat .gitignore 命令可以查看 .gitignore 的内容。接下来用 add,commit, 你就会发现 class1.class 是不会提交到仓库中的。

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ echo "class1" > class1.class

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ echo "java1" > java1.java

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ echo "class1.class" >.gitignore

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ vim .gitignore

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ cat .gitignore

class1.class

.gitignore

 

$ git status

$ git add .

$ git status

$ git commit -a -m "ignore test"

……

比较文件的不同

$ git diff( 默认是 $ git diff --staged)

$ git diff --staged: 比较 workspace VS staged

$ git diff --cached: 比较 staged VS local repo

演示思路:修改 helloworld.naxsu ,用 git diff 查看不同,把他 add 之后再查看他们的不同,然后 commit 后,又一次查看他们的不同。

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ vim helloworld.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git diff

diff --git a/helloworld.naxsu b/helloworld.naxsu

index 3b18e51..6d05489 100644

--- a/helloworld.naxsu

+++ b/helloworld.naxsu

@@ -1 +1,2 @@

 hello world

+add something

warning: LF will be replaced by CRLF in helloworld.naxsu.

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

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git add helloworld.naxsu

warning: LF will be replaced by CRLF in helloworld.naxsu.

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

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git diff --cached

diff --git a/helloworld.naxsu b/helloworld.naxsu

index 3b18e51..6d05489 100644

--- a/helloworld.naxsu

+++ b/helloworld.naxsu

@@ -1 +1,2 @@

 hello world

+add something

warning: LF will be replaced by CRLF in helloworld.naxsu.

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

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git commit helloworld.naxsu -m "modified helloworld.naxsu"

warning: LF will be replaced by CRLF in helloworld.naxsu.

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

[master warning: LF will be replaced by CRLF in helloworld.naxsu.

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

6e7814d] modified helloworld.naxsu

warning: LF will be replaced by CRLF in helloworld.naxsu.

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

 1 file changed, 1 insertion(+)

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git diff

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git diff --cached

文件的移动和删除

移动 = 删除 + 添加

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ ls *naxsu

helloworld.naxsu  test.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git rm test.naxsu

rm 'test.naxsu'

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git status

# On branch master

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

#

# Changes to be committed:

#   (use "git reset HEAD ..." to unstage)

#

#       deleted:    test.naxsu

#

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git reset head test.naxsu

Unstaged changes after reset:

D       test.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git status

# On branch master

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

#

# Changes not staged for commit:

#   (use "git add/rm ..." to update what will be committed)

#   (use "git checkout -- ..." to discard changes in working directory)

#

#       deleted:    test.naxsu

#

no changes added to commit (use "git add" and/or "git commit -a")

如果没提交还可以 checkout 进行恢复

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git checkout -- test.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ ls *.naxsu

helloworld.naxsu  test.naxsu

如果 commit 了之后,就不能 checkout

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git rm test.naxsu

rm 'test.naxsu'

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git commit -a -m "delete test.naxsu"

[master 46d28af] delete test.naxsu

 1 file changed, 1 deletion(-)

 delete mode 100644 test.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git checkout -- test.naxsu

error: pathspec 'test.naxsu' did not match any file(s) known to git.

移动用 mv 命令,具体参考 $ git mv --help

查看操作记录

git log 显示所有的提交( commit )记录

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git log

commit 46d28afa27a90678c7391fc0bc5549db345f3c7d

Author: yineng huang

Date:   Fri Aug 17 23:28:34 2012 +0800

 

delete test.naxsu

……

git whatchanged

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git whatchanged

commit 46d28afa27a90678c7391fc0bc5549db345f3c7d

Author: yineng huang

Date:   Fri Aug 17 23:28:34 2012 +0800

 

    delete test.naxsu

 

:100644 000000 77608b6... 0000000... D  test.naxsu

……

git-whatchanged 显示的信息比 git-log 更详细一些,可以显示具体的文件名。

本文链接: http://www.656463.com/portal.php?mod=view&aid=7 0 ,转载请注明出处

下一节: 共享及更新项目

你可能感兴趣的:(常用命令)