1、回滚之前的版本
git reset <commitId>
(base) dongnengwuxiandeMac-mini:KTMRobot eddy$ git reset 93ed329966f72818a9b528f763ce4c20cf2c8da4
Unstaged changes after reset:
M KTMRobot.xcodeproj/project.xcworkspace/xcuserdata/eddy.xcuserdatad/UserInterfaceState.xcuserstate
M auto.sh
2、查看提交commitId
git log
(base) dongnengwuxiandeMac-mini:KTMRobot eddy$ git log
commit 47863bc66fff4b462106a633c40b10c3f7a14090 (HEAD -> master, origin/master)
Author: Eddy Wong <784550567@qq.com>
Date: Fri Dec 4 10:33:58 2020 +0800
提交
commit 93ed329966f72818a9b528f763ce4c20cf2c8da4
Author: Eddy Wong <784550567@qq.com>
Date: Thu Nov 12 15:15:18 2020 +0800
更新适配unity、cocos
commit ea3da4852e3d08c9f649924b348f05844d23ac15
Author: Eddy Wong <784550567@qq.com>
Date: Wed Oct 14 10:12:59 2020 +0800
commit
commit 45d6601fa31fefce1841d70af77e5290c9e2f02f
Author: Eddy Wong <784550567@qq.com>
Date: Wed Oct 14 10:11:51 2020 +0800
Initial commit
:...skipping...
commit 47863bc66fff4b462106a633c40b10c3f7a14090 (HEAD -> master, origin/master)
Author: Eddy Wong <784550567@qq.com>
Date: Fri Dec 4 10:33:58 2020 +0800
查看提交的commitId,git log显示不完,光标显示在冒号的位置,按方向键上下翻,按q会退出
有是变成红色的代码,git reset回退版本时,回退的代码是保留下来的,被放到工作区间,git reset --hard就是把回退的代码直接删除掉,不会保留下来,这个指令很危险,一般不用。
3、提交到远程
git push -f
把刚才reset以后的版本推到远程,也覆盖掉远程仓库的最新的2个提交,回退远程的版本, -f参数强制覆盖远程,这样就可以把线上的版本给回退了
(base) dongnengwuxiandeMac-mini:KTMRobot eddy$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/hohua88/KTMRobot.git
+ 47863bc...93ed329 master -> master (forced update)
4、暂存修改
git stash
在工作进行中时,有些东西常常会处于凌乱的状态。如果这时需要切换到不同的分支该怎么办呢?Git是不允许你这么做的,因为还有尚未保存的修改。坦率地说,你并不想将半成品提交上去,后面再来修改。这个问题的解决之道就是使用git stash命令。Stash会接收工作目录的当前状态(比如说,修改了的追踪文件与暂存区的修改等),并将其保存到未完成的修改栈中,这样后面随时可以再来修改。可以通过如下命令来暂存你的工作
(base) dongnengwuxiandeMac-mini:KTMRobot eddy$ git stash
Saved working directory and index state WIP on master: 93ed329 更新适配unity、cocos
关于暂存,还有其他一些选项可用,如下所示:
git stash list # 查看暂存列表
git stash pop git stash pop stash@{0} # 弹出指定的暂存
git stash save "describe it" # 设置暂存名称
git stash clear # 清空暂存
git stash drop # 删除最新的stash
5、合并制定提交
git cherry-pick
合并指定的commitid并提交到远程
git merge 会把分支上的新提交都会合并到master主分支上,但是有的时候我们并不想合并所有的新提交,而是挑选几个commitid合并
(base) dongnengwuxiandeMac-mini:KTMRobot eddy$ git log
commit a94cf7f307504129ab47284f7b960d40a878f08d (HEAD -> dev, origin/dev)
Author: Eddy Wong <784550567@qq.com>
Date: Fri Dec 4 11:21:53 2020 +0800
Create 测试4.txt
commit 07b226dde4e5c17c1bf1d78be1a7f729f731daf1
Author: Eddy Wong <784550567@qq.com>
Date: Fri Dec 4 11:21:49 2020 +0800
Create 测试3.txt
commit e4700857f4fb8ed83df616c2756bf2b3840c9d3e
Author: Eddy Wong <784550567@qq.com>
Date: Fri Dec 4 11:21:46 2020 +0800
Create 测试2.txt
commit 1f080b406c4c44c2ac23c5c75d8c21ba3d73a68d
Author: Eddy Wong <784550567@qq.com>
Date: Fri Dec 4 11:21:41 2020 +0800
Create 测试1.txt
首先需要先切回master分支
(base) dongnengwuxiandeMac-mini:KTMRobot eddy$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
(base) dongnengwuxiandeMac-mini:KTMRobot eddy$ git cherry-pick a94cf7f307504129ab47284f7b960d40a878f08d 1f080b406c4c44c2ac23c5c75d8c21ba3d73a68d
[master 17d8722] Create 测试4.txt
Date: Fri Dec 4 11:21:53 2020 +0800
1 file changed, 51 insertions(+)
create mode 100644 "\346\265\213\350\257\2254.txt"
[master e15d87d] Create 测试1.txt
Date: Fri Dec 4 11:21:41 2020 +0800
1 file changed, 51 insertions(+)
create mode 100644 "\346\265\213\350\257\2251.txt"