git

git常用命令

基础

命令 作用
git init 初始化本地库
git status 查看本地库状态有哪些改动
git config –global user.name <用户名> 设置用户签名
git config –global user.email <邮箱> 设置用户签名
git add <文件名/.> 添加到索引(.代表当前文件夹中所有)
git commit -m [说明] -a 记录变更到仓库,附带提交说明,对未进行git add文件无效
git reflog 查看commit历史记录
git log 查看版本详细信息
git reset –hard <版本号> 版本穿梭
git –version 查看当前git版本

分支

命令 作用
git branch <分支名> 创建分支
git branch -v 查看已有分支
git branch -r 查看远程分支
git branch -a 查看已有&远程分支
git checkout <分支名> 切换分支
git merge <分支名> 把指定的分支合并到当前分支上
git branch -d [-D 强制] <分支名> 删除本地分支

对比

命令 作用
git diff 列出所有文件改动
git diff <文件路径> 对比某文件改动

git签名

签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。Git首次安装必须设置一下用户签名,否则无法提交代码。

注意:这里设置用户签名和将来登录GitHub(或其他代码托管中心)的账号没有任何关系。

1
2
3
4
5
6
 ~/ git config --global user.name fuding
 ~/ git config --global user.email f_ding@126.com
 ~/ cat ~/.gitconfig #设置之后就可以在这个配置文件里找到
[user]
name = fuding
email = f_ding@126.com

git初始化本地库

1
2
3
4
5
6
7
8
9
 ~/Desktop/笔记/git_demo/ ls -al 
drwxr-xr-x@ 4 fuding staff 128 Nov 29 19:18 .
drwxr-xr-x@ 6 fuding staff 192 Nov 29 19:18 ..
 ~/Desktop/笔记/git_demo/ git init #此命令生成.git文件夹 即:初始化本地库
Initialized empty Git repository in /Users/fuding/Desktop/笔记/git_demo/.git/
 ~/Desktop/笔记/git_demo/ [master] ls -al
drwxr-xr-x@ 4 fuding staff 128 Nov 29 19:18 .
drwxr-xr-x@ 6 fuding staff 192 Nov 29 19:18 ..
drwxr-xr-x@ 9 fuding staff 288 Nov 29 19:18 .git

查看本地库状态

首次查看状态

1
2
3
4
5
6
 ~/Desktop/笔记/git_demo/ [master] git status #除了.git没有任何文件
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

新增代码文件&再次查看状态

1
2
3
4
5
6
7
8
9
10
11
 ~/Desktop/笔记/git_demo/ [master] touch new.java
 ~/Desktop/笔记/git_demo/ [master] git status
On branch master

No commits yet

Untracked files: #此处监测到git未追踪的文件
(use "git add <file>..." to include in what will be committed)
new.java

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

将代码添加到索引

此处无新版本的创建

添加到索引(Index)可理解为将文件内容添加至git工作树,即Git的版本控制管理中跟踪它们。

1
2
3
4
5
6
7
8
9
 ~/Desktop/笔记/git_demo/ [master] git add new.java  #空代码文件添加到索引
 ~/Desktop/笔记/git_demo/ [master+] git status #+号代表已add未commit
On branch master

No commits yet

Changes to be committed: #已添加到暂存区
(use "git rm --cached <file>..." to unstage)
new file: new.java

git add:将写在工作树中的修改,存储到 Git 暂存区,从而将更改加入到下一次提交中。当你运行 git add 命令时,Git会将指定文件的修改保存到一个名为“index”的区域中,类似于一个临时的存储区域。

提交本地库

注意:提交本地库也意味着新版本的创建,切换版本一定要进行git commit,不然git add的部分代码将丢失!!

1
2
3
4
5
6
7
8
 ~/Desktop/笔记/git_demo/ [master+] git commit -m "我的helloword代码。" new.java # -m 【定义日志信息】文件名称一定要与之前一致
[master (root-commit) 6ed6d5c] 我的helloword代码。
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 new.java
 ~/Desktop/笔记/git_demo/ [master] git status

On branch master
nothing to commit, working tree clean

修改文件再次提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 ~/Desktop/笔记/git_demo/ [master] vim new.java
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
}
}
~
~
"new.java" 5L, 98C
 ~/Desktop/笔记/git_demo/ [master*] javac new.java
 ~/Desktop/笔记/git_demo/ [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: new.java

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

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

git commit:将暂存区保存的所有更改(使用 git add 命令添加的更改)捆绑为一次新的提交并保存到本地Git仓库中。Git会创建一个新的提交对象来保存这些更改,并根据时间戳和作者等其他元数据信息来标记提交。Git还会将这个新的提交对象链接到先前的提交对象上,形成一个提交历史记录。

查看历史版本

1
2
3
4
5
6
7
8
9
10
 ~/Desktop/笔记/git_demo/ [master*] git reflog  #查看版本信息
6ed6d5c (HEAD -> master) HEAD@{0}: commit (initial): 我的helloword代码。
(END)
 ~/Desktop/笔记/git_demo/ [master*] git reflog #查看版本详细信息
commit 6ed6d5cb2cb5706f38532fb92f8efbefc972928c (HEAD -> master)
Author: fuding <f_ding@126.com>
Date: Mon Nov 29 19:39:33 2021 +0800

我的helloword代码。
(END)

版本切换(版本穿梭)

Git 切换版本,底层其实是移动的HEAD 指针。

注意:切换版本的时候一定要进行add和commit操作,不然未追踪文件或者更改文件将会丢失!!

1
2
3
4
5
6
7
8
9
10
 ~/Desktop/笔记/git_demo/ [master*] git reflog  #查看版本信息
6ed6d5c (HEAD -> master) HEAD@{0}: commit (initial): 我的helloword代码。 #最初的版本(未切换到此版本状态)
(END)
 ~/Desktop/笔记/git_demo/ [master] git reset --hard 6ed6d5c #切换到最初版本
 ~/Desktop/笔记/git_demo/ [master] git reflog
HEAD is now at 6ed6d5c 我的helloword代码。
6ed6d5c (HEAD -> master) HEAD@{1}: reset: moving to 6ed6d5c #说明已经切换到最初版本
6ed6d5c (HEAD -> master) HEAD@{2}: commit (initial): 我的helloword代码。
(END)

add commit实验

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
~/Desktop/笔记/hello word/ ls #创建了一个新的空文件夹hello word
 ~/Desktop/笔记/hello word/ git init
Initialized empty Git repository in /Users/fuding/Desktop/笔记/hello word/.git/
 ~/Desktop/笔记/hello word/ [master] vim new.java
 ~/Desktop/笔记/hello word/ [master] git reflog
fatal: your current branch 'master' does not have any commits yet
 ~/Desktop/笔记/hello word/ [master] git commit -m "我的helloword代码。" new.java
error: pathspec 'new.java' did not match any file(s) known to git #此处说明不进行add不能commit
 ~/Desktop/笔记/hello word/ [master] git add new.java #准备添加到暂存区进行commit
 ~/Desktop/笔记/hello word/ [master+] git commit -m "我的helloword代码。" new.java #成功添加到本地库
[master (root-commit) 9ec2632] 我的helloword代码。
1 file changed, 5 insertions(+)
create mode 100644 new.java

 ~/Desktop/笔记/hello word/ [master] touch Hello.class
 ~/Desktop/笔记/hello word/ [master] ls
Hello.class new.java
 ~/Desktop/笔记/hello word/ [master] git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
Hello.class

nothing added to commit but untracked files present (use "git add" to track)
 ~/Desktop/笔记/hello word/ [master] git add Hello.class
 ~/Desktop/笔记/hello word/ [master+] touch Hello #+说明添加到了暂存区未进行提交到本地库
 ~/Desktop/笔记/hello word/ [master+] ls
Hello Hello.class new.java
 ~/Desktop/笔记/hello word/ [master+] vim Hello.class
 ~/Desktop/笔记/hello word/ [master+*] git add Hello.class #说明暂存区文件进行了修改且包含未commit内容

 ~/Desktop/笔记/hello word/ [master+] vim Hello #修改Hello(未添加到暂存区)看是否会出现+号
 ~/Desktop/笔记/hello word/ [master+] git commit Hello.class
Aborting commit due to empty commit message.
 ~/Desktop/笔记/hello word/ [master+] git commit -m "去掉+测试" Hello.class
[master 560c08b] 去掉+测试
1 file changed, 1 insertion(+)
create mode 100644 Hello.class
 ~/Desktop/笔记/hello word/ [master] ls #+被去掉,证明只是被add的内容修改未提交才会出现+号(即:untracked files)
Hello Hello.class new.java

 ~/Desktop/笔记/hello word/ [master] git reflog #查看commit版本
 ~/Desktop/笔记/hello word/ [master] git reset --hard 560c08b #切换版本,为了证明untracked files会不会消失
HEAD is now at 560c08b 去掉+测试
 ~/Desktop/笔记/hello word/ [master] ls #切换版本不会使untracked files消失
Hello Hello.class new.java
 ~/Desktop/笔记/hello word/ [master] git add Hello #如果add添加到暂存区,即:添加到内存
 ~/Desktop/笔记/hello word/ [master+] ls
Hello Hello.class new.java
 ~/Desktop/笔记/hello word/ [master+] git reflog
 ~/Desktop/笔记/hello word/ [master+] git reset --hard 560c08b #再次切换版本,就会丢失add文件但未commit的文件
HEAD is now at 560c08b 去掉+测试
 ~/Desktop/笔记/hello word/ [master] ls #丢失Hello文件!!
Hello.class new.java
######以下过程证明只有commit过的才能进行不同版本的保存且保存成每次commit的状态######
 ~/Desktop/笔记/hello word/ [master] git reflog
560c08b HEAD@{2}: reset: moving to 560c08b
560c08b HEAD@{3}: commit: 去掉+测试
9ec2632 (HEAD -> master) HEAD@{4}: commit (initial): 我的helloword代码。
(END)
 ~/Desktop/笔记/hello word/ [master] git reset --hard 9ec2632
HEAD is now at 9ec2632 我的helloword代码。
 ~/Desktop/笔记/hello word/ [master] ls
new.java
 ~/Desktop/笔记/hello word/ [master] cat new.java
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
}
}
 ~/Desktop/笔记/hello word/ [master] git reflog
9ec2632 (HEAD -> master) HEAD@{0}: reset: moving to 9ec2632
560c08b HEAD@{1}: reset: moving to 560c08b
560c08b HEAD@{3}: commit: 去掉+测试
9ec2632 (HEAD -> master) HEAD@{4}: commit (initial): 我的helloword代码。
(END)
 ~/Desktop/笔记/hello word/ [master] ls
new.java
 ~/Desktop/笔记/hello word/ [master] git reset --hard 560c08b
HEAD is now at 560c08b 去掉+测试
 ~/Desktop/笔记/hello word/ [master] ls
Hello.class new.java

git分支操作

分支:在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本。(分支底层其实也是指针的引用)

分支的好处

  • 同时并行推进多个功能开发,提高开发效率。

  • 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。

  • 分工进行代码的开发、测试、运维、上线

查看分支

1
2
3
4
 ~/Desktop/笔记/git_demo/ [master+*] git branch -v #既有+又有*代表有新的代码文件未进行add & 有add后的文件进行了修改且未进行add,从未进行过add的代码修改不会出现*号,add之前add过的文件后*消失,add新文件后+消失。
* master 6ed6d5c 我的helloword代码。 #星号表示在当前分支上
(END)

创建分支&切换分支

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
 ~/Desktop/笔记/hello word/ [master] git reflog
9ec2632 (HEAD -> master) HEAD@{0}: reset: moving to 9ec2632
560c08b HEAD@{1}: reset: moving to 560c08b
560c08b HEAD@{3}: commit: 去掉+测试 #新版本560c08b
9ec2632 (HEAD -> master) HEAD@{4}: commit (initial): 我的helloword代码。 #旧版本9ec2632
(END)
 ~/Desktop/笔记/hello word/ [master] git reset --hard 9ec2632 #切换到旧版本,此处为了证明切换到旧版本时创建分支会不会将新版本也copy到新的分支当中
HEAD is now at 9ec2632 我的helloword代码。
 ~/Desktop/笔记/hello word/ [master] ls
new.java
 ~/Desktop/笔记/hello word/ [master] git branch hot-fix #创建分支 hot-fix
 ~/Desktop/笔记/hello word/ [master] git branch -v
hot-fix 560c08b 去掉+测试
* master 560c08b 去掉+测试
(END)
 ~/Desktop/笔记/hello word/ [master] ls #处于master旧版本
new.java
 ~/Desktop/笔记/hello word/ [master] git reset --hard 560c08b #切换到master新版
HEAD is now at 560c08b 去掉+测试
 ~/Desktop/笔记/hello word/ [master] ls
Hello.class new.java
 ~/Desktop/笔记/hello word/ [master] git reset --hard 9ec2632 #mastet旧版
HEAD is now at 9ec2632 我的helloword代码。
 ~/Desktop/笔记/hello word/ [master] ls
new.java
 ~/Desktop/笔记/hello word/ [master] git checkout hot-fix #切换分支到 hot-fix
Switched to branch 'hot-fix'
 ~/Desktop/笔记/hello word/ [hot-fix] ls #处于hot-fix 旧版
new.java
 ~/Desktop/笔记/hello word/ [hot-fix] git checkout master
Switched to branch 'master'
 ~/Desktop/笔记/hello word/ [master] ls
new.java

 ~/Desktop/笔记/hello word/ [master] git reset --hard 560c08b #切换到master新版
HEAD is now at 560c08b 去掉+测试
 ~/Desktop/笔记/hello word/ [master] ls
Hello.class new.java
 ~/Desktop/笔记/hello word/ [master] git checkout hot-fix #切换到hot-fix,还是处于旧版本
Switched to branch 'hot-fix'
 ~/Desktop/笔记/hello word/ [hot-fix] ls
new.java


以上操作说明两个分支之间的操作不会互相影响。

2023-5-16实验:

  1. 此时有一个未进行git add .git commit代码的主分支master
  2. 创建一个新的分支,名为feature,此时在feature分支下也是显示未git add&git commit
  3. 切换到master,进行了git add .git commit代码
  4. 再次切换到feature,此时一样显示没有需要git add .git commit的代码
  5. 切换到feature,是master的前一个版本
  6. 查看VScode依旧是feature版本,查看代码是master的前一个版本
  7. 如果在VScode命令行下切换分支,VScode显示的代码瞬间改变另一个分支。

修改分支

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
64
65
66
67
68
69
70
71
72
73
######以下证实两个分支上的最新版本内容都相同######
 ~/Desktop/笔记/hello word/ [master] ls #处于master分区的新版本上
Hello.class new.java
 ~/Desktop/笔记/hello word/ [master] vim new.java
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
System.out.println("master 分区上做修改。");
}
}
 ~/Desktop/笔记/hello word/ [master*] git commit -m "master的新版本上做修改" new.java #由于之前在master上已经add过new.java 所以此时不用直接add,*
[master f9d4193] master的新版本上做修改
1 file changed, 1 insertion(+)
 ~/Desktop/笔记/hello word/ [master] git branch -v
hot-fix 560c08b 去掉+测试
* master f9d4193 master的新版本上做修 #处于master的最新版本上
 ~/Desktop/笔记/hello word/ [master] git reflog
f9d4193 (HEAD -> master) HEAD@{0}: commit: master的新版本上做修改 #更新版本
560c08b (hot-fix) HEAD@{1}: checkout: moving from hot-fix to master
560c08b (hot-fix) HEAD@{2}: reset: moving to 560c08b #新版本
9ec2632 HEAD@{3}: reset: moving to 9ec2632
9ec2632 HEAD@{4}: checkout: moving from master to hot-fix #旧版本
560c08b (hot-fix) HEAD@{5}: reset: moving to 560c08b

 ~/Desktop/笔记/hello word/ [master] git checkout hot-fix # 切换另一个分支
Switched to branch 'hot-fix'
 ~/Desktop/笔记/hello word/ [hot-fix] git reset --hard f9d4193 #hot-fix也出现更新版本
HEAD is now at f9d4193 master的新版本上做修改
 ~/Desktop/笔记/hello word/ [hot-fix] ls
Hello.class new.java
 ~/Desktop/笔记/hello word/ [hot-fix] cat new.java
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
System.out.println("master 分区上做修改。");
}
}
 ~/Desktop/笔记/hello word/ [hot-fix] git reflog
f9d4193 (HEAD -> hot-fix, master) HEAD@{0}: reset: moving to f9d4193 #证实hot-fix也出现更新版本
560c08b (v) HEAD@{1}: checkout: moving from master to hot-fix
f9d4193 (HEAD -> hot-fix, master) HEAD@{2}: commit: master的新版本上做修改

######以下各进行对两个不同分支的new.java修改######
 ~/Desktop/笔记/hello word/ [master] vim new.java
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
System.out.println("master 分区上做修改。");
System.out.println("master分支最新版本上对new.java做修改。");
}
}
 ~/Desktop/笔记/hello word/ [master*] git commit -m "master分支的新版本上的new.java做修改" new.java
[master 69d7418] master分支的新版本上的new.java做修改
1 file changed, 1 insertion(+)

 ~/Desktop/笔记/hello word/ [master] git checkout hot-fix
Switched to branch 'hot-fix'
 ~/Desktop/笔记/hello word/ [hot-fix] ls
Hello.class new.java

 ~/Desktop/笔记/hello word/ [hot-fix] vim new.java
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
System.out.println("master 分区上做修改。");
System.out.println("hot-fix分支最新版本上对new.java做修改。");
}
}
 ~/Desktop/笔记/hello word/ [hot-fix*] git commit -m "hot-fix分支的新版本上的new.java做修改" new.java
[hot-fix bdc5af2] hot-fix分支的新版本上的new.java做修改
1 file changed, 1 insertion(+)


合并分支

特殊符号:<<<<<<< HEAD(master) {当前分支的代码块}

======= {合并过来的代码块} >>>>>>> hot-fix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 ~/Desktop/笔记/hello word/ [hot-fix] git checkout master 
Switched to branch 'master'
 ~/Desktop/笔记/hello word/ [master] git merge hot-fix #合并分支hot-fix到当前master分支命令
Auto-merging new.java
CONFLICT (content): Merge conflict in new.java
Automatic merge failed; fix conflicts and then commit the result.
 ~/Desktop/笔记/hello word/ [master|merge+*] cat new.java
quit
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
System.out.println("master 分区上做修改。");
<<<<<<< HEAD
System.out.println("master 最新版本上对new.java做修改。");
=======
System.out.println("hot-fix分支最新版本上对new.java做修改。");
>>>>>>> hot-fix
}
}

解决冲突

解决冲突:编辑有冲突的文件,删除特殊符号,决定要使用的内容。

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
 ~/Desktop/笔记/hello word/ [master|merge+*] vim new.java
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
System.out.println("master 分区上做修改。");

System.out.println("master 最新版本上对new.java做修改。");

System.out.println("hot-fix分支最新版本上对new.java做修改。");

}
}
~
~
-- INSERT --
 ~/Desktop/笔记/hello word/ [master|merge+*] git add new.java #合并分支生成新的文件new.java 于是显示+,new.java 文件且未进行add
 ~/Desktop/笔记/hello word/ [master|merge+] git commit -m "nerge hot-fix" new.java #一定要进行add,毕竟有新文件
fatal: cannot do a partial commit during a merge.
 ~/Desktop/笔记/hello word/ [master|merge+] git commit -m "nerge hot-fix" #一定要进行commit,毕竟有原文件进行了更改
[master ace2e5d] nerge hot-fix
 ~/Desktop/笔记/hello word/ [master] ls
Hello.class new.java
 ~/Desktop/笔记/hello word/ [master] cat new.java #只有master上的文件进行了合并更改
quit
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
System.out.println("master 分区上做修改。");

System.out.println("master 最新版本上对new.java做修改。");

System.out.println("hot-fix分支最新版本上对new.java做修改。");

}
}

 ~/Desktop/笔记/hello word/ [master] git checkout hot-fix #此处未进行合并更改,还是以前更改的内容
Switched to branch 'hot-fix'
 ~/Desktop/笔记/hello word/ [hot-fix] ls
Hello.class new.java
 ~/Desktop/笔记/hello word/ [hot-fix] cat new.java
quit
class Hello {
public static void main (String args[]){
System.out.println("Hello Java!!");
System.out.println("master 分区上做修改。");
System.out.println("hot-fix分支最新版本上对new.java做修改。");
}
}


github&gitee

远程仓库操作

去gitee创建一个仓库,直接可以看到如何进行

命令名称 作用
git remote -v 查看所有当前自定义远程仓库地址别名
git remote add <别名> <仓库地址> 为仓库地址起别名(方便操作仓库链接)
git remote remove <别名> 根据仓库删除别名仓库
git remote set-url 修改名称的远程的
git push <别名> <分支> [–force] 推送本地分支上的内容到制定仓库制定分支,–force 强制推送(会覆盖所有原有的代码,差异非常大时使用)
git clone <地址> 将远程仓库的main分支克隆到本地仓库
git clone -b <分支名> <地址> 克隆其他分支到本地仓库
git fetch 从远程仓库下载最新的提交和数据到本地,会将远程仓库的最新提交下载到本地的一个特殊的分支(通常是 origin/master 或者远程分支的名称),但不会自动合并这些更新到当前分支
git pull [远程仓库地址别名] [远程分支名] 执行 git fetch 后立即执行 git merge,将远程仓库的更新合并到当前分支。

模板项目初始化

直接可更改模板项目的名称,并将项目根目录下的.git文件夹删除,再进行git初始化即可。

1
2
3
4
5
6
7
8
9
10
# 重新初始化
git init
# 参看远程仓库别名设置
git remote -v
# 添加别名远程仓库连接
git remote add <别名,默认origin> <别名地址>
# 所有文件添加至暂存区
git add .
# 提交本地库
git commit -m "网页制作" -a

创建一个新的本地库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    ~/桌面  cd new-mater                                                                                                                         ✔ 
   ~/桌面/new-mater  git init  ✔
提示:使用 'master' 作为初始分支的名称。这个默认分支名称可能会更改。要在新仓库中
提示:配置使用初始分支名,并消除这条警告,请执行:
提示:
提示: git config --global init.defaultBranch <名称>
提示:
提示:除了 'master' 之外,通常选定的名字有 'main''trunk''development'
提示:可以通过以下命令重命名刚创建的分支:
提示:
提示: git branch -m <name>
已初始化空的 Git 仓库于 /home/fuding/桌面/new-mater/.git/
   ~/桌面/new-mater    master  git reflog  ✔  5s 
fatal: 您的当前分支 'master' 尚无任何提交

设置本地库远程链接别名

1
2
3
4
5
    ~/桌面/new-mater    master  git remote -v   #查看本地库中定义的链接别名,新创建的本地库不含有任何别名链接等信息                                                                                128 ✘ 
   ~/桌面/new-mater    master  git remote add test-git https://gitee.com/ding-f/test-git.git  ✔ #添加一条别名为test-git的远程库链接
   ~/桌面/new-mater    master  git remote -v  ✔ #再次显示本地库别名链接配置单
test-git https://gitee.com/ding-f/test-git.git (fetch)
test-git https://gitee.com/ding-f/test-git.git (push)

向远程库推送代码

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
    ~/桌面/new-mater    master  vim README.md                                                                                                 ✔ 
# 测试
~
~
   ~/桌面/new-mater    master ?1  git add README.md  ✔  20s 
   ~/桌面/new-mater    master +1  git commit -m "我的第一个提交!!"  1 ✘
[master(根提交) 68447e5] 我的第一个提交!!
1 file changed, 1 insertion(+)
create mode 100644 README.md
   ~/桌面/new-mater    master  git push -u test-git master  ✔ #新建的远程仓库教程里push多了个-u 其实上面的表格没有也一样可以哦
Username for 'https://gitee.com': ding-f
Password for 'https://ding-f@gitee.com':
枚举对象中: 3, 完成.
对象计数中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 239 字节 | 239.00 KiB/s, 完成.
总共 3(差异 0),复用 0(差异 0),包复用 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/ding-f/test-git.git
* [new branch] master -> master
分支 'master' 设置为跟踪来自 'test-git' 的远程分支 'master'
   ~/桌面/new-mater    master  vim README.md  ✔  5m 37s 

# 测试
## 只add 推送测试(只进行或不进行add是不进行commit本地库是不会被推送的!!)
~
~
   ~/桌面/new-mater    master !1  git push test-git master #没有进行add  ✔  47s 
Username for 'https://gitee.com': ding-f
Password for 'https://ding-f@gitee.com':
Everything up-to-date #此处说明远程库和本地库是一样的
   ~/桌面/new-mater    master ?1  git add  ✔  20s 
   ~/桌面/new-mater    master !1  git commit -m "我的第2个提交!!" README.md  1 ✘
[master 766ac65] 我的第2个提交!!
1 file changed, 1 insertion(+)
   ~/桌面/new-mater    master ⇡1  git push test-git master  ✔
Username for 'https://gitee.com': ding-f
Password for 'https://ding-f@gitee.com':
枚举对象中: 5, 完成.
对象计数中: 100% (5/5), 完成.
写入对象中: 100% (3/3), 294 字节 | 294.00 KiB/s, 完成.
总共 3(差异 0),复用 0(差异 0),包复用 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/ding-f/test-git.git
68447e5..766ac65 master -> master

推送批量文件实验

推送一个之前开发的安康学院校园墙源码到我的私有仓库,且之前没有用到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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 ~/Desktop/ ls      #将源码放到了schoolwall文件夹,无压缩
schoolwall schoolwall-main test-git 笔记
 ~/Desktop/ cd schoolwall
 ~/Desktop/schoolwall/ git init
Initialized empty Git repository in /Users/fuding/Desktop/schoolwall/.git/
 ~/Desktop/schoolwall/ [master] git status
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
.DS_Store
.idea/
pom.xml
schoolwall.iml
schoolwall.sql
src/

nothing added to commit but untracked files present (use "git add" to track)
 ~/Desktop/schoolwall/ [master] git add . #添加本文件夹所有内容到暂存区
 ~/Desktop/schoolwall/ [master+] git status
On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .DS_Store
new file: .idea/.gitignore
new file: .idea/.name
new file: .idea/artifacts/schoolwall.xml
#此处省略几百行···
#···
 ~/Desktop/schoolwall/ [master+] git commit -m "1.0" #由于add过就不需要再指定文件路径
[master (root-commit) 5ecd93f] 1.0
219 files changed, 23059 insertions(+)
create mode 100644 .DS_Store
create mode 100644 .idea/.gitignore
create mode 100644 .idea/.name
create mode 100644 .idea/artifacts/schoolwall.xml
#此处省略几百行···
#···
 ~/Desktop/schoolwall/ [master] git reflog
5ecd93f HEAD@{1}: commit (initial): 1.0
(END)
 ~/Desktop/schoolwall/ [master] git remote add school https://gitee.com/ding-f/schoolwall.git
 ~/Desktop/schoolwall/ [master] git push school master
Username for 'https://gitee.com': ding-f
Password for 'https://ding-f@gitee.com':
Enumerating objects: 243, done.
Counting objects: 100% (243/243), done.
Delta compression using up to 4 threads
Compressing objects: 100% (236/236), done.
Writing objects: 100% (243/243), 19.75 MiB | 1.05 MiB/s, done.
Total 243 (delta 49), reused 0 (delta 0)
remote: Resolving deltas: 100% (49/49), done.
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/ding-f/schoolwall.git
* [new branch] master -> master
 ~/Desktop/schoolwall/ [master] git status #顺便检测哪些文件未add操作
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: .DS_Store
modified: src/.DS_Store

Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md #加入README.md
"Web\351\253\230\347\272\247\347\274\226\347\250\213\350\257\276\347\250\213\350\256\276\350\256\241\346\212\245\345\221\212\346\250\241\346\235\277 (1).doc" #加入我的项目书

no changes added to commit (use "git add" and/or "git commit -a")
 ~/Desktop/schoolwall/ [master*] git add . #*代表有未add内容
 ~/Desktop/schoolwall/ [master+] #出现+说明有文件提交到暂存区但未提交本地库

 ~/Desktop/schoolwall/ [master+] git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: .DS_Store
new file: README.md
new file: "Web\351\253\230\347\272\247\347\274\226\347\250\213\350\257\276\347\250\213\350\256\276\350\256\241\346\212\245\345\221\212\346\250\241\346\235\277 (1).doc"
modified: src/.DS_Store

 ~/Desktop/schoolwall/ [master+] git commit
Aborting commit due to empty commit message.
 ~/Desktop/schoolwall/ [master+] git commit -m "1.0" #虽然一样的评论内容,但不会被git视为同一版本,它将生成第二次commit的不同版本号
[master 6f83872] 1.0
4 files changed, 2 insertions(+)
create mode 100644 README.md
create mode 100644 "Web\351\253\230\347\272\247\347\274\226\347\250\213\350\257\276\347\250\213\350\256\276\350\256\241\346\212\245\345\221\212\346\250\241\346\235\277 (1).doc"
 ~/Desktop/schoolwall/ [master] git status
On branch master
nothing to commit, working tree clean
 ~/Desktop/schoolwall/ [master] git reflog
6f83872 (HEAD -> master, school/master) HEAD@{0}: commit: 1.0 #以下为两次提交的不同的版本号
5ecd93f HEAD@{1}: commit (initial): 1.0
 ~/Desktop/schoolwall/ [master] git push school master
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 4 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 3.02 MiB | 3.67 MiB/s, done.
Total 7 (delta 2), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/ding-f/schoolwall.git
5ecd93f..6f83872 master -> master
 ~/Desktop/schoolwall/ [master] git status
On branch master
nothing to commit, working tree clean

克隆远程仓库到本地

执行克隆远程仓库代码时,如果是私有仓库就要进行用户名&密码的验证或者验证ssh密钥,不建议国内用户使用github,哪怕是有梯子也简直吐血,推荐使用gitee(码云)。

1
2
3
4
5
6
7
8
9
10
 ~/Desktop/schoolwall-main/ [master] git clone https://gitee.com/ding-f/schoolwall.git
Cloning into 'schoolwall'...
remote: Enumerating objects: 250, done.
remote: Counting objects: 100% (250/250), done.
remote: Compressing objects: 100% (194/194), done.
remote: Total 250 (delta 52), reused 242 (delta 49), pack-reused 0
Receiving objects: 100% (250/250), 22.78 MiB | 6.08 MiB/s, done.
Resolving deltas: 100% (52/52), done.
 ~/Desktop/schoolwall-main/ [master] ls
schoolwall

常用操作

推荐使用SSH公私钥验证方式对仓库的代码上传修改等操作,无需每次都要输入密码验证,github已不支持使用密码验证,直接就是SSH/PGP密钥进行对仓库的管理,推荐这篇文章:Gitee帮助中心:生成/添加SSH公钥(github/gitee均适用),不同机器只需生成一对非对称加密公私密钥,公钥放gitee/github,私钥放置在本机~/.ssh(linux)。

值得注意的是,如果使用SSH密钥方式,则一定要使用SSH的仓库链接,例如git@github.com:ding-f/hexo-code.git

建议生成的私密钥对每台设备生成一对,可以给多个可以使用Git工具的网站使用,只需要复制公钥到github/gitee网站指定设置SSH密钥的地方添加进去公钥,再进行一个验证:命令行输入ssh -T git@gitee.com,提示Hi **ding-f**! You've successfully authenticated就成功了。

参考:

Gitee帮助中心:生成/添加SSH公钥

GitHub:Generating a new SSH key and adding it to the ssh-agent

GitHub:Adding a new SSH key to your GitHub account

签名

签名

1
2
3
4
5
6
7
git config --global user.name fuding
git config --global user.email f_ding@126.com
cat ~/.gitconfig #设置之后就可以在这个配置文件里找到
[user]
name = ding-f
email = f_ding@126.com
git config --list # 显示已设置的配置

初始化:

1
git init  #首先要进入对应要提交的文件夹

新加远程仓库:

1
git remote add gitee git@gitee.com:ding-f/hexo-code.git  # 如果设置了密钥对,建议使用ssh连接

移除远程仓库:

1
git remote remove gitee

查看本地已设置的仓库别名:

1
git remote -v

提交所有本项目下所有内容到暂存区:

1
git add .   # 初始化未提交过到暂存区时使用(首次提交必须执行的命令)

提交所有代码到本地仓库:

1
git commit -m "项目整合基本完成" -a 

提交代码到指定仓库:

1
2
git push origin master   #此处直接提交的条件是一定要将ssh公私秘钥配置好,不然只能先按一遍密码才可以提交
git push -u github master #-u 建立追踪关系。通过追踪关系,我们可以知道当前本地分支和哪个远程分支对应,并可以轻松地推送和拉取更新。这个命令会将本地 main 分支推送到远程仓库的 main 分支,并将本地 main 分支和远程 origin/main 分支建立关联,以后推送时就可以直接使用 git push 命

追踪关系通常是由 Git 自动创建的,但是也可以手动设置,例如通过使用 git branch --set-upstream-to 命令来设置本地分支和远程分支的对应关系。在使用 git push -u 命令推送更新时,如果当前分支还没有建立追踪关系,则 Git 会自动帮我们将本地分支与远程分支建立追踪关系,以便我们以后可以轻松地推送和拉取更新。

IDE集成Git

Vscode

Vscode 默认自带了一个集成Git,但是如果本机命令行已经安装配置过Git,并且设置了密钥对,就会导致Vscode中的集成Git可能会出现不能提交代码的情况,最好的解决方案就是用命令行的正式Git来替换集成Git。

  1. 打开VS Code并导航到您的代码项目。

  2. 在VS Code的顶部菜单栏中,点击”文件”(File)并选择”首选项”(Preferences),然后选择”设置”(Settings)。

  3. 在设置界面中,搜索”git.path”以找到Git可执行文件的路径设置。

  4. 在搜索结果中,找到”Git: Path”选项,并点击”编辑设置.json”以编辑JSON配置文件。

    1
    2
    3
    4
    // Linux
    "git.path": "/usr/bin/git"
    // Windows
    "git.path": "C:\Program Files\Git\bin\git.exe"
  5. 在配置文件中,您可以设置Git可执行文件的路径,例如:"git.path": "/usr/bin/git"。请根据您自己的环境修改路径。在大多数Linux系统中,Git的路径通常是/usr/bin/git

  6. 保存配置文件并关闭设置界面。

  7. 在VS Code的顶部菜单栏中,点击”文件”(File)并选择”首选项”(Preferences),然后选择”设置”(Settings)。

  8. 在设置界面中,搜索”git.enabled”以找到Git集成设置。

  9. 在搜索结果中,找到”Git: Enabled”选项,并确保它被勾选上,以启用VS Code的Git集成功能。

  10. 关闭设置界面。

  11. 现在,您可以在VS Code中使用正式的命令行Git功能,包括提交、推送和拉取代码等操作。

实验

分支合并

项目中一共分了两个分支,分别是master feature,其中feature是之前改动过的,master是目前 刚更改过的。分支成两个版本的原因是master中的功能开发了一半,但要立即进行另一个功能的开发,于是分成了两个不同的版本。

完成了master中的功能,但要将两个分支的所有功能合并到一个分支,于是在master分支下创建了combine分支,切换到combine分支(和master分支的内容一致)。

切换分支

1
git checkout master

创建合并分支

1
2
3
git branch combine 
# 查看已有分支
git branch -v

切换至新分支

1
git checkout combine 

查看各个分支的提交历史

1
2
# 实现进行
git log [分支名]

合并分支

使用的是ort合并策略(在Git 2.30版本引入),它采用了一个基于树的合并模型,该模型通过比较和匹配源分支和目标分支的代码树结构来自动解决合并冲突。它可以处理代码移动、重命名和复杂结构变化等情况下的合并冲突。

以下报告没有指出合并有冲突的文件,代表合并完成。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
git merge feature 
自动合并 src/api/product.js
自动合并 src/components/kq-goods-item/index.vue
自动合并 src/components/kq-goods/index.vue
自动合并 src/pages/product/product.vue
Merge made by the 'ort' strategy.
src/api/order.js | 6 ++-
src/api/product.js | 3 +-
src/components/kq-block/index.vue | 28 +++++++++++---
src/components/kq-cart-goods/index.vue | 10 ++++-
src/components/kq-cart/index.vue | 15 +++++++-
src/components/kq-goods-item/index.vue | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
src/components/kq-goods/index.vue | 6 +++
src/components/kq-show/index.vue | 1 +
src/pages.json | 18 ++++-----
src/pages/order/order.vue | 42 ++++++++++++++++-----
src/pages/product/product.vue | 2 +-
src/store/index.js | 83 +++++++++++++++++++++++++++++++++++++++---
test_code/mktoken.js | 1 +
13 files changed, 307 insertions(+), 51 deletions(-)
  • +++ 表示在目标分支(合并到的分支)中添加的行。
  • --- 表示在源分支(被合并的分支)中删除的行。
  • +++---+- 表示有行被修改,既有行添加又有行删除。

如果有合并冲突

  1. Git会在冲突的文件中标记出冲突的部分。打开包含冲突的文件,你会看到类似下面的内容:

    1
    2
    3
    4
    5
    bashCopy code<<<<<<< HEAD
    // 代码来自当前分支(HEAD)
    =======
    // 代码来自合并的分支
    >>>>>>> branch-name
  2. 仔细检查冲突标记之间的代码,并根据需要进行修改。你需要决定保留哪些代码,或者如何将两个版本的代码合并在一起。

  3. 在冲突标记之间做出修改后,保存文件。

  4. 使用git add命令将已解决冲突的文件标记为已解决。例如,如果你正在解决名为file.txt的文件冲突,可以运行:

    1
    git add file.txt
  5. 如果合并操作涉及多个文件,重复上述步骤,解决所有的冲突。

  6. 当所有冲突都解决并且所有文件都被标记为已解决后,运行git commit命令提交合并结果:

    1
    git commit -m "Resolve merge conflicts"
  7. 如果在解决冲突期间有其他的合并冲突提示,按照相同的步骤进行解决,直到所有冲突都被解决。

故障

  • 可能使用了代理,我猜是DNS漏了啥的,报错如下:

    1
    2
    3
    4
    5
    6
    kex_exchange_identification: Connection closed by remote host
    Connection closed by 20.27.177.113 port 22
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.
  • 也可能是~/.gitconfig添加了如下:

    不清楚这个是干啥的,后面查

    1
    [safe]