git 常用命令总结(一)

记录 Git 的一些基础操作命令, 假如不记得了就来此查阅

① 修改文件名

git mv readme.txt  readme.md
git commit -m"rename readme file"

② 清理暂存区

//把暂存区全给清理了
git reset --hard

③ 分支相关

//查看当前分支
git branch 

//创建并切换到一个新分支
git checkout -b new_branch
//这一行等于 git branch new_branch  + git checkout new_branch
//或者使用这个
git switch -c new_branch2

//删除不需要的分支
git branch -d new_branch2
//远程分支
git branch -al 
git push origin --delete 远程分支名
//or git push origin :远程分支名

//将新建分支与远程仓库的分支进行连接
git branch --set-upstream-to=origin/远程分支名

④ commit 相关

// 修改commit message
git commit --amend
// 修改之前 commit 的备注
1. git rebase -i  (指定commit 的前一个 commit id, 第一个commit的话需要在第二步中手动添加)
2. 将需要修改的 commit message 前的 pick 修改为 r, 然后保存退出
3. 在弹出的页面中修改 commit message,然后保存退出即可

// 合并连续多个 commit
1. git rebase -i  (指定commit 的前一个 commit id)
2. 将需要修改的 commit message 前的 pick 修改为 s, 然后保存退出
3. 在弹出的页面中直接保存退出,也可以根据需要添加一些备注

// 合并不连续多个 commit
在第二步弹出的编辑页面手动剪切 不连续的多个 pick 为连续的

//删除不需要的 commit
git reset --hard  (commit id)

//比较 commit 之间的差异
git diff id1 id2 (指定文件名)  

//撤销 commit
①撤销commit 至 add 状态(暂存区)
git reset --soft HEAD^ (也可以使用 HEAD~1, HEAD~2是把近两次 commit都恢复)

②撤销 commit 至 工作区
git reset --mixed HEAD^ 

③撤销commit 至上一个 commit状态,把工作区这次修改的内容全部放弃
git reset --hard HEAD^ 

⑤ 文件误删

//先查看哪个文件被删除了
git status

//方式①
git checkout  文件

//②
git restore  文件

//方式③
git reset HEAD  文件
git checkout    文件

⑥暂存当前工作区内容,处理其他需要求后再恢复

//暂存工作区修改,
git stash
//或者保存一下当前工作的标签
git stash save "do xxx"

//并且查看一下
git stash list

此时 git status 就是干净的了

*******************
//拉取代码解决一个紧急任务  远程仓库分支名+本地分支名
git pull origin master
//do something and push
git push
*******************

//还原 stash, index 为stash list的索引
git stash apply stash@{
    
    index} (这个会保留 stash)  
或者     
git stash pop (stash@{
    
    0}

⑦暂存区

//检查暂存区和当前分支的差异
git diff --cached
//如果发现不及预期修改的内容,那么修改后再 add 一次, 检查无误再 commit

//检查工作区和暂存区的差异
git diff
git diff --(指定文件名)

//撤销暂存区的文件
git reset HEAD -- 指定文件名 (否则还原全部暂存区) 

//工作区恢复成暂存区
git checkout -- 要恢复的文件名

猜你喜欢

转载自blog.csdn.net/PX1525813502/article/details/126072770