Git 有三个分区:working directory,stage/index area,commit history
working directory:工作目录
stage/index area:暂存区,git add命令会把working directory中的修改添加到暂存区
commit history:git commit命令会把暂存区的内容提交到该分区。每个commit都有一个唯一的 Hash 值
三者的关系如下图所示:
常用 git 命令
# 初始化 git 项目
git init
# working dir -> stage area
git add .
# stage area -> commit history
git commit -m "xxxx"
# 查看工作区提交情况
git status
# 查看 git commit 提交记录
git log
# 创建新分支
git branch xxx
# 切换新分支
git checkout xxx
# 创建并切换新分支
git checkout -b xxx
# 将 commit history 区的代码恢复到 stage
git reset
常用 git 操作
把stage中的修改还原到working dir中
# 恢复某一个文件
git checkout [fileName]
# 恢复所有文件
git checkout .
把commit history区的历史提交还原到working dir中
git checkout HEAD .
将commit history区的文件还原到stage区
git reset [fileName]
git reset .
撤销git commit
git reset --soft HEAD^
# 等价
git reset --soft HEAD~1
# 撤销两次 git commit
git reset --soft HEAD~2
# 如果此时我们同时需要撤销远程的提交记录
# -f 表示强制推送到远程
git push -f
下面介绍一下几个参数
–mixed:不删除工作空间改动代码,撤销commit,并且撤销git add .操作
--soft`:不删除工作空间改动代码,撤销`commit`,不撤销`git add .
--hard` :删除工作空间改动代码,撤销`commit`,撤销`git add .
修改git commit -m " "注释
git commit --amend
合并相同的git commit
# 选择需要合并 git commit 最早的一个 id 的前一个 (因为不包括本 id 的 git commit)
git rebase -i [git logID]
当前我们只要知道pick和squash这两个命令即可
- pick的意思是要会执行这个 commit
- squash的意思是这个 commit 会被合并到前一个commit
撤销git rabase合并
# 查看本地记录
git reflog
git reset --hard [id]
# 同时修改远程的提交
git push -f