转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
1.成功安装git之后,打开Git bash终端
2.创建仓库:
$ mkdir learngit
$ cd learngit
$ pwd
/Users/mdl/learngit
3.进入仓库learngit目录下通过git init
命令把这个目录变成Git可以管理的仓库:
$ git init
Initialized empty Git repository in /Users/mdl/learngit/.git/
4.用ls -ah
命令可以看见目录下所有文件(包括隐藏的)。
5.使用Notepad++的默认编码设置为UTF-8 without BOM**在仓库learngit目录下(或者其子目录下)**创建readme.txt,内容如下:
Git is a version control system.
Git is free software.
6.用命令git add
告诉Git,把文件添加到仓库(执行完后没有任何提示,表示add成功):
$ git add readme.txt
- 6.1可能会出现的错误(仓库没有初始化):
fatal: Not a git repository (or any of the parent directories): .git
- 解决办法:初始化该仓库
$ git init
7.用命令git commit
告诉Git,把文件提交到仓库:
$ git commit -m "wrote a readme file"
[master (root-commit) cb926e7] wrote a readme file
1 file changed, 2 insertions(+)
create mode 100644 readme.txt
注:
1.
-m "wrote a readme file"
表示提交代码的注释;2.commit可以一次提交很多文件,可以多次add不同的文件,或者一次add多个不同的文件。
$ git add file1.txt
$ git add file2.txt file3.txt
$ git commit -m "add 3 files."
8.修改readme.txt文件
Git is a distributed version control system.
Git is free software.
9.运行git status
命令,如下:
$ 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: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
注:git status命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,readme.txt被修改过了,但还没有准备提交的修改。
10.使用git diff
查看修改的内容:
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
Git is free software.
11.再次git add
到仓库
$ git add readme.txt
-
git commit
前再查看状态git status
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#
12.git status
告诉我们,将要被提交的修改包括readme.txt
$ git commit -m "add distributed"
[master ea34578] add distributed
1 file changed, 1 insertion(+), 1 deletion(-)
- 提交后,再用
git status
命令看看仓库的当前状态
$ git status
# On branch master
nothing to commit (working directory clean)
Git提示当前没有需要提交的修改,而且工作目录是干净(working directory clean)的。