首页 > 代码库 > Git 基本操作

Git 基本操作

1.创建仓库

  ——创建工作目录(Working Directory):git三种副本:工作目录(Working Direcotry),暂存区域(Stage,索引(Index)),仓库(History)

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

2.查看文件状态

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.添加文件

  ——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.提交文件

  ——git commit 给暂存区域生成快照并提交。

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.查看工作目录提交记录

(回顾:git status--查看文件状态,git-diff--查看文件内容状态)

# 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"

Git 基本操作