Git
命令
bash
git config --globle user.name ""
git config --globle user.email ""
git config -l
git config --globle --unset []
git add []
git commit -m ""]
git log
//当行且美观
git log --pretty=oneline
//以图形化和简短的方式
git log --graph --abbrev-commit
git cat-file -p [3a6640b795dd96f8d1d4f7574c9db489cdc1a2ab]
git status
git diff file_name
版本回退
bash
git reset [ --soft | --mixed | --hard ] [3a6640b795dd96f8d1d4f7574c9db489cdc1a2ab]
op:
--soft : 只回退版本库,工作区和暂存区不回退。
--mixed : 回退版本库和暂存区,工作区不回退。(default op)
--hard : 全部都要回退
git reflog
撤销
只在工作区
bash
//将工作区回退到当前版本
git checkout --[file_name]
在工作区和暂存区
bash
//版本库和暂存区回退到当前版本
git reset HEAD [--mixed | --hard]
//工作区回退到当前版本
git checkout -- [file_name]
在工作、暂存、版本区
前提: commit 之后 没有 push
bash
//版本库和暂存区回退到上个版本
git reset HEAD^ [--mixed | --hard]
//工作区回退到当前版本
git checkout -- [file_name]
删除
bash
//删除工作区和暂存区的内容
git rm [file_name]
git commit -m ""
分支
分支管理
bash
//查看分支
git branch
//创建一个分支
git branch branch_name
//更换当前分支
git checkout branch_name
//创建并更换当前分支
git checkout -b branch_name
//合并分支
git merge branch_name
//删除分支
git branch -d branch_name
//分支未合并,但已经提交
git branch -D branch_name
分支冲突
merge 冲突需要手动解决,并且需要进行一次提交。
合并分支的时候最好使用no fast forward模式,以便于溯源提交。
bash
git merge -no-ff -m "merge dev2" dev2
bash
//将工作区内容保存
git stash
git stash pop
远程操作
bash
git clone url
git remote -v
使用ssh克隆仓库
bash
ssh-keygen -t rsa -C "email地 址"
//再将ssh公钥配置对应的平台
推送
bash
//本地:远程 分支名称相同可以省略
git push origin master:master
git push origin branch_name
拉取
bash
//远程:本地 分支名称相同可以省略
git pull origin master:master
配置.gitignore
bash
# 排除
*.so
*.cc
# 不排除
!c.so
bash
//强制提交忽略文件
git add -f file_name
git check-ignore -v file_name
git配置
bash
//git st
git config --global alias.st status
//git lpa
git config --global alias.lpa 'log --pretty=oneline --abbrev-commit'
标签管理
针对最后一次 commit 起一个 v1.0 的标签
bash
git tag v1.0 commitId
git tag
git tag -a v0.8 -m "important"
git show v0.8
git tag -d v0.9
//本地删除v1.0,再推送远端
git push origin:v1.0
git push origin v1.0
//推送所有标签
git push origin --tags
多人协作
bash
git branch -r
git branch -a
//查看链接情况
git branch -vv
//两种链接方式:
git branch --set-upstream-to=origin/dev dev
git checkout -b dev origin/dev
git pull
# 1拉取分支内的内容
# 2拉取仓库的内容(不用建立链接 )
各自负责对应的分支
远程删除分支后,本地依然能看到
bash
git remote show origin
git remote prune