Git入门指令
一、下载及查阅资料
二、秘钥生成
1、****打开终端(git)进入.ssh目录
cd ~/.ssh
如果没有,创建改文件夹
mkdir ~/.ssh
2、生成RSA秘钥
ssh-keygen -t rsa -C "你的邮箱@xxx.com"
3、查看秘钥并将秘钥部署到github
cat ~/.ssh/id_rsa.pub
三、GIt常用操作
1、克隆远程仓库代码,生成本地仓库:
代码格式:
git clone “git@ssh地址”
例如:
git clone git@gitee.com:github-29529984/mooc.git
下载存储在远程仓库git@gitee.com:github-29529984/mooc.git的代码
2、提交代码:
a、git add . =>有有变化的文件提交到缓冲区
b、git commit -m 'init' => 设置评论
c、git push origin master => 上传到master分支
\# git add .
\# git commit -m 'init'
\# git push origin master
#将当前分支与远程分支融合,并存入缓冲区
\# git pull origin master
3、新建组合操作
a、新建项目文件,进入
cd 项目名
b、建立git仓库
git init
c、将项目中新增或有变化的文件上传到缓冲区
git add .
d、将add的文件添加备注
git commit -m '备注'
e、将本地仓库关联到码云上
git remote add origin git@gitee.com:shiqingqing/test.git
f、上传码云前要先pull 下,把码云中的代码与本地代码融合
git pull origin master
g、最后一步 上传代码到码云仓库
git push origin master
四、可能问题
问题1:如何去解决fatal: refusing to merge unrelated histories
答:因为两个仓库不同,发现refusing to merge unrelated histories,无法pull
因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在git pull,
这句代码是在git 2.9.2版本发生的,最新的版本需要添加--allow-unrelated-histories
git pull origin master --allow-unrelated-histories
参考:https://blog.csdn.net/m0_37402140/article/details/72801372
问题2: failed to push some refs to 'git@gitee.com:software_reliability/java_timeseries.git'
$ git push origin master
To gitee.com:software_reliability/java_timeseries.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitee.com:software_reliability/java_timeseries.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
原因:
出现错误的主要原因是github中的README.md文件不在本地代码目录中
可以通过如下命令进行代码合并【注:pull=fetch+merge]
git pull --rebase origin master
参考:https://jingyan.baidu.com/article/f3e34a12a25bc8f5ea65354a.html
问题3:git报错:'fatal:remote origin already exists'怎么处理?
步骤1:删除remote
git remote rm origin
步骤2:重新添加remote
git remote add origin git@ssh
问题4:删除git remote
迁移原文:https://blog.csdn.net/luodao_/article/details/79041918
删除****git remote
查看当前存在的远程连接remote
$git remote -v
---
origin https://github.com/luodao236/test.git (fetch)
origin https://github.com/luodao236/test.git (push)
test https://github.com/luodao236/onceAgain.git (fetch)
test https://github.com/luodao236/onceAgain.git (push)
---------------------
现在想删除其中的一个test,方法为:
git remote remove <name>
$ git remote remove test
结果为:
$ git remote -v
origin https://github.com/luodao236/test.git (fetch)
origin https://github.com/luodao236/test.git (push)
---------------------
问题5:ERROR: Repository not found.
$ git push -u origin master
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
-----
解决,添加公有秘钥到项目中