1. 程式人生 > >git初始化和建立專案

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)

檢視遠端伺服器:

[email protected] ~/hello-world (master)

$ git remote

origin

[email protected] ~/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" 提交到本地倉庫

[email protected] ~/hello-world (master)

$ echo "hello world" >> helloworld.naxsu

[email protected] ~/hello-world (master)

$ ls *.naxsu

helloworld.naxsu

[email protected] ~/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")

[email protected] ~/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.

[email protected] ~/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

#

[email protected] ~/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

[email protected] ~/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")

[email protected] ~/hello-world (master)

$

忽略某些檔案

Java 檔案編譯成 .class 檔案,他是自動生成的,我們沒必要用版本控制它,所以提交的時候可以用忽略。

建立檔案 class1.class 、 java1.java ,建立 .gitignore ,並把 class1.class 新增到 .gitignore 中 , 同時用 vim 編輯 .gitignore ,把他自己也新增到裡面,用 $ cat .gitignore 命令可以檢視 .gitignore 的內容。接下來用 add,commit, 你就會發現 class1.class 是不會提交到倉庫中的。

[email protected] ~/hello-world (master)

$ echo "class1" > class1.class

[email protected] ~/hello-world (master)

$ echo "java1" > java1.java

[email protected] ~/hello-world (master)

$ echo "class1.class" >.gitignore

[email protected] ~/hello-world (master)

$ vim .gitignore

[email protected] ~/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 後,又一次檢視他們的不同。

[email protected] ~/hello-world (master)

$ vim helloworld.naxsu

[email protected] ~/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.

[email protected] ~/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.

[email protected] ~/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.

[email protected] ~/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(+)

[email protected] ~/hello-world (master)

$ git diff

[email protected] ~/hello-world (master)

$ git diff --cached

檔案的移動和刪除

移動 = 刪除 + 新增

[email protected] ~/hello-world (master)

$ ls *naxsu

helloworld.naxsu  test.naxsu

[email protected] ~/hello-world (master)

$ git rm test.naxsu

rm 'test.naxsu'

[email protected] ~/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

#

huangyi[email protected] ~/hello-world (master)

$ git reset head test.naxsu

Unstaged changes after reset:

D       test.naxsu

[email protected] ~/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 進行恢復

[email protected] ~/hello-world (master)

$ git checkout -- test.naxsu

[email protected] ~/hello-world (master)

$ ls *.naxsu

helloworld.naxsu  test.naxsu

如果 commit 了之後,就不能 checkout 了

[email protected] ~/hello-world (master)

$ git rm test.naxsu

rm 'test.naxsu'

[email protected] ~/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

[email protected] ~/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 )記錄

[email protected] ~/hello-world (master)

$ git log

commit 46d28afa27a90678c7391fc0bc5549db345f3c7d

Author: yineng huang

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

delete test.naxsu

……

git whatchanged

[email protected] ~/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 更詳細一些,可以顯示具體的檔名。