Skip to content

10 Git 版本控制系统

Git 初识

  1. 概念:一个免费开源,分布式的代码版本控制系统,帮助开发团队维护代码

  2. 作用:记录代码内容,切换代码版本,多人开发时高效合并代码内容

  3. 如何学:

    • 个人本机使用:Git 基础命令和概念
    • 多人共享使用:团队开发同一个项目的代码版本管理

7c252429-6980-43e2-9e78-5a821c3df55a

bash
# 安装 Git
 scoop install main/git

# 校验安装
 git --version
git version 2.43.0.windows.1

# 配置用户名
 git config --global user.name "your name"

# 配置邮箱
 git config --global user.email "your email"

仓库

  • Git 仓库(repository):记录文件状态内容的地方,存储着修改的历史记录(.git 文件夹)

    7e59da99-b3b5-45a3-aed9-68a990853f26

  • 创建 Git 仓库:

    • 把本地文件夹转换成 Git 仓库:命令 git init
    • 从其他服务器上克隆 Git 仓库

    f517a9f8-e8d6-4f3c-bc3f-850ab4e47bc6

新建本地仓库
bash
 git init # 初始化本地仓库
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in E:/git_study/.git/

 git branch -m main # 修改默认分支名称为 main

 git status # 查看当前分支状态
On branch main # 当前在 main 分支

No commits yet

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

 tre -l 2 -a # 查看当前目录文件夹结构
.
├── .git # 仓库
   ├── config # 配置信息
   ├── description # 描述信息
   ├── HEAD # 当前状态信息
   ├── hooks # 钩子信息
   ├── info # 其他信息
   ├── objects # 对象信息
   └── refs # 引用信息
└── login
├── index.css
├── index.html
└── index.js

三个区域

  • 工作区:实际开发时操作的文件夹
  • 暂存区:保存之前的准备区域(暂存改动过的文件)
  • 版本库:提交并保存暂存区中的内容,产生一个版本快照
命令作用
git add 文件名暂存指定文件
git add .暂存所有改动的文件
git commit -m "注释说明"提交并保存,产生版本快照

bafb8a8b-a74f-4af2-ad09-6a2d55bfd538

Git 的三个区域
bash
 git add .\login\index.html

 git commit -m "1. 登录页面 html"
[main (root-commit) 98f556b] 1. 登录页面 html
1 file changed, 1 insertion( + )
create mode 100644 login/index.html

 git add .\login\index.css

 git commit -m "2. 登录页面 css"
[main fade4be] 2. 登录页面 css
1 file changed, 1 insertion( + )
create mode 100644 login/index.css

文件状态

  • 未跟踪:新文件,从未被 Git 管理过
  • 已跟踪:Git 已经知道和管理的文件(新添加,未修改,已修改)
  • 查看暂存区和工作区文件状态:git status -s
文件状态概念场景
未跟踪 U从未被 Git 管理过新文件
新添加 A第一次被 Git 暂存之前版本记录无此文件
未修改 ''三个区域统一提交保存后
已修改 M工作区内容变化修改了内容产生

259419ce-c265-4131-bfd6-175e4a7e79fd

查看文件状态
  • 需求:新增 css 文件,并使用 git status -s 查看文件状态,并最终提交
bash
 git status -s
?? login/index.js

 git add .

 git status -s
A  login/index.js

 lvim login/index.css

 git status -s
 M login/index.css
A  login/index.js

暂存区作用

  • 暂存区:暂时存储,可以临时恢复代码内容,与版本库解耦
  • 暂存区 -> 覆盖 -> 工作区,命令:git restore 目标文件(注意:完全确认覆盖时使用)
  • 从暂存区移除文件,命令:git rm --cached 目标文件

d3dcf4de-7120-4dcf-929a-f7c03172ab94

移除暂存区已暂存的文件
  • 移除暂存区已暂存的文件:git rm --cached 文件名
bash
 git add .

 git status -s
M  login/index.css

 git rm --cached .\login\index.css
rm 'login/index.css'

 git status -s
D  login/index.css
?? login/index.css

切换版本

  • 概念:把版本库某个版本对应的内容快照,恢复到工作区/暂存区

  • 查看提交历史:git log --oneline 或者 git reflog --oneline

  • 回退命令:

    • git reset --soft 版本号(其他文件未跟踪)
    • git reset --hard 版本号
    • git reset --mixed 版本号(与 git reset 等价)

注意

  • 只有记录在版本库的提交记录才能恢复
  • 回退后,继续修改 -> 暂存 -> 提交操作即可(产生新的提交记录过程)
选项效果工作区暂存区提交历史
--mixed重置 HEAD 到指定提交,保留工作区,取消提交,保留更改在暂存区保留保留部分取消之前的提交
--soft重置 HEAD 到指定提交,保留工作区,保留提交,保留更改在暂存区保留保留部分保留提交,保留更改
--hard重置 HEAD 到指定提交,丢弃工作区和暂存区的更改,取消提交丢弃丢弃取消之前的提交
查看版本库提交历史
bash
 git log --oneline
2f95f69 (HEAD -> main) update login page css
7c23d38 3. 登录页面 js
fade4be 2. 登录页面 css
98f556b 1. 登录页面 html

 git reflog --oneline
2f95f69 (HEAD -> main) HEAD@{0}: commit: update login page css
7c23d38 HEAD@{1}: commit: 3. 登录页面 js
fade4be HEAD@{2}: commit: 2. 登录页面 css
98f556b HEAD@{3}: commit (initial): 1. 登录页面 html

 git log --graph --topo-order --date=short --abbrev-commit --decorate --all --boundary --pretty=format:'%Cblue%ad %C(auto)%h%Creset -%C(auto)%d%Creset %s %Cblue[%aN]%Creset %Cblue%G?%Creset'
* 2024-01-16 2f95f69 - (HEAD -> main) update login page css [itheima] N
* 2024-01-16 7c23d38 - 3. 登录页面 js [itheima] N
* 2024-01-16 fade4be - 2. 登录页面 css [itheima] N
* 2024-01-16 98f556b - 1. 登录页面 html [itheima] N
bash
 git reflog --oneline
09b4a4e (HEAD -> main) HEAD@{0}: commit: 4. 压缩登录页面 css
52a9304 HEAD@{1}: commit: 3. 登录页面 js
b5f40cb HEAD@{2}: commit: 2. 登录页面 css
2beb7c3 HEAD@{3}: commit (initial): 1. 登录页面 html

 git reset --mixed 52a9304
Unstaged changes after reset:
M       login/index.css

 git reflog --oneline
52a9304 (HEAD -> main) HEAD@{0}: reset: moving to 52a9304
09b4a4e HEAD@{1}: commit: 4. 压缩登录页面 css
52a9304 (HEAD -> main) HEAD@{2}: commit: 3. 登录页面 js
b5f40cb HEAD@{3}: commit: 2. 登录页面 css
2beb7c3 HEAD@{4}: commit (initial): 1. 登录页面 html

 git log --oneline
52a9304 (HEAD -> main) 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

 git status # 可以看到 reset --mixed 后,login/index.css 文件的状态为 modified,但是没有被 add(未暂存),文件在工作区
On branch main
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:   login/index.css

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

 git add .
 git commit -m "5. reset --mixed 后再次压缩登录页面 css"

 git log --oneline
ff81185 (HEAD -> main) 5. reset --mixed 后再次压缩登录页面 css
52a9304 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

 git reflog --oneline
ff81185 (HEAD -> main) HEAD@{0}: commit: 5. reset --mixed 后再次压缩登录页面 css
52a9304 HEAD@{1}: reset: moving to 52a9304
09b4a4e HEAD@{2}: commit: 4. 压缩登录页面 css
52a9304 HEAD@{3}: commit: 3. 登录页面 js
b5f40cb HEAD@{4}: commit: 2. 登录页面 css
2beb7c3 HEAD@{5}: commit (initial): 1. 登录页面 html
bash
 git reflog --oneline
ff81185 (HEAD -> main) HEAD@{0}: commit: 5. reset --mixed 后再次压缩登录页面 css
52a9304 HEAD@{1}: reset: moving to 52a9304
09b4a4e HEAD@{2}: commit: 4. 压缩登录页面 css
52a9304 HEAD@{3}: commit: 3. 登录页面 js
b5f40cb HEAD@{4}: commit: 2. 登录页面 css
2beb7c3 HEAD@{5}: commit (initial): 1. 登录页面 html

 git reset --soft 52a9304

 git reflog --oneline
52a9304 (HEAD -> main) HEAD@{0}: reset: moving to 52a9304
ff81185 HEAD@{1}: commit: 5. reset --mixed 后再次压缩登录页面 css
52a9304 (HEAD -> main) HEAD@{2}: reset: moving to 52a9304
09b4a4e HEAD@{3}: commit: 4. 压缩登录页面 css
52a9304 (HEAD -> main) HEAD@{4}: commit: 3. 登录页面 js
b5f40cb HEAD@{5}: commit: 2. 登录页面 css
2beb7c3 HEAD@{6}: commit (initial): 1. 登录页面 html

 git status # 可以看到 reset --soft 后,login/index.css 文件的状态为 modified,但是文件已经 add 到暂存区了(没有出现 Changes not staged for commit),文件在暂存区
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   login/index.css

 git commit -m "6. reset --soft 后再次压缩登录页面 css"

 git log --oneline
e9ef5ba (HEAD -> main) 6. reset --soft 后再次压缩登录页面 css
52a9304 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

 git reflog --oneline
e9ef5ba (HEAD -> main) HEAD@{0}: commit: 6. reset --soft 后再次压缩登录页面 css
52a9304 HEAD@{1}: reset: moving to 52a9304
ff81185 HEAD@{2}: commit: 5. reset --mixed 后再次压缩登录页面 css
52a9304 HEAD@{3}: reset: moving to 52a9304
09b4a4e HEAD@{4}: commit: 4. 压缩登录页面 css
52a9304 HEAD@{5}: commit: 3. 登录页面 js
b5f40cb HEAD@{6}: commit: 2. 登录页面 css
2beb7c3 HEAD@{7}: commit (initial): 1. 登录页面 html
bash
 git reflog --oneline
e9ef5ba (HEAD -> main) HEAD@{0}: commit: 6. reset --soft 后再次压缩登录页面 css
52a9304 HEAD@{1}: reset: moving to 52a9304
ff81185 HEAD@{2}: commit: 5. reset --mixed 后再次压缩登录页面 css
52a9304 HEAD@{3}: reset: moving to 52a9304
09b4a4e HEAD@{4}: commit: 4. 压缩登录页面 css
52a9304 HEAD@{5}: commit: 3. 登录页面 js
b5f40cb HEAD@{6}: commit: 2. 登录页面 css
2beb7c3 HEAD@{7}: commit (initial): 1. 登录页面 html

 git reset --hard 52a9304
HEAD is now at 52a9304 3. 登录页面 js

 git reflog --oneline
52a9304 (HEAD -> main) HEAD@{0}: reset: moving to 52a9304
e9ef5ba HEAD@{1}: commit: 6. reset --soft 后再次压缩登录页面 css
52a9304 (HEAD -> main) HEAD@{2}: reset: moving to 52a9304
ff81185 HEAD@{3}: commit: 5. reset --mixed 后再次压缩登录页面 css
52a9304 (HEAD -> main) HEAD@{4}: reset: moving to 52a9304
09b4a4e HEAD@{5}: commit: 4. 压缩登录页面 css
52a9304 (HEAD -> main) HEAD@{6}: commit: 3. 登录页面 js
b5f40cb HEAD@{7}: commit: 2. 登录页面 css
2beb7c3 HEAD@{8}: commit (initial): 1. 登录页面 html

 git status # 可以看到工作区和暂存区都是干净的
On branch main
nothing to commit, working tree clean

 git log --oneline
52a9304 (HEAD -> main) 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

 git reset --hard e9ef5ba
HEAD is now at e9ef5ba 6. reset --soft 后再次压缩登录页面 css

 git reflog --oneline
e9ef5ba (HEAD -> main) HEAD@{0}: reset: moving to e9ef5ba
52a9304 HEAD@{1}: reset: moving to 52a9304
e9ef5ba (HEAD -> main) HEAD@{2}: commit: 6. reset --soft 后再次压缩登录页面 css
52a9304 HEAD@{3}: reset: moving to 52a9304
ff81185 HEAD@{4}: commit: 5. reset --mixed 后再次压缩登录页面 css
52a9304 HEAD@{5}: reset: moving to 52a9304
09b4a4e HEAD@{6}: commit: 4. 压缩登录页面 css
52a9304 HEAD@{7}: commit: 3. 登录页面 js
b5f40cb HEAD@{8}: commit: 2. 登录页面 css
2beb7c3 HEAD@{9}: commit (initial): 1. 登录页面 html

 git log --oneline
e9ef5ba (HEAD -> main) 6. reset --soft 后再次压缩登录页面 css
52a9304 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

删除文件

  • 需求:删除 login/index.html 文件,并产生一次版本记录

  • 步骤:

    1. 手动删除工作区文件
    2. 暂存变更/手动删除暂存区文件造成变更
    3. 提交保存
  • 总结:工作区只要改变,都可以暂存提交产生新记录

删除文件
bash
 git log --oneline
ba46fd6 (HEAD -> main) 7. reset --hard 后再次压缩登录页面 css
52a9304 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

 rm login/index.css # 删除工作区文件

 git status # 可以看到工作区和暂存区的状态:工作区有变化,暂存区没有
On branch main
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    login/index.css

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

 git ls-files
login/index.css
login/index.html
login/index.js

 git rm --cached .\login\index.css # 删除暂存区文件
rm 'login/index.css'

 git ls-files
login/index.html
login/index.js

 git status # 可以看到工作区和暂存区的状态:暂存区有变化,等待提交
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    login/index.css

 git commit -m "8. 删除 login/index.css"
[main 1acd640] 8. 删除 login/index.css
 1 file changed, 1 deletion(-)
 delete mode 100644 login/index.css

 git log --oneline
1acd640 (HEAD -> main) 8. 删除 login/index.css
ba46fd6 7. reset --hard 后再次压缩登录页面 css
52a9304 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

忽略文件

  • 概念:.gitignore 文件可以让 Git 彻底忽略跟踪指定文件件

  • 目的:让 Git 仓库更小更快,避免重复无意义的文件管理

  • 例如:

    • 系统或软件自动生成的文件
    • 编译产生的结果文件
    • 运行时生成的日志文件,缓存文件,临时文件等
    • 涉密文件,密码,秘钥等文件
  • 创建:

    1. 项目根目录新建 .gitignore 文件
    2. 填入相应配置来忽略指定文件

注意

如果文件已经被暂存区跟踪过,可以从暂存区移除即可

忽略文件
bash
 echo "login/index.html" > .gitignore

 git status -s
?? .gitignore

 git add .gitignore # 添加忽略文件

 git commit -m "9. add .gitignore" # 提交忽略文件
[main ef83e50] 9. add .gitignore
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

 git status -s # 查看状态:可以看到工作区和暂存区干净

 rm login/index.html # 删除工作区 login/index.html 文件

 git rm --cached .\login\index.html # 删除暂存区 login/index.html 文件
rm 'login/index.html'

 git commit -m "10. del login/index.html" # 提交删除操作
[main 3c1ccb7] 10. del login/index.html
 1 file changed, 1 deletion(-)
 delete mode 100644 login/index.html

 echo "<h1>新的 index.html 文件</h1>" > login/index.html # 创建新的 login/index.html 文件

 git status -s # 查看状态(输出为空): 可以看到工作区 login/index.html 文件被忽略

分支的概念

  • 概念:本质上是指向提交节点的可变指针,默认名字是 master

  • 注意:HEAD 指针影响工作区/暂存区的代码状态

  • 修改当前分支名: git branch -m 新名字

  • 场景:开发新需求 / 修复 Bug,保证主线代码随时可用,多人协同开发提高效率

  • 例如:在现有代码上创建新分支完成内容列表业务,突然需要紧急修复 Bug (单独创建分支解决 Bug)

    31133aab-38fd-4a59-89b8-807c7cbba54c

  • 创建分支:git branch 分支名

  • 切换分支:git checkout 分支名 或者 git switch 分支名

  • 创建并切换到分支:git checkout -b 分支名

  • 查看本地分支:git branch

分支的概念
bash
 git log --oneline -a
3c1ccb7 (HEAD -> main) 10. del login/index.html
ef83e50 9. add .gitignore
1acd640 8. 删除 login/index.css
ba46fd6 7. reset --hard 后再次压缩登录页面 css
52a9304 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

 git checkout -b content
Switched to a new branch 'content'

 mkdir content
 touch content/index.html content/index.css content/index.js

 git status -s
?? content/

 git add content/index.html
 git commit -m "11. Add content/index.html"

 git add content/index.css
 git commit -m "12. Add content/index.css"

 git add content/index.js
 git commit -m "13. Add content/index.js"

 git log --oneline -a
7715b3d (HEAD -> content) 13. Add content/index.js
0a8ced8 12. Add content/index.css
45636c2 11. Add content/index.html
3c1ccb7 (main) 10. del login/index.html
ef83e50 9. add .gitignore
1acd640 8. 删除 login/index.css
ba46fd6 7. reset --hard 后再次压缩登录页面 css
52a9304 3. 登录页面 js
b5f40cb 2. 登录页面 css
2beb7c3 1. 登录页面 html

git_study [ content]
 git branch # 查看本地分支
* content
  main

git_study [ content]
 git branch -v # 查看本地分支的最后一次提交
* content 7715b3d 13. Add content/index.js
  main    3c1ccb7 10. del login/index.html

分支合并与删除

  • 需求:把 content 合并回到 main 分支并删除 content 分支

  • 步骤:

    1. 切回到要合入的分支上:git checkout main
    2. 合并其他分支过来:git merge content
    3. 删除合并后的分支指针:git branch -d content
分支合并与删除
bash
git_study [ content]
 git branch # 查看分支
* content
  main

git_study [ content]
 git checkout main # 切换到 main 分支
Switched to branch 'main'

git_study [ main]
 git merge content  # 合并 content 分支
Updating 3c1ccb7..7715b3d
Fast-forward
 content/index.css  | 0
 content/index.html | 0
 content/index.js   | 0
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 content/index.css
 create mode 100644 content/index.html
 create mode 100644 content/index.js

git_study [ main]
 git log --oneline -a # 查看提交记录
7715b3d (HEAD -> main, content) 13. Add content/index.js
0a8ced8 12. Add content/index.css
45636c2 11. Add content/index.html
3c1ccb7 10. del login/index.html
ef83e50 9. add .gitignore
1acd640 8. 删除 login/index.css
ba46fd6 7. reset --hard 后后再再次次压压缩缩登登录录页页面面 css
52a9304 3. 登登录录页页面面 js
b5f40cb 2. 登登录录页页面面 css
2beb7c3 1. 登登录录页页面面 html

git_study [ main]
 git branch -d content # 删除 content 分支
Deleted branch content (was 7715b3d).

git_study [ main]
 git branch -a # 查看分支
* main

分支合并提交

  • 合并提交:发生于原分支产生了新的提交记录后,再合并回去时发生,自动使用多个快照记录合并后产生一次新的提交

  • 步骤:

    1. 切回到要合入的分支上:git checkout main
    2. 合并其他分支过来:git merge publish
    3. 删除合并后的分支:git branch -d publish
分支合并提交
bash
git_study [ main]
 git ll
* 2024-01-02 72bc0f6 - (publish) 11.3. Add publish/index.js [itheima] N
* 2024-01-02 cf34567 - 11.2. Add publish/index.css [itheima] N
* 2024-01-02 6f95284 - 11.1. Add publish/index.html [itheima] N
| * 2024-01-02 7715b3d - (HEAD -> main) 13. Add content/index.js [itheima] N
| * 2024-01-02 0a8ced8 - 12. Add content/index.css [itheima] N
| * 2024-01-02 45636c2 - 11. Add content/index.html [itheima] N
|/
* 2024-01-02 3c1ccb7 - 10. del login/index.html [itheima] N
* 2024-01-02 ef83e50 - 9. add .gitignore [itheima] N
...

git_study [ main]
 git merge publish
Merge made by the 'ort' strategy.
 publish/index.css  | 1 +
 publish/index.html | 1 +
 publish/index.js   | 1 +
 3 files changed, 3 insertions(+)
 create mode 100644 publish/index.css
 create mode 100644 publish/index.html
 create mode 100644 publish/index.js

git_study [ main]
 git log --oneline -a
33fd243 (HEAD -> main) Merge branch 'publish'
72bc0f6 (publish) 11.3. Add publish/index.js
cf34567 11.2. Add publish/index.css
6f95284 11.1. Add publish/index.html
7715b3d 13. Add content/index.js
0a8ced8 12. Add content/index.css
45636c2 11. Add content/index.html
3c1ccb7 10. del login/index.html
ef83e50 9. add .gitignore
...

分支合并冲突

  • 概念:不同分支中,对同一个文件的同一部分修改,Git 无法干净的合并,产生合并冲突

  • 步骤:

    1. 基于 main 分支新建 publish-fix 分支
    2. publish-fix 分支上修改 publish/index.html 文件
    3. 切换回 main 分支,修改 publish/index.html 文件
    4. 合并 publish-fix 分支到 main 分支:git merge publish-fix
    5. 解决冲突,重新提交
分支合并冲突
bash
git_study [ main]
 git ll
* 2024-01-02 949afdd - (HEAD -> main) Update wangeditor5 stylesheet with integrity and crossorigin attributes [itheima] N
* 2024-01-02 9a75dcc - Update publish/index.html with Bootstrap 5.3.2 and fix formatting [itheima] N
| * 2024-01-02 7d74148 - (publish-fix) 14. Update publish/index.html with Bootstrap and WangEditor styles [itheima] N
|/
*   2024-01-02 33fd243 - Merge branch 'publish' [itheima] N
|\
| * 2024-01-02 72bc0f6 - 11.3. Add publish/index.js [itheima] N
| * 2024-01-02 cf34567 - 11.2. Add publish/index.css [itheima] N
| * 2024-01-02 6f95284 - 11.1. Add publish/index.html [itheima] N
* | 2024-01-02 7715b3d - 13. Add content/index.js [itheima] N
* | 2024-01-02 0a8ced8 - 12. Add content/index.css [itheima] N
* | 2024-01-02 45636c2 - 11. Add content/index.html [itheima] N
|/
* 2024-01-02 3c1ccb7 - 10. del login/index.html [itheima] N
* 2024-01-02 ef83e50 - 9. add .gitignore [itheima] N
...

git_study [ main]
 git merge publish-fix # 合并分支:发现冲突
Auto-merging publish/index.html
CONFLICT (content): Merge conflict in publish/index.html
Automatic merge failed; fix conflicts and then commit the result.

> lvim publish/index.html # 解决冲突

> git commit -m "Merge branch 'publish-fix' into 'main'"

 git ll
*   2024-01-02 d0d552d - (HEAD -> main) Merge branch 'publish-fix' into 'main' [itheima] N
|\
| * 2024-01-02 7d74148 - (publish-fix) 14. Update publish/index.html with Bootstrap and WangEditor styles [itheima] N
* | 2024-01-02 949afdd - Update wangeditor5 stylesheet with integrity and crossorigin attributes [itheima] N
* | 2024-01-02 9a75dcc - Update publish/index.html with Bootstrap 5.3.2 and fix formatting [itheima] N
|/
*   2024-01-02 33fd243 - Merge branch 'publish' [itheima] N
|\
| * 2024-01-02 72bc0f6 - 11.3. Add publish/index.js [itheima] N
| * 2024-01-02 cf34567 - 11.2. Add publish/index.css [itheima] N
| * 2024-01-02 6f95284 - 11.1. Add publish/index.html [itheima] N
* | 2024-01-02 7715b3d - 13. Add content/index.js [itheima] N
* | 2024-01-02 0a8ced8 - 12. Add content/index.css [itheima] N
* | 2024-01-02 45636c2 - 11. Add content/index.html [itheima] N
|/
* 2024-01-02 3c1ccb7 - 10. del login/index.html [itheima] N
* 2024-01-02 ef83e50 - 9. add .gitignore [itheima] N
...

远程仓库

  • 概念:托管在因特网或其他网络中的你的项目的版本库

  • 作用:保存版本库的历史记录,多人协作

  • 创建:公司自己服务器 / 第三方托管平台(Gitee,GitLab,GitHub…)

  • 需求:创建远程版本库,并把本地 Git 仓库推送上去保存

  • 步骤:

    1. 注册第三方托管平台网站账号

    2. 新建仓库得到远程仓库 Git 地址

    3. 本地 Git 仓库关联远程仓库地址

      bash
      git remote add 远程仓库别名 远程仓库地址
      # git remote add origin https://gitee.com/lidongxu/work.git
    4. 本地 Git 仓库推送版本记录到远程仓库

      bash
      git push -u 远程仓库别名 本地和远程分支名
      # git push -u origin main
      # 完整写法:git push --set-upstream origin main:main

克隆远程仓库

  • 克隆:拷贝一个 Git 仓库到本地,进行使用

    bash
    git clone 远程仓库地址
    # git clone https://gitee.com/lidongxu/work.git
  • 效果:在运行命令所在文件夹,生成 work 项目文件夹(包含版本库,并映射到暂存区和工作区)

注意

  • Git 本地仓库已经建立好和远程仓库的链接
  • 仓库公开随意克隆,推送需要身为仓库团队成员

拉取代码

  • 概念:从远程仓库拉取最新代码到本地仓库

  • 场景:多人协作开发,保证本地仓库代码是最新的

  • 想要看到别人同步上去的最新内容:

    bash
    git pull origin main
    
    # 等价与
    git fetch origin main # 拉取最新代码到本地仓库
    git merge origin/main # 合并到当前分支

Git 常用命令

命令作用注意
git -v查看 Git 版本
git init初始化 Git 仓库
git add 文件标识暂存某个文件文件标识以终端为起始的相对路径
git add .暂存所有文件
git commit -m '说明注释'提交产生版本记录每次提交,把暂存区内容快照一份
git status查看文件状态 - 详细信息
git status -s查看文件状态 - 简略信息第一列是暂存区状态,第二列是工作区状态
git ls-files查看暂存区文件列表
git restore 文件标识从暂存区恢复到工作区如果文件标识为 . 则恢复所有文件
git rm --cached 文件标识从暂存区移除文件不让 Git 跟踪文件变化
git log查看提交记录 - 详细信息
git log --oneline查看提交记录 - 简略信息版本号 分支指针 提交时说明注释
git reflog --oneline查看完整历史 - 简略消息包括提交,切换,回退等所有记录
git reset <commit-id>切换版本代码到暂存区和工作区--soft 模式保留暂存区和工作区原本内容
--hard 模式不保留暂存区和工作区原本内容
--mixed 模式不保留暂存区,工作区保留(默认)先覆盖到暂存区,再用暂存区对比覆盖工作区
git branch 分支名创建分支
git branch查看本地分支
git branch -d 分支名删除分支请确保记录已经合并到别的分支下,再删除分支
git checkout 分支名切换分支
git checkout -b 分支名创建并立刻切换分支
git merge 分支名把分支提交历史记录合并到当前所在分支
git remote add 远程仓库别名 远程仓库地址添加远程仓库地址别名唯一,地址是 .git 结尾的网址
git remote -v查看远程仓库地址
git remote remove 远程仓库别名删除远程仓库地址
git pull 远程仓库别名 分支名 拉取git pull 远程仓库别名 远程分支名:本地分支名
等价于:git fetch 和 git merge
git push 远程仓库别名 分支名推送git push 远程仓库别名 本地分支名:远程分支名 -u
建立通道后以后可以简写 git push
git pull --rebase 远程仓库别名 分支名拉取合并合并没有关系的记录
git clone 远程仓库地址克隆从 0 得到一个远程的 Git 仓库到本地使用

面试题

Git 和其他版本控制系统有什么区别

  • Git 和其他版本控制系统(如 SVN 和 CVS)之间的主要区别在于它们的架构和工作方式。Git 是一个分布式版本控制系统,每个开发人员都可以拥有一个完整的代码库副本,并在需要时进行合并更改。
  • 而 SVN 和 CVS 等传统的版本控制系统是基于中央服务器的,每个开发人员都从中央服务器检出代码,然后提交更改

Git 的三个区域是什么?如何将代码从一个区域移动到另一个区域

  • Git 的三个区域是工作区、暂存区和本地仓库。可以通过 git add 将代码从工作区移动到暂存区,通过 git commit 将代码从暂存区移动到本地仓库

如何将本地的代码推送到远程仓库

  • 可以通过 git push 将本地的代码推送到远程仓库。
  • 例如,可以使用 git push origin main 将本地的 main 分支推送到名为 origin 的远程仓库。

如何从远程仓库拉取代码到本地

  • 可以通过 git pull 从远程仓库拉取代码到本地。
  • 例如,可以使用 git pull origin main 从名为 origin 的远程仓库的 main 分支拉取最新代码。

如何在 Git 中合并两个分支

  • 使用 git merge 命令合并两个分支。
  • 例如,要将名为 feature-branch 的分支合并到当前分支 git merge feature-branch

如何在 Git 中解决冲突

  • 使用 git merge 命令合并分支时可能会发生冲突。
  • 要解决冲突,请手动编辑包含冲突的文件,然后使用 git addgit commit 命令提交更改。

什么是 Git flow

  • Git flow 是一种 Git 分支模型,它为团队提供了一种规范化的开发流程,使得代码库更容易管理、维护和协作。

  • 它的核心思想是在代码库中维护两个主要的分支:

    • 一个稳定的主分支 main,用于发布生产版本;
    • 另一个是开发分支 develop,用于开发新功能和修复错误。
  • 此外,Git flow 还定义了一些支持分支,如 featurebugfixreleasehotfix 等,它们有助于更好地管理和协作团队成员在不同的开发阶段中的工作。

  • 总之,Git flow 提供了一种标准化的 Git 分支模型,使团队能够更有效地协作和管理 Git 代码库。