Git基本操做(add,commit的理解)

1.建立倉庫linux

  ——建立工做目錄(Working Directory)git三種副本:工做目錄(Working Direcotry),暫存區域(Stage,索引(Index)),倉庫(History)git

#/home/yang/Documents/repo01
. ├── datafiles │   └── data.txt ├── test01 ├── test02 └── test03
# Initialize the local Git repository(在根目錄下生成.git文件夾)
git init

2.查看文件狀態this

yang@mint-linux ~/Documents/repo01 $ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    datafiles/
#    test01
#    test02
#    test03
nothing added to commit but untracked files present (use "git add" to track)

 

3.添加文件spa

  ——git add files 把當前文件放入暫存區域版本控制

yang@mint-linux ~/Documents/repo01 $ git add .
yang@mint-linux ~/Documents/repo01 $ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
#    new file: datafiles/data.txt
#    new file: test01
#    new file: test02
#    new file: test03
#

3.提交文件code

  ——git commit 給暫存區域生成快照並提交。blog

yang@mint-linux ~/Documents/repo01 $ git commit -m "Initial commit"
[master (root-commit) 3f79459] Initial commit
 4 files changed, 4 insertions(+)
 create mode 100644 datafiles/data.txt
 create mode 100644 test01
 create mode 100644 test02
 create mode 100644 test03
yang@mint-linux ~/Documents/repo01 $ git status
# On branch master
nothing to commit, working directory clean

4.查看文件內容變動索引

yang@mint-linux ~/Documents/repo01 $ echo "This is a change" > test01
yang@mint-linux ~/Documents/repo01 $ echo "and this is another change" > test02
yang@mint-linux ~/Documents/repo01 $ git diff
diff --git a/test01 b/test01
index 749eb2d..d0a432b 100644
--- a/test01
+++ b/test01
@@ -1,4 +1 @@
-datafiles
-test01
-test02
-test03
+This is a change
diff --git a/test02 b/test02
index e69de29..552c22e 100644
--- a/test02
+++ b/test02
@@ -0,0 +1 @@
+and this is another change
yang@mint-linux ~/Documents/repo01 $ git commit -a -m "These are new changes"

5.查看工做目錄提交記錄rem

(回顧:git status--查看文件狀態,git-diff--查看文件內容狀態)it

# Make some changes in the file
echo "This is a new change" > test01
echo "and this is another new change" > test02
yang@mint-linux ~/Documents/repo01 $ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   test01
#    modified:   test02
#
no changes added to commit (use "git add" and/or "git commit -a")
yang@mint-linux ~/Documents/repo01 $ git diff
diff --git a/test01 b/test01
index d0a432b..18fec42 100644
--- a/test01
+++ b/test01
@@ -1 +1 @@
-This is a change
+This is a new change
diff --git a/test02 b/test02
index 552c22e..0b17386 100644
--- a/test02
+++ b/test02
@@ -1 +1 @@
-and this is another change
+and this is another new change
yang@mint-linux ~/Documents/repo01 $ git add . && git commit -m "More changes - type in the commit message"
[master 8a18ab1] More changes - type in the commit message
 2 files changed, 2 insertions(+), 2 deletions(-)
yang@mint-linux ~/Documents/repo01 $ git log
commit 8a18ab1c77ecaf049d17e5ac8fb682ae618cd710
Author: Will Hunting <yangqionggo@gmail.com>
Date:   Sun Aug 18 18:57:38 2013 +0800

    More changes - type in the commit message

commit a6bd74cdbaa1b349d537008f33fa186eae9d48c9
Author: Will Hunting <yangqionggo@gmail.com>
Date:   Sun Aug 18 18:56:07 2013 +0800

    These are new changes

commit 3f794593e7008286a893a5d00f81ee5757140469
Author: Will Hunting <yangqionggo@gmail.com>
Date:   Sun Aug 18 18:50:58 2013 +0800

    Initial commit

6.刪除文件

若是你刪除了一個在版本控制之下的文件,那麼使用git add .不會在索引中刪除這個文件。須要經過帶-a選項的git commit命令和-A選項的git add命令來完成

# Create a file and put it under version control
touch nonsense.txt
git add . && git commit -m "a new file has been created"
# Remove the file
rm nonsense.txt
# Try standard way of committing -> will not work 
git add . && git commit -m "a new file has been created"
# Now commit with the -a flag
git commit -a -m "File nonsense.txt is now removed"
# Alternatively you could add deleted files to the staging index via
git add -A . 
git commit -m "File nonsense.txt is now removed"

7.更正提交的信息

git commit --amend -m "More changes - now correct"
相關文章
相關標籤/搜索