介绍

暂存区中是我们添加的变动的文件但是还没有提交,任然可以回退。在暂存区的文件是已经被git管控的,但是还未提交。

下面这张图是本文的内容流程。

Demo01

为一个html页面添加js、css等,并通过git做管理。项目结构如下:

1
2
3
4
5
6
7
8
9
10
 G:\mygitea\GitLearn 
$ tree
Folder PATH listing for volume 新加卷
Volume serial number is 406B-D0C3
G:.
├───learn01 # git仓库所在目录
└───material01 # Demo01所需要的文件所在文件夹
├───images
├───js
└───styles

初始化仓库:

1
2
3
4
5
6
7
8
9
10
11
12
13
 G:\mygitea\GitLearn 
$ cd learn01\

G:\mygitea\GitLearn\learn01 
$ git init
Initialized empty Git repository in G:/mygitea/GitLearn/learn01/.git/

G:\mygitea\GitLearn\learn01   master 
$ ls -al
total 4
drwxr-xr-x 1 WhiteCookies 197121 0 5月 5 09:13 ./
drwxr-xr-x 1 WhiteCookies 197121 0 5月 5 09:10 ../
drwxr-xr-x 1 WhiteCookies 197121 0 5月 5 09:13 .git/

创建readme

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 G:\mygitea\GitLearn\learn01   master ± 
$ vim Readme.md

G:\mygitea\GitLearn\learn01   master ± 
$ ls
Readme.md

G:\mygitea\GitLearn\learn01   master ± 
$ git add .
warning: LF will be replaced by CRLF in Readme.md.
The file will have its original line endings in your working directory

G:\mygitea\GitLearn\learn01   master ± 
$ git commit -m "add readme"
[master (root-commit) cf6aed6] add readme
1 file changed, 3 insertions(+)
create mode 100644 Readme.md

添加所需文件:

1
2
3
4
5
6
7
8
9
 G:\mygitea\GitLearn\learn01   master 
$ cp ..\material01\index.html index.html

G:\mygitea\GitLearn\learn01   master ± 
$ cp -r ..\material01\images\ .

G:\mygitea\GitLearn\learn01   master ± 
$ ls
images/ index.html Readme.md

查看git状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 G:\mygitea\GitLearn\learn01   master ± 
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
images/
index.html

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

G:\mygitea\GitLearn\learn01   master ± 
$ git add .
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directory

G:\mygitea\GitLearn\learn01   master ± 
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: images/git-logo.png
new file: index.html


G:\mygitea\GitLearn\learn01   master ± 
$ git commit -m "add html images file"
[master 94997cd] add html images file
2 files changed, 49 insertions(+)
create mode 100644 images/git-logo.png
create mode 100644 index.html

G:\mygitea\GitLearn\learn01   master 
$ git status
On branch master
nothing to commit, working tree clean

G:\mygitea\GitLearn\learn01   master 
$ git log --oneline # 简化历史修改信息
94997cd (HEAD -> master) add html images file
cf6aed6 add readme

G:\mygitea\GitLearn\learn01   master 
$ git log
commit 94997cdaabec3ed8d9b49eb0d7941fcf1c90a5e3 (HEAD -> master)
Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
Date: Thu May 5 09:19:20 2022 +0800

add html images file

commit cf6aed69070c42ad0319971578e2509991c6f5d0
Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
Date: Thu May 5 09:16:13 2022 +0800

add readme

我们点击查看index.html:

可以看到页面挺简陋的,我们再次添加点css。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 G:\mygitea\GitLearn\learn01   master 
$ mkdir styles

G:\mygitea\GitLearn\learn01   master 
$ ls
images/ index.html Readme.md styles/

G:\mygitea\GitLearn\learn01   master 
$ cp ..\material01\styles\style.css styles\style.css

G:\mygitea\GitLearn\learn01   master ± 
$ tree /f
Folder PATH listing for volume 新加卷
Volume serial number is 406B-D0C3
G:.
│ index.html
│ Readme.md

├───images
│ git-logo.png

└───styles
style.css

G:\mygitea\GitLearn\learn01   master ± 
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
styles/

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

G:\mygitea\GitLearn\learn01   master ± 
$ git add .
warning: LF will be replaced by CRLF in styles/style.css.
The file will have its original line endings in your working directory

G:\mygitea\GitLearn\learn01   master ± 
$ git commit -m "add css"
[master 750aba6] add css
1 file changed, 50 insertions(+)
create mode 100644 styles/style.css

G:\mygitea\GitLearn\learn01   master 
$ git log
commit 750aba6d48d9c7cd7de2c0c604d6c0c952379d4f (HEAD -> master)
Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
Date: Thu May 5 09:25:21 2022 +0800

add css

commit 94997cdaabec3ed8d9b49eb0d7941fcf1c90a5e3
Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
Date: Thu May 5 09:19:20 2022 +0800

add html images file

commit cf6aed69070c42ad0319971578e2509991c6f5d0
Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
Date: Thu May 5 09:16:13 2022 +0800

add readme

接下来继续查看html页面。

可以看到,添加了css后样式稍微好看了点。下面继续添加js交互。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 G:\mygitea\GitLearn\learn01   master 
$ cp -r ..\material01\js\ .

G:\mygitea\GitLearn\learn01   master ± 
$ tree /f
Folder PATH listing for volume 新加卷
Volume serial number is 406B-D0C3
G:.
│ index.html
│ Readme.md

├───images
│ git-logo.png

├───js
│ script.js

└───styles
style.css

G:\mygitea\GitLearn\learn01   master ± 
$ git add .
warning: LF will be replaced by CRLF in js/script.js.
The file will have its original line endings in your working directory

G:\mygitea\GitLearn\learn01   master ± 
$ git commit -m "add js"
[master 3fba203] add js
1 file changed, 15 insertions(+)
create mode 100644 js/script.js

G:\mygitea\GitLearn\learn01   master 
$ git log --graph # 以图形化的形式查看git分支
* commit 3fba2037053d4ee6159d4068dfdf32419cdd9a8e (HEAD -> master)
| Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
| Date: Thu May 5 09:30:52 2022 +0800
|
| add js
|
* commit 750aba6d48d9c7cd7de2c0c604d6c0c952379d4f
| Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
| Date: Thu May 5 09:25:21 2022 +0800
|
| add css
|
* commit 94997cdaabec3ed8d9b49eb0d7941fcf1c90a5e3
| Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
| Date: Thu May 5 09:19:20 2022 +0800
|
| add html images file
|
* commit cf6aed69070c42ad0319971578e2509991c6f5d0
Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
Date: Thu May 5 09:16:13 2022 +0800

add readme

接下来,我们在html文件里添加信息:

接着编辑css样式,调整附加信息布局:

查看html页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 G:\mygitea\GitLearn\learn01   master ± 
$ 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: index.html
modified: styles/style.css

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

G:\mygitea\GitLearn\learn01   master ± 
$ git add -u # 将已经被git管控的文件一起提交

G:\mygitea\GitLearn\learn01   master ± 
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
modified: styles/style.css

G:\mygitea\GitLearn\learn01   master ± 
$ git commit -m "add modified html css"
[master 6dc251b] add modified html css
2 files changed, 22 insertions(+), 1 deletion(-)

到目前为止,静态页面已经完工。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 G:\mygitea\GitLearn\learn01   master 
$ git log --graph
* commit 6dc251bdb1b00c3d6c9f6f57fab74bb9b465d53c (HEAD -> master)
| Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
| Date: Thu May 5 09:43:29 2022 +0800
|
| add modified html css
|
* commit 3fba2037053d4ee6159d4068dfdf32419cdd9a8e
| Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
| Date: Thu May 5 09:30:52 2022 +0800
|
| add js
|
* commit 750aba6d48d9c7cd7de2c0c604d6c0c952379d4f
| Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
| Date: Thu May 5 09:25:21 2022 +0800
|
| add css
|
* commit 94997cdaabec3ed8d9b49eb0d7941fcf1c90a5e3
| Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
| Date: Thu May 5 09:19:20 2022 +0800
|
| add html images file
|
* commit cf6aed69070c42ad0319971578e2509991c6f5d0
Author: GitHub Whiteco-okie <GitHub 1961663351@qq.com>
Date: Thu May 5 09:16:13 2022 +0800

add readme

将项目上传到远程仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 设置用户
G:\mygitea\GitLearn\learn01   master 
$ git config --local user.name "Jabari"

G:\mygitea\GitLearn\learn01   master 
$ git config --local user.email "innocenfox@gmail.com"

# 关联到远程仓库
G:\mygitea\GitLearn\learn01   master 
$ git remote add origin ssh://git@gitea.everweekup.com:222/Jabari/GitLearn.git
# 本地仓库和远程仓库同步合并
G:\mygitea\GitLearn\learn01   master 
$ git pull --rebase origin master
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 2.58 KiB | 264.00 KiB/s, done.
From ssh://gitea.everweekup.com:222/Jabari/GitLearn
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Successfully rebased and updated refs/heads/master.

# 把本地库的内容推送到远程(执行此命令后会要求输入用户名、密码,验证通过后即开始上传)
# 这里我添加了ssh,直接上传
G:\mygitea\GitLearn\learn01   master ± 
$ git push -u origin master
Enumerating objects: 22, done.
Counting objects: 100% (22/22), done.
Delta compression using up to 16 threads
Compressing objects: 100% (16/16), done.
Writing objects: 100% (21/21), 21.72 KiB | 1.09 MiB/s, done.
Total 21 (delta 6), reused 0 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To ssh://gitea.everweekup.com:222/Jabari/GitLearn.git
df647fe..b390c28 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

总结

流程:

  1. 初始化git仓库;
  2. 添加文件进入git仓库,查看文件无误后添加进git管控git add -u / git add .
  3. 提交git commit -m ""
  4. 之后若有修改重复上述流程。