Git操作实验手册 - 本地常用命令(一)

本文通过模拟一个简单 nodejs 应用构建过程来演示常用的 git 命令。

准备实验目录

1
2
3
mkdir todo-srv
cd todo-srv
git init
1
Initialized empty Git repository in /root/todo-srv/.git/

使用 ls -la 命令,可以看到当前目录中多了一个隐藏目录 .git, 这个目录是Git来跟踪管理版本的。

初始化node项目

1
npm init

目录中多了 package.json 文件

新增名为 server.js 的文件,内容如下:

1
2
3
4
5
6
7
8
9
var http = require('http');

http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('ok');
res.end();
}).listen(3000);

console.log("HTTP server is listening at port 3000.");

检查当前的 git 状态

1
git status

系统显示:

1
2
3
4
5
6
7
8
9
10
11
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)

package.json
server.js

nothing added to commit but untracked files present (use "git add" to track)

可以看到,新增的两个文件 package.json 和 server.js 都没有加入到git仓库中。

把文件加入仓库

在当前目录运行:

1
git add .

命令,将当前目录中新增的文件加入到 git 本地仓库中。 再查看状态

1
git status

系统显示

1
2
3
4
5
6
7
8
9
On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: package.json
new file: server.js

提交变更

1
git commit -m "init"
1
2
3
4
5
6
7
8
9
*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.
1
2
git config --global user.email "root@abc.com"
git config --global user.name "root"

再次提交,

1
git commit -m "init"

系统提示:

1
2
3
4
[master (root-commit) 455d5c9] init
2 files changed, 20 insertions(+)
create mode 100644 package.json
create mode 100644 server.js

再次使用 git status 查询状态,可以看到

1
2
On branch master
nothing to commit, working tree clean

更改 server.js

1
2
3
4
5
6
7
8
9
10
11
12
var http = require('http');

http.createServer(function(req, res) {

var ret = { succ: true, code:'', message:'', data:''}

res.writeHead(200, {'Content-Type': 'text/html'});
res.write(JSON.stringify(ret));
res.end();
}).listen(3000);

console.log("HTTP server is listening at port 3000.");
1
2
3
4
5
6
7
8
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: server.js

no changes added to commit (use "git add" and/or "git commit -a")

提交修改,执行

1
2
git add server.js
git commit -m "change the format of result to json"

在执行 git status, 可以没有更改但未提交的文件。

使用git log 查看更改历史

在目录中执行:

1
git log

系统提示:

1
2
3
4
5
6
7
8
9
10
11
[[33mcommit 290a26658acbac2cca19232e06f434acd6ca65b9[[m[[33m ([[m[[1;36mHEAD -> [[m[[1;32mmaster[[m[[33m)[[m
Author: root <root@abc.com>
Date: Wed Nov 13 12:51:46 2019 +0000

change the format of result to json

[[33mcommit 455d5c9f8294b351cc16e14ea4570e604bb1a084[[m
Author: root <root@abc.com>
Date: Wed Nov 13 12:35:10 2019 +0000

init

再次改变

将输出类型改为 “application/json”

1
res.writeHead(200, {'Content-Type': 'application/json'});

提交改变,然后通过 git log 查看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[[33mcommit 331b541afdbe1646d297b3a129cec2d51414ec23[[m[[33m ([[m[[1;36mHEAD -> [[m[[1;32mmaster[[m[[33m)[[m
Author: root <root@abc.com>
Date: Wed Nov 13 13:38:41 2019 +0000

change the content-type

[[33mcommit 290a26658acbac2cca19232e06f434acd6ca65b9[[m
Author: root <root@abc.com>
Date: Wed Nov 13 12:51:46 2019 +0000

change the format of result to json

[[33mcommit 455d5c9f8294b351cc16e14ea4570e604bb1a084[[m
Author: root <root@abc.com>
Date: Wed Nov 13 12:35:10 2019 +0000

init

回退到版本

要回退到上一个版本,执行:

1
git reset --hard HEAD^

系统显示:

1
HEAD is now at 290a266 change the format of result to json

查看文件内容, 可以看到,Content-Type已经恢复为: ‘text/html’

1
res.writeHead(200, {'Content-Type': 'text/html'});

查看 git log, 可以看到最后一次修改被回退了。

执行:

1
git reflog
1
2
3
4
[[33m290a266[[m[[33m ([[m[[1;36mHEAD -> [[m[[1;32mmaster[[m[[33m)[[m HEAD@{0}: reset: moving to HEAD^
[[33m331b541[[m HEAD@{1}: commit: change the content-type
[[33m290a266[[m[[33m ([[m[[1;36mHEAD -> [[m[[1;32mmaster[[m[[33m)[[m HEAD@{2}: commit: change the format of result to json
[[33m455d5c9[[m HEAD@{3}: commit (initial): init

可以查看到版本的内部id

回复到 “331b541” 版,执行:

1
git reset --hard 331b541

系统提示:

1
HEAD is now at 331b541 change the content-type

参考文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
var http = require('http');

http.createServer(function(req, res) {

var ret = { succ: true, code:'', message:'', data:''}

res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(ret));
res.end();
}).listen(3000);

console.log("HTTP server is listening at port 3000.");

执行:

1
git log

可以看到如下结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[[33mcommit 331b541afdbe1646d297b3a129cec2d51414ec23[[m[[33m ([[m[[1;36mHEAD -> [[m[[1;32mmaster[[m[[33m)[[m
Author: root <root@abc.com>
Date: Wed Nov 13 13:38:41 2019 +0000

change the content-type

[[33mcommit 290a26658acbac2cca19232e06f434acd6ca65b9[[m
Author: root <root@abc.com>
Date: Wed Nov 13 12:51:46 2019 +0000

change the format of result to json

[[33mcommit 455d5c9f8294b351cc16e14ea4570e604bb1a084[[m
Author: root <root@abc.com>
Date: Wed Nov 13 12:35:10 2019 +0000

init

再次执行 git reflog 进行比较:

1
2
3
4
5
[[33m331b541[[m[[33m ([[m[[1;36mHEAD -> [[m[[1;32mmaster[[m[[33m)[[m HEAD@{0}: reset: moving to 331b541
[[33m290a266[[m HEAD@{1}: reset: moving to HEAD^
[[33m331b541[[m[[33m ([[m[[1;36mHEAD -> [[m[[1;32mmaster[[m[[33m)[[m HEAD@{2}: commit: change the content-type
[[33m290a266[[m HEAD@{3}: commit: change the format of result to json
[[33m455d5c9[[m HEAD@{4}: commit (initial): init

理解暂存区

修改 server.js 文件,在文件中增加一行,申明一个 data 对象,并插入到 res 的结果中。 修改后的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var http = require('http');

http.createServer(function(req, res) {

var ret = { succ: true, code:'', message:'', data:''}
var data = { id: 1, title: 'call tom' }
ret.data = data

res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(ret));
res.end();
}).listen(3000);

console.log("HTTP server is listening at port 3000.");

执行:

1
git status

系统提示:

1
2
3
4
5
6
7
8
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: server.js

no changes added to commit (use "git add" and/or "git commit -a")

使用 add 命令将改变提交到暂存区

1
git add server.js

继续修改 server.js 文件,加入在 data 中添加一个属性:

1
2
var data = { id: 1, title: 'call tom', desc: 'call tom and jack' }
`

再次用 git status 命名查看状态,看到如下结果:

1
2
3
4
5
6
7
8
9
10
11
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: server.js

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: server.js

这时如果要放弃上一步的改动,可以直接运行:

1
git checkout server.js

再次查看 server.js 的内容,可以看到内容已经恢复了。

如果需要撤销在暂存区的修改,可以使用 rest 指令, 如下:

1
git reset server.js

系统显示:

1
2
Unstaged changes after reset:
M server.js

本文标题:Git操作实验手册 - 本地常用命令(一)

文章作者:梅老师

发布时间:2019年11月13日 - 07:11

最后更新:2020年09月16日 - 08:09

原始链接:https://www.mls-tech.info/git/git-common-operation/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。