开始(配置环境)
查看配置信息
git config -l
# 在有git仓库的地方显示如下
user.email=xxxx@163.com
user.name=name
core.editor=vim
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@192.168.2.61:likx/ElevatorEmbedded.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.add-testtask.remote=origin
branch.add-testtask.merge=refs/heads/add-testtask
# 没有git仓库的地方显示如下
user.email=xxxxx@163.com
user.name=name
core.editor=vim
这里是引用
查看git用户名、密码、邮箱的配置
git config user.name
git config user.password
git config user.email
设置git用户名、密码、邮箱的配置(修改只需要再设置一遍)
git config user.name "name"
git config user.password "123456"
git config user.email "xxxxx@163.com"
配置公钥
1、生成秘钥,youremail@example.com是在githug上注册的邮箱
ssh-keygen -t rsa -C "youremail@example.com"
# 上面的加密方式过期了,使用下面的
ssh-keygen -t ecdsa -C "youremail@example.com"
2、~/.ssh下有生成的秘钥
3、复制id_rsa.pub下的秘钥到github上
重新设置密码和链接不上仓库的问题
修改密码后发生校验失败
# 测试远程仓库是否链接
ssh -T <gitname>@github.com
# 如果显示显现受限,更新一下
eval "$(ssh-agent -s)"
后面再测成功
分支
新建分支
git checkout –b <branchname>
删除分支
git branch –d <branchname>
切换分支
git checkout <branchname>
合并分支
1、 切换到被合并分支
git checkout <branchname>
2、 合并被合并的分支
git merge <branchname>
远程仓库
添加远程仓库
#git remote add lkx git@192.168.2.61:likx/ElevatorEmbedded.git
git remote add <name> <git url>
同步本地仓库
git pull origin add-testtask
同步远程仓库
git push origin add-testtask
获取远程仓库指定目录/文件
# 创建空本地仓库
git init
# 以下在仓库里面操作
# 1、设置允许使用克隆子目录
git config core.sparsecheckout true
# 2、设置需要克隆的子目录,childfile是要克隆的目录
echo 'childfile*' >> .git/info/sparse-checkout
# 3、配置远程仓库
git remote add origin <giturl>
# 4、同步
git pull origin master
日志查看
查看提交日志
git log
回滚后查看所有的日志
git reflog
修改提交日志,输入下面命令,在弹出的界面上修改log内容即可;
(必须是最近提交,还没有push的注释???没验证,不知道是否是这样)
git commit --amend
提交
git diff
查看未提交的修改
git diff
查看2个分支的差异
git diff <num> <num>
git diff <branch>..<branch>
# 或者
git diff <num>..<num>
git diff <branch>..<branch>
# 如果只指定一个num,则是这个nun较HEAD的修改
补丁(diff方式)
git diff <num>_<num> >xxx.patch
# 打补丁,0,1,2,3是跳过目录的级数,0表示不跳目录,1表示跳过一级目录
patch -p<0/1/2/3> < file.patch
打补丁
.gitkeep和.gitignore
1、git提交的时候不允许提交空的文件夹,
2、.gitkeep作用就是占位这个文件夹,使这个文件夹可以提交;
3、.gitkeep也可以用.nofile代替,常用的还是.gitkeep;
4、.gitignore忽略跟踪指定的文件夹或者文件;
引用子模块
添加子模块
# url 远程仓库路径,例如:git@github.com:pChenGithub/open_lib.git
# path 工程放置路径 例如: xxx/xxx1,源码将会下载到xxx/xxx1,注意不同于clone,不是从工程文件夹开始的
git submodule add <url> <path>
添加了子模块后任然需要提交
# 参考git提交说明
git commit xxx
克隆项目子模块为空的问题,需要下载子模块
git submodule init
git submodule update
# 或者
git submodule update --init --recursive
同步子模块
1、到子模块目录,pull远程仓库代码
# 参考pull操作
git pull xxx
2、更新子模块后需要提交更改