git 配置信息
# 当前git 配置信息
git config --list
# 可以查看用户名:
git config user.name
# 可以查看用户名:
git config user.email
# 切换用户:
git config --global user.name "xxx"
# 切换邮箱:
git config --global user.email "xxx"。
注:--global针对系统上所有仓库,去掉仅对当前仓库有效
基本命令
# 克隆,repo Git仓库地址;directory:本地目录,为空时默认当前。
git clone <repo> <directory>
# 初始化仓库
git init
# 提交代码
git add .
git commit -m '描述'
# 撤销
git reset --soft HEAD^
# 拉取
git pull
# 推送
git push
分支
#查看所有分支
git branch -a
* master //当前分支
remotes/origin/1.0
remotes/origin/1.1
remotes/origin/1.2
# 检出远端1.2 分支到本地1.2
git checkout -b 1.2 origin/1.2
# 切换本地为1.2分支
git checkout 1.2
# 合并1.2分支代码到当前分支
git merge orgin 1.2
# 查看分支列表
git branch —list
# 删除本地分支
git branch —-delete 'branchName'
# 删除远端分支
git push origin —delete 'branchName'
标签
# 查看所有标签
git tag
# 删除本地标签
git tag -d 1.0
# 推送删除标签
git push origin :refs/tags/1.0
子模块
应用场景
1、大项目模块开发,模块之间不相互影响时,只拉取主项目和模块项目开发时。
2、项目需要中包含并使用另一个项目。 也许是第三方库,或者你独立开发的,用于多个父项目的库。
Git的子模块(submodule)允许将一个 Git 仓库作为另一个 Git 仓库的子目录。 可以将另一个仓库克隆到自己的项目中,同时还保持提交的独立。
新建子模块
# [URL]:子模块对应的仓库相对或绝对路径
git submodule add [URL]
主工程中 .gitmodules 文件表示关联子模块信息,详情如下:
[submodule "web"]
path = web
url = http://XXX/XX_web.git
[submodule "XX-crud"]
path = XX-crud
url = http://XXX/XX-crud.git
克隆并检出子模块
# 1、克隆主项目,[URL]主项目地址。(子模块文件夹内容为空)
git clone [URL]
# 2、拉取子模块
# 把子模块的URL加入到.git/config
git submodule init
# 克隆子模块的仓库并检出父项目中指定的那个版本
git submodule update
# 上述脚本可合并为一个,如下:
git submodule update --init
# 若还要初始化、抓取并检出任何嵌套的子模块, 执行如下脚本:
git submodule update --init --recursive
# 克隆主项目并检出所有嵌套的子模块
git clone --recurse-submodules [URL]
# 注:如果想更新子模块,主项目先执行 git pull,再执行 git submodule update。
stash
当在新分支做新功能开发时,需要切换到其他分支修改bug,而当前代码为开发完成,可将当前修改存放于堆栈中,当bug修复完成,切回之前分支,并取出当前堆栈内容应用于该分支。
# 能够将所有未提交的修改(工作区和暂存区)保存至堆栈中,用于后续恢复当前工作目录。
git stash
# 作用同git stash,同时可以添加注释。
git stash save '注释'
# 查看当前 stash 中的内容。
git stash list
# 将当前stash中的内容弹出,并应用到当前分支对应的工作目录上。
# 注:该命令将堆栈中最近保存的内容删除(栈是先进后出),所以如果堆栈中有冲突,会报错,可以通过创建新的分支解决冲突。
git stash pop
# 将堆栈中的内容应用到当前目录,但不会将内容从堆栈中删除,适用于多个分支的情况。
# git stash apply [stash_name],如:git stash apply stash{0},将指定的堆栈内容应用于当前目录。
git stash apply
# 移除堆栈中指定的stash。
git stash drop + 名称
# 清楚堆栈中的所有内容。
git stash clear
# 查看堆栈中最新保存的stash和当前目录的差异;添加stash名称,则查看该stash与当前目录的差异;-p:查看详细不同。
git stash show
# 从最新的stash创建分支
git stash branch