第2章 Git 常用命令
2.1 设置用户签名
命令名称 | 作用 |
---|---|
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,一次确认本次提交是谁做的。Git 首次安装必须设置一下用户签名,否则无法提交代码。
注意:这里设置用户签名和将来登录 GitHub(或其它代码托管中心)的账号没有任何关系。
2.2 初始化本地库
1)基本语法
git init
2)案例实操
PS E:\rep\git-demo> git init
Initialized empty Git repository in E:/rep/git-demo/.git/
2.3 查看本地库状态
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
添加一个文件 hello.txt
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
2.4 添加暂存区
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in hello.txt. 这句话的意思是git自动转换了行末换行符,将LF 转换为CRLF,CRLF是我的工作目录下的默认换行符。
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
$ git rm --cached hello.txt
rm 'hello.txt'
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
$ ll
total 1
-rw-r--r-- 1 mcj 197121 400 5月 18 22:28 hello.txt
2.5 提交本地库
git commit -m "日志信息" 文件名
$ git commit -m "first commit"hello.txt
[master (root-commit) fe5f492] first commithello.txt
1 file changed, 16 insertions(+)
create mode 100644 hello.txt
$ git status
On branch master
nothing to commit, working tree clean
2.6 修改文件
git 按行维护文件
$ git commit -m "second commit" hello.txt
[master e0a6e56] second commit
1 file changed, 1 insertion(+), 1 deletion(-)
第3次修改
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add hello.txt
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
$ git commit -m "third commit" hello.txt
[master e210c7f] third commit
1 file changed, 1 insertion(+), 1 deletion(-)
查看版本信息:
$ git reflog
e210c7f (HEAD -> master) HEAD@{0}: commit: third commit
e0a6e56 HEAD@{1}: commit: second commit
fe5f492 HEAD@{2}: commit (initial): first commithello.txt
2.7 历史版本
2.7.1 查看历史版本
1 基本语法
git reflog
查看版本信息
git log
查看版本详细信息
2.7.2 版本穿梭
git reset --hard <版本号>
$ git reset --hard e0a6e56
HEAD is now at e0a6e56 second commit
$ git reflog
e0a6e56 (HEAD -> master) HEAD@{0}: reset: moving to e0a6e56
e210c7f HEAD@{1}: commit: third commit
e0a6e56 (HEAD -> master) HEAD@{2}: commit: second commit
fe5f492 HEAD@{3}: commit (initial): first commithello.txt