本文提供Git的一些基本操作。
下载和安装Git
在Git test文件夹中新建两个文件web1.html和web2.html用于测试
web1.html
web2.html
在git bash中打开到项目文件夹(Git test),使用git init初始化当前文件夹为本地仓库
hp@pc MINGW64 ~
$ cd ~/Desktop/Blog/Git\ test/
hp@pc MINGW64 ~/Desktop/Blog/Git test
$ ls
web1.html web2.html
hp@pc MINGW64 ~/Desktop/Blog/Git test
$ git init
Initialized empty Git repository in C:/Users/hp/Desktop/Blog/Git test/.git/
hp@pc MINGW64 ~/Desktop/Blog/Git test
$ ls
web1.html web2.html
hp@pc MINGW64 ~/Desktop/Blog/Git test
$ git init
Initialized empty Git repository in C:/Users/hp/Desktop/Blog/Git test/.git/
使用git status查看当前项目状态,特别是文件修改情况
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
web1.html
web2.html
nothing added to commit but untracked files present (use "git add" to track)
使用git add filename添加指定文件到缓存,包括新增的文件,被修改的文件和被删除的文件
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add web1.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: web1.html
Untracked files:
(use "git add ..." to include in what will be committed)
web2.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$
此时文件web1.html已经添加到缓存,显示changes to committed可以提交。文件web2.html没有添加到缓存中,仍然属于untracked file
使用git add . 可以将当前文件夹下所有文件提交到缓存
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add .
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: web1.html
new file: web2.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$
提交前需要先指定当前的用户名和邮箱,用于后续日志中记录是谁提交的
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git config --global user.name 'Jason'
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git config --global user.email [email protected]
使用git commit可以将当前缓存区的内容提交到仓库, -m代表为当前提交添加信息,类似备注
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git commit -m 'Version one'
[master (root-commit) 0ed3e44] Version one
2 files changed, 10 insertions(+)
create mode 100644 web1.html
create mode 100644 web2.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
nothing to commit, working tree clean
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$
使用git log可以看历史的提交信息
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git log
commit 0ed3e441adc41f2edfe14e4361af84134c4ececc (HEAD -> master)
Author: Jason
Date: Wed Mar 14 01:08:48 2018 -0700
Version one
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$
下一篇:Git基本操作2