常用Git操作(一)
创建一个文件夹用作本地Git仓库(repository)
$ mkdir repository
cd到该目录,初始化该目录,之后可用Git管理
$ cd repository $ git init
在该目录可用 ls -ah看到隐藏的文件夹.git ,该文件夹用于管理仓库
添加文件到库
在repository的本地目录或子目录下,加入hello.json
$ git add hello.json
此时已经将文件添加到了暂存区,再用git commit命令提交
$ git commit -m "add hello.json" [master (root-commit) 2743d73] add hello.json 1 file changed, 1 insertion(+) create mode 100644 hello.json
其中,commit时需要添加commit信息,指令为
git commit -m "要添加的信息"
通常,要添加的信息为本次提交做了些什么
工作区状态
当hello.json被更改时,可以用 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: hello.json可见hello.json已经被修改,而要查看修改详情,使用 git diff即可
$ git diff hello.json diff --git a/hello.json b/hello.json index 2889c67..0261296 100644 --- a/hello.json +++ b/hello.json @@ -1 +1,2 @@ -Hello,world! \ No newline at end of file +Hello,world! +add a line \ No newline at end of file
再次提交更改过的hello.json,并查看状态
git add hello.json