Git 配置命令
bash
git config --global user.name "xxx"
git config --global user.email xxx@gmail.com
# 查看配置信息
git config --list
# 生成 SSH 密钥
ssh-keygen -t rsa -b 4096 -C "xxx@gmail.com"
Git 工作流程 && 常用命令
bash
# 克隆远程仓库
git clone xxxx
# 查看工作区状态
git status
# 添加到缓存区
git add <修改的文件>
# 提交修改
git commit
# 推送
git push
- 创建新分支
bash
git checkout -b new-feature
- 暂存
bash
git add .
- 提交修改
bash
git commit -m "提交信息"
- 拉取修改
bash
git pull origin main
- 推送更改
bash
git push origin XXX
- 合并修改
bash
git check main
git pull origin main
git merge new-feature
- 删除分支
bash
git branch -d new-feature
或者从远程仓库删除分支:
bash
git push origin --delete new-feature
- 查看提交历史
bash
git log
- 查看工作区和暂存区的差异
bash
git diff
Git 创建仓库
使用当前目录作为git仓库,我们要进行初始化
bash
git init
执行命令后在当前目录生成一个.git目录
Git 分支管理
- 创建分支
创建新分支并切换到该分支:
bash
git checkout -b <branchname>
- 查看分支
查看所有分支:
bash
git branch
查看远程分支:
bash
git branch -r
查看所有本地和远程分支:
bash
git branch -a
- 合并分支
将其他分支合并到当前分支:
bash
git merge <branchname>
- 删除分支
删除本地分支:
bash
git branch -d <branchname>
强制删除未合并的分支:
bash
git branch -D <branchname>
删除远程分支:
bash
git push origin --delete <branchname>
恢复和回退
Git 提供了多种方式来恢复和回退到之前的版本,不同的命令适用于不同的场景和需求。
以下是几种常见的方法:
git checkout:切换分支或恢复文件到指定提交。
git reset:重置当前分支到指定提交(软重置、混合重置、硬重置)。
git revert:创建一个新的提交以撤销指定提交,不改变提交历史。
git reflog:查看历史操作记录,找回丢失的提交。
- git reset:重置当前分支到特定提交
git reset 命令可以更改当前分支的提交历史,它有三种主要模式:--soft、--mixed 和 --hard。
--soft:只重置 HEAD 到指定的提交,暂存区和工作目录保持不变。
bash
git reset --soft <commit>
--mixed(默认):重置 HEAD 到指定的提交,暂存区重置,但工作目录保持不变。
bash
git reset --mixed <commit>
--hard:重置 HEAD 到指定的提交,暂存区和工作目录都重置。
bash
git reset --hard <commit>
Git 进阶操作
- Git Stash:临时保存工作进度,方便切换任务。
- Git Rebase:将一个分支上的更改移到另一个分支之上,保持提交历史线性。
- git pull --rebase :
- 假设你的提交历史是 A-B-C,远程有人提交了 D:
- 你的状态:A-B-C(C 是你的本地提交)。
- 远程状态:A-B-D
- 执行 git pull --rebase:
- Git 会先把 C 拿掉,暂存起来。
- 把本地分支更新到 D(变成 A-B-D)。
- 把 C 重新应用在 D 之后(变成 A-B-D-C')。
- Git Cherry-Pick:选择特定提交并应用到当前分支。