u
This commit is contained in:
1 parent
07e0001b87
commit
46cbb2f50d
543 files changed
+32576
-4231
No files matched your search
@@ -0,0 +1,339 @@
|
||||
# Git(版本控制系统)
|
||||
|
||||
Git:https://git-scm.com/
|
||||
|
||||
Git 是一个免费、开源的分布式版本控制系统,旨在以高效、快速的方式处理从小型到超大型的各种项目。
|
||||
|
||||
Git 的速度非常快,并且拥有庞大的生态系统,包括各种图形界面(GUI)、代码托管服务以及命令行工具。
|
||||
|
||||
## 安装
|
||||
### windows
|
||||
https://git-scm.com/
|
||||
|
||||
### linux
|
||||
```bash
|
||||
sudo apt install git-all
|
||||
```
|
||||
> https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git
|
||||
|
||||
都可以使用 `git -v` 检验是否安装成功
|
||||
|
||||
|
||||
## 配置
|
||||
查看当前配置
|
||||
```bash
|
||||
git config --list
|
||||
```
|
||||
|
||||
设置邮箱和用户名
|
||||
```bash
|
||||
git config --global user.name "你的用户名"
|
||||
git config --global user.email "你的邮箱"
|
||||
```
|
||||
|
||||
设置代理
|
||||
```bash
|
||||
git config --global http.proxy http://127.0.0.1:7890
|
||||
git config --global https.proxy http://127.0.0.1:7890
|
||||
```
|
||||
|
||||
取消代理
|
||||
```bash
|
||||
git config --global --unset http.proxy
|
||||
git config --global --unset https.proxy
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 概念
|
||||

|
||||
|
||||
Git 管理代码时,一般会经过这四个地方:
|
||||
- 工作区:放置代码的 目录/文件夹
|
||||
- 暂存区:就是相当于一个临时地方,将代码先放到这里
|
||||
- 本地仓库:电脑的正是仓库,就是相当于将代码正式放到了自己电脑的仓库中,保存代码的历史记录
|
||||
- 远程仓库:网络/云 上的仓库,方便和别人进行协作
|
||||
|
||||
可根据上面的图进行理解
|
||||
|
||||
## 基础操作
|
||||
```bash
|
||||
// 一般只有初始化仓库的时候才会用到
|
||||
git init
|
||||
|
||||
// 1.克隆远程仓库的代码
|
||||
git clone [url]
|
||||
|
||||
// 2.提交自己修改过的代码
|
||||
git add .
|
||||
git commit -m "注释"
|
||||
|
||||
// 3.拉取远程仓库的最新代码
|
||||
git pull
|
||||
|
||||
// 4.将自己的修改推送到远程仓库
|
||||
git push origin master
|
||||
```
|
||||
|
||||
## 命令速查表
|
||||

|
||||
|
||||
|
||||
|
||||
## 子模块
|
||||
|
||||
主仓库就像一个指向子模块的“快捷方式”,它记录了子模块的位置和版本。子模块是一个独立的仓库,存自己的代码和历史,主仓库只知道子模块的位置,不会存实际代码内容。
|
||||
|
||||
简单来说,它们是两个独立的仓库。推送代码时,应该先推送子模块的代码,再推送主仓库的代码,避免跟踪警告。如果你推送了子模块的修改,无论主仓库的代码有没有变化,都需要重新推送一次主仓库,以确保引用更新。
|
||||
|
||||
**添加子模块**
|
||||
|
||||
```bash
|
||||
git submodule add <url> <path>
|
||||
```
|
||||
|
||||
例如:
|
||||
|
||||
```bash
|
||||
git submodule add https://github.com/oneao/docusaurus code/docusaurus
|
||||
```
|
||||
|
||||
添加成功后会在主仓库根目录下生成 `.gitmodules` 文件,该文件内有子模块的信息
|
||||
|
||||
**初始化子模块并获取内容**
|
||||
|
||||
只使用
|
||||
|
||||
```bash
|
||||
git submodule update --init --recursive
|
||||
```
|
||||
|
||||
需要想修改代码并提交,需要切换到指定的分支
|
||||
```bash
|
||||
git branch
|
||||
|
||||
git checkout master
|
||||
```
|
||||
|
||||
**拉取子模块代码**
|
||||
|
||||
先拉取主仓库代码
|
||||
|
||||
```bash
|
||||
git pull
|
||||
```
|
||||
|
||||
再在主仓库位置更新子模块引用
|
||||
|
||||
```bash
|
||||
git submodule update --init --recursive
|
||||
```
|
||||
|
||||
然后进入子模块位置
|
||||
```bash
|
||||
git pull
|
||||
```
|
||||
|
||||
**从主仓库中移除子模块**
|
||||
|
||||
```bash
|
||||
git rm --cached code/docusaurus
|
||||
|
||||
// 删除子模块文件夹(如果不需要),提交更改。
|
||||
```
|
||||
|
||||
## 常见问题
|
||||
|
||||
### push之前未pull
|
||||
|
||||
报错信息:
|
||||
|
||||
```bash
|
||||
D:\code\test\test1>git push origin master
|
||||
To http://192.168.1.189:8443/r/test.git
|
||||
! [rejected] master -> master (fetch first)
|
||||
error: failed to push some refs to 'http://192.168.1.189:8443/r/test.git'
|
||||
hint: Updates were rejected because the remote contains work that you do not
|
||||
hint: have locally. This is usually caused by another repository pushing to
|
||||
hint: the same ref. If you want to integrate the remote changes, use
|
||||
hint: 'git pull' before pushing again.
|
||||
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
|
||||
```
|
||||
|
||||
解决方法:
|
||||
|
||||
```bash
|
||||
# 先拉取
|
||||
git pull
|
||||
# 后推送
|
||||
git push origin master
|
||||
```
|
||||
|
||||
**注意:** 不要强推,可能覆盖他人修改,一般用不到。
|
||||
|
||||
```bash
|
||||
git push -f origin master
|
||||
```
|
||||
|
||||
### 合并冲突(修改同一文件)
|
||||
当 A 和 B 同时修改了同一文件的时候,本地文件和远程文件发生了冲突。Git 无法自动合并,需要你手动解决冲突。
|
||||
|
||||
例如:
|
||||
|
||||
```bash
|
||||
CONFLICT (content): Merge conflict in a.txt
|
||||
Automatic merge failed; fix conflicts and then commit the result.
|
||||
```
|
||||
|
||||
意思是:
|
||||
|
||||
- Git 在合并文件 `a.txt` 时,发现本地和远程对同一部分代码进行了修改;
|
||||
- 无法判断以哪一方为准,自动合并失败;
|
||||
- 需要你手动处理这个冲突。
|
||||
|
||||
解决方案:
|
||||
|
||||
1. 打开冲突的 `a.txt` 文件,查看里面的内容:
|
||||
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
12 # 这里是 A 修改的
|
||||
=======
|
||||
1 # 这里是 B 修改的
|
||||
2
|
||||
>>>>>>> 6fe1e2d97eae79e0c5080ddea4bdef658841486d
|
||||
```
|
||||
|
||||
2. 手动修改内容,保留需要的部分。例如:
|
||||
|
||||
```
|
||||
12
|
||||
```
|
||||
|
||||
3. 添加修改后的文件:
|
||||
|
||||
```bash
|
||||
git add a.txt
|
||||
|
||||
git commit -m "解决 a.txt 冲突"
|
||||
|
||||
git push origin master
|
||||
```
|
||||
|
||||
### 修改文件但 git status 显示未修改
|
||||
|
||||
可能的原因:
|
||||
|
||||
- 你改的是换行符(LF/CRLF)但内容没变;
|
||||
- 编辑器保存规则设置导致未检测到真实修改;
|
||||
- 文件编码问题或隐藏字符。
|
||||
|
||||
解决方法:
|
||||
|
||||
- 用 `git diff` 检查是否真的修改了;
|
||||
- 也可以尝试先改点内容,再恢复,重新保存。
|
||||
|
||||
### 误删文件
|
||||
#### 文件误删并已 add
|
||||
误删了一个文件(比如 `b.txt`),然后执行了:
|
||||
|
||||
```bash
|
||||
git add a.txt
|
||||
```
|
||||
|
||||
- 这时候 Git **把删除操作加入暂存区**,等待 commit。
|
||||
- 文件还没有被 commit,但已经从工作区和暂存区记录为“删除”。
|
||||
|
||||
|
||||
|
||||
**查看哪些文件被误删**
|
||||
|
||||
```bash
|
||||
git status
|
||||
```
|
||||
|
||||
示例输出
|
||||
|
||||
```bash
|
||||
Changes to be committed:
|
||||
(use "git restore --staged <file>..." to unstage)
|
||||
new file: a.txt
|
||||
|
||||
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: a.txt
|
||||
```
|
||||
|
||||
|
||||
|
||||
**恢复文件**
|
||||
|
||||
这会把文件从暂存区移除,但 **不会恢复工作区文件**。
|
||||
|
||||
```bash
|
||||
git restore --staged a.txt
|
||||
```
|
||||
|
||||
这会把文件从最后一次 commit 的版本恢复到工作区。
|
||||
|
||||
```bash
|
||||
git restore a.txt
|
||||
```
|
||||
|
||||
或者可以循环恢复多个误删文件:
|
||||
|
||||
```
|
||||
git restore --staged .
|
||||
git restore .
|
||||
```
|
||||
|
||||
> `git restore .` 会把所有误删且未 commit 的文件恢复。
|
||||
|
||||
#### 文件已 commit,误删未 add
|
||||
|
||||
场景说明
|
||||
|
||||
- 文件 **已经被 Git commit**(也就是之前已经被保存到版本历史中)
|
||||
- 你在工作区 **误删了该文件**
|
||||
- **还没有执行 `git add`**
|
||||
|
||||
> 这时候 Git 知道文件原本存在,但工作区已经缺失。
|
||||
|
||||
|
||||
|
||||
**查看被误删的文件**
|
||||
|
||||
```bash
|
||||
git status
|
||||
```
|
||||
|
||||
示例输出:
|
||||
|
||||
```bash
|
||||
Changes not staged for commit:
|
||||
(use "git add/rm <file>..." to update what will be committed)
|
||||
deleted: a.txt
|
||||
```
|
||||
|
||||
> 🔹 `deleted: a.txt` 就是被误删的文件。
|
||||
|
||||
**恢复误删文件(未 add)**
|
||||
|
||||
```bash
|
||||
git restore a.txt
|
||||
```
|
||||
|
||||
> 这会从 **最近一次 commit** 恢复文件到工作区。
|
||||
|
||||
|
||||
|
||||
**检查文件是否恢复成功**
|
||||
|
||||
```
|
||||
git status
|
||||
```
|
||||
|
||||
- 文件会出现在工作区
|
||||
- `git status` 不再显示 `deleted`
|
||||
|
||||
Reference in new issue
Block a user