Git使用说明及常用命令
1 客户端的安装
具体的安装过程没有特殊的地方,一切默认就行。有兴趣的可以仔细看看
这里提供两个下载地址,界面风格略有些不同
2 客户端配置
$ git config --global user.name "用户名"
$ git config --global user.email 你的邮箱
生成密钥
$ ssh-keygen -t rsa-C "shenyang@seassoon.com"
生成两个密钥文件在文件夹下
打开id_rsa.pub文件,将内容复制到git平台上
至此git就可以正常使用了
3 Git使用
希望可视化工具来操作的可以直接跳到第4
3.1 需要从远程仓库拷贝内容
git clone 仓库地址
3.2 创建内容并提交到远程仓库
git init
git remote add origingit@10.1.12.146:shenyang/mytest.git
git add .
git commit -m "Initial commit"
git push -u origin master
4 使用SourceTree可视化工具
工具的安装过程需要用户名和密码挺麻烦的,下面教大家怎么绕过这个过程
5 Git常用命令
clone = init + remote add + pull
= init + remote add + fetch +checkout
1) 远程仓库相关命令
检出仓库:$ git clonegit://github.com/jquery/jquery.git
查看远程仓库:$ git remote -v
添加远程仓库:$ git remote add [name] [url]
删除远程仓库:$ git remote rm [name]
修改远程仓库:$ git remote set-url--push[name][newUrl]
拉取远程仓库:$ git pull [remoteName][localBranchName]
推送远程仓库:$ git push [remoteName][localBranchName]
分支:
1.查看所有分支
git branch -a
2.查看本地分支(有*的是当前正在使用的分支)
git branch
git branch -r (查看远程分支)
3.创建本地分支
git branch local_branch
4.切换到分支
git checkout local_branch
5.创建本地分支并切换到该分支
git checkout -b local_branch
6.本地分支推送到远程分支(创建远程分支)
a)当前在分支上
git push origin remote_branch
b)不在当前分支上
git push origin local_branch:remote_branch
7.删除远程分支(本地分支依然在)
git push origin :remote_branch
8.删除本地分支
git branch -d local_branch
标签:
1.查看所有标签
git tag
2.查看符合要求的标签,*为模糊查询
git tag -l 'V1.*'
3.创建标签
a)轻量标签,无注释
git tag v1.1.1
b)附注标签
git tag -a v1.1.2 -m '标签说明'
参数a为annotated,标签类型
参数m指定标签说明信息保存在当前标签中
4.显示当前标签的内容(还会显示打标签的所有内容。。)
git show v1.1.1
6 常见问题
6.1 git pull 失败 ,提示:fatal: refusing to merge unrelated histories
git pull origin master --allow-unrelated-histories
参考:https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories
6.2 windows使用git时出现:warning: LF will be replaced by CRLF
windows中的换行符为 CRLF,而在linux下的换行符为LF,所以在执行add . 时出现提示。
git config --global core.autocrlf false //禁用自动转换
参考:http://blog.youkuaiyun.com/starry_night9280/article/details/53207928
6.3 fatal: HttpRequestExceptionencountere
执行$ git push origin master一直推送失败。