Files
workspace/notes/git/基础使用.md
T
2025-08-06 15:04:01 +08:00

7.6 KiB
Raw Blame History

1 基础流程

1.1 克隆仓库:git clone

git clone <远程仓库地址>

1.2 添加修改:git add

将修改的文件添加到 暂存区,准备提交:

git add <文件名>        # 添加指定文件
git add .              # 添加当前目录下所有修改文件

1.3 提交修改:git commit

将暂存区内容提交到本地仓库:

git commit -m "提交说明"

示例:

git commit -m "修复首页按钮样式"

1.4 拉取远程更新:git pull

获取远程仓库最新内容并合并到本地:

git pull origin <分支名>

示例:

git pull origin master

💡 在多人协作时,最好先 git pull 再修改,减少冲突。

1.5 推送到远程仓库:git push

将本地提交上传到远程仓库:

git push origin <分支名>

示例:

git push origin master

✅ origin 是远程仓库默认名称,master 是分支名。

1.6 简单流程总结

# 第一次获取仓库
git clone <repo>

# 修改代码
git add .
git commit -m "说明"

# 上传修改
git push origin master

# 更新最新远程内容
git pull origin master

1.7 创建仓库并推送到远程

# 初始化git仓库
git init

git add README.md

git commit -m "first commit"

# 将当前本地 Git 仓库关联到一个远程仓库地址,并命名为 origin
git remote add origin http://admin@192.168.1.189:8443/r/test.git

git push -u origin master

2 git常用命令流程图

img

3 git命令速查表

在这里插入图片描述

4 常见问题

4.1 push之前未pull

报错信息:

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.

解决方法:

# 先拉取
git pull
# 后推送
git push origin master

注意: 不要强推,可能覆盖他人修改,一般用不到。

git push -f origin master

4.2 合并冲突(修改同一文件)

当 A 和 B 同时修改了同一文件的时候,本地文件和远程文件发生了冲突。Git 无法自动合并,需要你手动解决冲突。

例如:

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
  1. 手动修改内容,保留需要的部分。例如:
12
  1. 添加修改后的文件:
git add a.txt

git commit -m "解决 a.txt 冲突"

git push origin master

4.3 fatal: not a git repository

报错:

fatal: not a git repository (or any of the parent directories): .git

原因:你当前所在的目录不是一个 Git 仓库,也没有执行 git init 或 git clone。

解决方法:

# 初始化仓库
git init
# 或者你在项目目录外部,cd 回正确的目录
cd your-project-folder

4.4 修改文件但 git status 显示未修改

可能的原因:

  • 你改的是换行符(LF/CRLF)但内容没变;
  • 编辑器保存规则设置导致未检测到真实修改;
  • 文件编码问题或隐藏字符。

解决方法:

  • 用 git diff 检查是否真的修改了;
  • 也可以尝试先改点内容,再恢复,重新保存。

4.5 误删文件

4.5.1 文件误删并已 add

误删了一个文件(比如 b.txt),然后执行了:

git add a.txt
  • 这时候 Git 把删除操作加入暂存区,等待 commit。
  • 文件还没有被 commit,但已经从工作区和暂存区记录为“删除”。

查看哪些文件被误删

git status

示例输出

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

恢复文件

这会把文件从暂存区移除,但 不会恢复工作区文件。

git restore --staged a.txt

这会把文件从最后一次 commit 的版本恢复到工作区。

git restore a.txt

或者可以循环恢复多个误删文件:

git restore --staged .
git restore .

🔹 git restore . 会把所有误删且未 commit 的文件恢复。

4.5.2 文件已 commit,误删未 add

场景说明

  • 文件 已经被 Git commit(也就是之前已经被保存到版本历史中)
  • 你在工作区 误删了该文件
  • 还没有执行 git add

这时候 Git 知道文件原本存在,但工作区已经缺失。

查看被误删的文件

git status

示例输出:

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
    deleted:    a.txt

🔹 deleted: a.txt 就是被误删的文件。

恢复误删文件(未 add)

git restore a.txt

这会从 最近一次 commit 恢复文件到工作区。

检查文件是否恢复成功


git status
  • 文件会出现在工作区
  • git status 不再显示 deleted

分支操作

本地创建分支

git branch test

查看分支

命令:

git branch # 查看本地分支
git branch -r	# 查看远程分支
git branch -a	# 查看本地 + 远程分支

示例输入:

  master
* test
=================================================
origin/HEAD -> origin/master
origin/master
origin/test
=================================================
  master
* test
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/test

切换分支

命令:

git checkout test

示例输入:

Switched to branch 'test'

推送分支到远程

假设本地分支名为 test,远程仓库名为 origin:

git push -u origin test

解释:

  • origin → 远程仓库
  • feature/login → 分支名
  • -u → 设置 上游分支(tracking branch),即本地分支会自动跟踪远程分支
  • 之后可以直接 git pull 或 git push,Git 会知道对应的远程分支

拉取分支

命令:

git checkout -b test origin/test 

这将:

  1. 从远程获取 test 分支;
  2. 在本地创建一个同名分支 test 并切换过去;
  3. 本地分支会追踪远程的 origin/test 分支。

合并分支

# 切换到 master 分支
git checkout master

# 更新 master 分支(可选,确保是最新的)
git pull origin master

# 合并 test 分支到 master
git merge test

# 如果有冲突,解决冲突后:
git add .
git commit

# 推送到远程仓库
git push origin master