Git 常用命令
Git初始化
: git init
Git设置配置文件
:git config --global user.name ‘’
:git config --global user.email‘’
:git config --local user.name ‘’
:git config --local user.email‘’
Git 提交描述写错修改提交描述
:git commit --amend -m ‘再次描述’
Git日志查看查看最近n条
:git log -3 查看最近3条
:git log --oneline
Git查看帮助信息
:git help config
Git创建分支
:git branch new_branch
Git 切换分支
:git checkout new_branch
Git创建并切换到分支
:git checkout -b new_branch
Git删除分支
:git branch -d new_branch
Git 合并到当前分支
:git merge new_branch
Git 添加修改到暂存区
: git add text.txt
: git add . (添加所有修改不包括.gitignore中的文件)
: git add * (添加所有修改包括.gitignore中的文件)
Git add的三种使用情况
1. 将未被追踪的文件添加到暂存区
2. 将修改的文件添加到暂存区
3. 将合并冲突的文件添加到暂存区
Git 丢弃掉相对于暂存区中最后一次添加的文件内容所做的变更
:git checkout -- test.txt
Git 将之前添加到暂存区(stage, index)的内容从暂存区移除到工作区
: git reset HEAD test.txt
Git 恢复到某版本代码
: git reset --hard HEAD^ 恢复到上一版本代码
: git reset --hard HEAD^^ 恢复到上上一版本代码
: git reset --hard <commit_id> 恢复到某一版本代码
Git 切换到某个提交
: git checkout 97f8b2d
Git 创建分支以某个提交做基础
:git branch new_branck 97f8b2d
Git stash将未提交的修改临时保存
: git stash
Git 将临时修改保存并自定义名称
: git stash save ‘name’
Git 查看临时修改保存列表
:git stash list
Git 还原保存的临时修改
: git stash pop
Git还原至指定stash
: git stash apply stash@{1}
Git标签
新建标签,标签有两种:轻量级标签(lightweight)与带有附注标签(annotated)
创建一个轻量级标签
:git tag v1.0.1
创建一个带有附注的标签
: git tag -a v1.0.2 -m ‘release version’
删除标签
: git tag -d tag_name
查看标签
: git tag
: git show v1.0 查看标签信息
: git tag -l ‘?2*’ 模糊查询标签
Git blame查看某文件提交id,作者与修改
: git blame test.txt
Git diff查看工作区与暂存区差异
: git diff test.txt
Git diff查看工作区与最后一次提交的差异
: git diff HEAD test.txt
Git diff查看暂存区与最后一次提交的差异
: git diff --cached
Git diff查看暂存区与某一次提交的差异
: git diff --cached commitid
Git 远程仓库
查看远程仓库 origin
: git remote show
查看远程仓库详细信息 origin
: git remote show origin
删除remote
: git remote remove <name>
Git开发使用模型
- Gitflow
- 基于Git分支的开发模型:
develop分支 开发分支,频繁变化的分支
test分支 测试分支,供测试与产品等人员使用的分支,变化不是特别频繁
master分支 生产发布分支,变化非常不频繁的一个分支由test分支合并过来
bugfix(hotfix)分支 生产系统出现紧急bug用于紧急修复的分支
Git远程x
- Git远程拉取仓库
Eg: git init
: git remote add origin git@github.com:gitlecture/gitlecture.git
: git remote show origin(添加公钥/私钥后可使用)
: git push -u origin master (推送代码并关联master与远程master)
- 查看远程分支
: git branch -a
: git branch -av 显示最后一次提交信息
- git下载远程分支
Eg: git clone git@github.com:gitlecture/gitlecture.git
- git pull/ git push的使用
:git pull (拉取远程代码 git pull = git fetch + git merge)
:git pull (拉取远程代码完整命令 git pull = git pull origin src[github]:dest[local])
: git push (推送本地代码到远端 git push = git push origin src[local]:dest[github])
Src 代表本地分支名称,dest代表远程分支名称
6.关于Git分支的最佳实践(远程)
- 通常来说,git分支会有如下几种
- Master分支
- Test分支
- Develop分支
- Hotfix分支
7.Git远程分支、别名、gitk与git gui
7.1 git 合并原则,git遵循三方合并原则
7.2 git 别名 git config --global alias.br branch
(使用git br 代替git branch)
外部命令别名git config --global alias.ui ‘!gitk’
(使用git ui代替gitk)
8.创建远程分支并设置成上游分支(当前属于develop分支)
: git push --set-upstream origin develop
: git push --set-upstream origin src:dest
9.拉取远程分支并与本地分支关联(当前属于master分支)
//在本地创建develop分支并追踪远程的develop分支
:git checkout -b develop origin/develop
:git checkout --track origin/develop (本地新建分支与远程分支名字一致)
10.删除远程分支develop
: git push origin :develop (将一个空分支推送到远程develop)
:git push origin --delete develop
- 推送git标签到远程(git push 默认不会推送标签到远程)
: git push origin v1.0
Git gc 垃圾收集
Git gc垃圾收集将分散在不同目录的文件收集到packed-refs
: git gc
Git会将文件文件存放在.git/objects
Git裸库与submodule
Git裸库:没有工作区的git仓库(服务器上的git托管仓库)
创建裸库:git init --bare
Git submodule:
1.关联子模块到mymodule_dir文件夹下
:git submodule add git@github.com:gitlecture/git_child.git mymodule_dir
在parent中更新所有submodule
: git submodule foreach git pull
2.克隆父工程(默认clone 不会拉取submodule代码,要在clone父工程后再执行submodule初始化)
: git clone git@github.com:gitlecture/parent.git
: cd mymodule_dir
: git submodule init
: git submodule update --recursive
或者简易clone
: git clone git@github.com:gitlecture/parent.git git_parent_dir --recursive
3.删除submodule
: git rm --cached mymodule_dir 将submodule的文件夹从缓存区移除
: rf -rm mymodule_dir 将submodule的文件夹实体删除
: git commit -m ‘remove submodule’ 提交删除动作
: git push 推送删除到远程
: rm .gitmodules 将.gitmodules文件删除
: git commit -m ‘remove .gitmodules’ 提交删除动作
: git push 推送删除到远程
Git subtree:
Git subtree 出现在git submodule 之后解决了双向修改的问题,官方推荐使用git subtree
引入subtree:
: git remote add subtree-origin https://gitee.com/mjy/submodule.git
: git subtree add --prefix=subtree_dir subtree-origin master --squash
: git subtree add --prefix=subtree_dir subtree-origin master
添加subtree到subtree_dir文件夹 使用subtree-origin 拉取master分支 忽略历史提交信息
--squash 会使commit合并成一条,但是会形成新的commitid,会丢失共同的分支点
注意如果使用squash参数,则之后使用命令时都要使用squash
//subtree拉取
: git subtree pull --prefix=subtree_dir subtree-origin master
//subtree推送
: git subtree push --prefix=subtree_dir subtree-origin master
Git cherry-pick
Cherry-pick主要应用在本地分支上,解决修改错分支问题
: git cherry-pick <其他分支commit_id>
Git rebase
Rebase:变基,衍合,意为改变分支的根基
rebase的功能类似于merge,不过二者的工作原理和过程完全不一样
Git rebase的最佳实践:不要对master分支执行rebase,执行rebase的分支是未提交到远程分支的本地分支,因为rebase会修改提交历史信息,注意绝对不要在与别人共享分支下做rebase操作。