git语法
git clean
删除当前未跟踪的文件git log
查看日志- git commit 提交本地修改更新的代码快照
git branch (branchname)
新建分支git branch
查看现有分支git checkout (branchname)
切换分支,实际上是切换到当前head所指的commitgit merge (branchname)
,合并(branchname)
到当前分支git rebase other_feature
也是合并分支,会将other_feature
分支下的提交合并到当前分分支,但是会将当前分支的基分支改变,改变成other_feature
,使git tree
更简洁
git rebase
,顾名思义,会改变基分支,虽然看起来更简洁,但是改变基分支之后,就无法查到之前是基于哪个分支开始开发的,所以git merge
更常用
HEAD
指向当前分支的指针
git checkout (ref)
head切换到到ref所指commit中HEAD^
当前head所指commit的前一个,HEAD~3
当前commit所指的前3个commitgit branch -f main HEAD^
切换分支main所指向的commit为HEAD^git reset HEAD^
放弃当前head所指commit,回退代码到上一个commit,这个是针对本地的仓库//当对一个方法修改错误后提交了,可以使用这个进行回退git revert head
放弃当前head所指commit,回退代码到上一个commit,针对远程仓库的,使同事也看到回退的代码git stash
隐藏当前修改,转移到另一个分支上
more
git cherry-pick c3 c4
将提交的c3、c4commit,转移到当前分支上,不限分支git rebase -i (branchmame)
执行这个命令后,可以对(branchname)到当前分支中提交的commit进行删选
开发实战
- 想要回退代码到
push
之前的commit,后面的都不要了:
git reset --hard target-commit
git push -f origin target_feature
- 某几个分支不需要,用
git revert
,上线前比较紧急时,直接到公司的GitLab
上修改 - 公司上线后,现有代码出错,想回退到之前正确的提交,但是想保存提交历史,且不一个个的
revert
,用reset
git checkout orgin master
git pull orgin master
git reset --hard correct_feature
git reset --soft orgin master
git commit -m "rollback"
git push orign master
关于已提交的代码回滚,更详细的请到:https://blog.csdn.net/weixin_36691991/article/details/125995535
- 已经
commit
未push
的代码回退
git reset --soft HEAD~1 //撤销提交,但保留修改的内容
git reset --soft HEAD~1 //撤销提交,不保留修改的内容