git 常用简单操作
第一步 拉下你的代码,或者创建本地代码库,git init
-
git clone
-
git checkout -b branchname
第二步 整一个开发要用的分支 git checkout
-
git checkout -b
-
创建新分支,分支名后,可以添加 origin 远程分支名,同步远程分支创建,
git checkout -b master origin master
-
分支间需要乱切时
git checkout branch
第三步,提交代码是,注意commit格式 ,format
-
说明下修改类型 type: feat,fix,doc,refactor,style,test
-
说明下影响范围 scope
-
给你的提交价一句话的描述 subject
-
详细描述下你的提交都做了什么,必要时贴上url body
-
示范提交 type scope:subject body ,
fix 局部改动:a模块 主要改动为。。。 技术文档。。。
分支提交多了想少一点提交记录 rebase
合并提交成一个新的commit hash,基于原分支最新的commit提交,保持线性提交
git rebase -i
-
(startid
-
HEAD ~num
参数编辑,修改完后 git push -f
-
r 只修改commit
-
e 修改commit和提交的内容
-
s 合并commit到上一个提交
-
f 同s,不保留该提交的注释信息
-
d 丢掉commit
如果想复用原有分支,且复用上次提交 amend, 提交完后 git push -f
git commit --amend --no-edit
如果想会退到之前的版本,不想merge了或者代码有问题 reset
git reset --hard commit_id
看当前分支提交历史 git log
看分支历史操作 git reflog
想切换到其他分支干活,写了的代码不想扔 git stash
-
先起个名字存下来,
git stash save "comment"
-
要用的时候捡起来
git stash apply
,还有其他选项,有需要自己详细了解
暴力合并,有冲突解决冲突,只是会多个merge的记录git merge branch ,如果合并远程有冲突参考如下:
-
Step 1. 拉下待合并分支:
git fetch origingit checkout -b cancel origin/cancel
-
Step 2. 合并分支:
git fetch origingit checkout origin/mastergit merge --no-ff cancel
-
Step 3. 推送到远程:
git push origin master
想拉取其它分支一部分提交时,这样可以少解决点冲突git cherry-pick
git cherry-pick commithash
-
拉去部分提交
git cherry-pick A..B
-
转移多个提交,不包括a ,
git cherry-pick A\^..B
-
转移多个提交,包括a
git cherry-pick A^..B
本地更新远程代码 git fetch origin
idea 自带工具使用,看diff,和commit提交差异
- git diff commit1 commit2
不提交多余文件, 删除步骤
-
删除不必要文件夹,比如编译文件,.idea,.eclipse这种
-
git rm -r --cached .idea
(cached不会把本地的.idea删除) -
git add .
-
git commit -m '忽略idea'
-
git pull
-
git push
删除无用分支
-
本地,
git branch -D
分支名 -
远程
git push origin --delete dev
gitlab
-
合并提交后移除不必要分支 remove source tree
-
压缩提交 squash commit
-
让人检查同意 need one approve
-
权限控制,owner,master,developer,reporter