Git 全局通用设置
git init //初始化本地git仓库
rm -rf .git //删除已存在的本地git仓库
git config --global user.name "自定义用户名" //标记你的名字
git config --global user.email "注册绑定用的邮箱" //标记你的邮箱
git add . //提交文件夹里的所有文件、将文件保存到缓存区、等待提交到哪个地方
git commit -m "first commit" //添加文件描述
Git提交
git remote add origin https://gitee.com/xxx521/xxx.git //指定提交的地址、和远程仓库进行关联
git remote rm origin //rm取消上一步操作
git push -u origin master //将本地仓库推送到远程仓库、提交到指定的地址,成功提交
//以上是提前配置好密钥绑定之后,日常可以以上操作
Git错误情况演示
git push origin master //push以后如果出现错误、采取如下操作
git pull origin master --allow-unrelated-histories //把远程仓库和本地同步,消除差异
git add . //提交文件夹里的所有文件、将文件保存到缓存区、等待提交到哪个地方
git commit -m "first commit" //添加文件描述
git push origin master //重新将本地仓库推送到远程仓库、成功提交
以上错误解释、遇到refusing to merge unrelated histories这个提示是因为两个仓库不同,发现refusing to merge unrelated histories,无法pull。要把两个不同的项目合并,git需要添加一句代码,在git pull,这句代码是在git 2.9.2版本发生的,最新的版本需要添加–allow-unrelated-histories
Git生成SSH密钥
码云(https://gitee.com/help/articles/4181#article-header0)
1.在git官网下载对应版本的git并按提示安装
2.安装成功后打开git Bash 设置用户名和邮箱
git config --global user.name "YourName"
git config --global user.email "email@example.com"
3.生成SSH Key
在git Bash 中输入ssh-keygen -t rsa -C "email@example.com"
连按三次enter键,会看到生成了 一个id_rsa.pub文件
4.登录码云并添加密钥
在提示的文件目录下打开id_rsa.pub文件(或在git Bash中执行 cat id_rsa.pub命令)
复制全部内容到码云添加密钥
(https://gitee.com/profile/sshkeys)
5.测试是否连接成功
git Bash中输入: ssh -T git@git.oschina.net, 返回 Welcome to Git@OSC, yourname!则成功
备注:如果两台电脑(公司和私人)都要使用同一个码云账号管理项目,那就在码云里面配置两台电脑分别对应的SSH Key
Github(https://blog.csdn.net/weixin_41990913/article/details/91373362)
1、打开git bash添加上自己的git用户名和git邮箱;
2、添加上远程仓库:git remote add [shortname] [url]
3、输入命令:ssh-keygen -t rsa -C "youremail@example.com"
4、然后一直回车,成功的话会在 ~/ 下生成 .ssh 文件夹,进去,打开 id_rsa.pub,复制里面的 key。
5、回到 github 上,进入 Account => Settings(账户配置)。
6、左边选择 SSH and GPG keys,然后点击 New SSH key 按钮,title 设置标题,可以随便填,粘贴在你电脑上生成的 key。
7、在git bash中输入 ssh -T git@github.com即可查看是否成功。
Git命令全介绍
https://mp.weixin.qq.com/s/YU43b7OeBRJT1puGcj7HwA
Git案例
git clone url(项目仓库地址) //克隆一个远程仓库,就是在本地建立一个新的项目。
git pull origin master //如果已经有一个项目,通过这条指令可以直接更新该项目
提交修改的项目,步骤如下:
git status
git add -A
git commit -a -m "填写修改的内容"
git push origin master
补充一个可能遇到的警告:
warning: LF will be replaced by CRLF in ......
The file will have its original line endings in your working directory.
解决方法:
git config --global core.autocrlf false
原因是路径中存在 / 的符号转义问题,false就是不转换符号。
而默认是true,相当于把路径的 / 符号进行转义,这样添加的时候就有问题.