git命令使用

217 阅读2分钟

一、从本地创建git仓库步骤

1、建立git仓库,cd到你的本地项目根目录下,执行git命令 

 git init 

2、将项目的所有文件添加到仓库中 

 git add . 

3、将add的文件commit到仓库 

 git commit -m "注释语句" 

4、去github上创建自己的Repository 

输入一个仓库名称,完成创建:

点击Clone or download按钮,复制弹出的地址"git@github.com:luoyanbei/binance-quant-robot.git",记得要用SSH的地址,尽量不要用HTTPS的地址。

 5、将本地的仓库关联到github上

 git remote add origin git@github.com:luoyanbei/binance-quant-robot.git

6、上传github之前,要先pull一下

执行如下命令: 

 git pull origin main 

7、上传代码到github远程仓库 

 git push -u origin main

二、常见错误

1、pull时报错

% git pull origin main 

错误提示:

Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

没有配置证书,无法执行该操作。

解决:

(1)本地生成RSA秘钥

% ssh-keygen -t rsa -C 你的github注册邮箱@126.com 

输出如下:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxxx/.ssh/id_rsa): /Users/xxxx/.ssh/id_rsa
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub.

rsa秘钥已生成并保存到了/Users/xxxx/.ssh/ 目录下。

(2)github上在 Setting 中找到 "SSH and GPG keys"

     

点击"New SSH key",新增公钥:

2、推送代码失败

% git push -u origin main 

错误提示:

 To github.com:luoyanbei/binance-quant-robot.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'git@github.com:luoyanbei/binance-quant-robot.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.

解决:

 问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。

可以执行强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容 

 git push -f

具体指令是:

git push -f -u origin main

本次仓库github地址

github.com/luoyanbei/b…

这是一个简易量化的Python项目,感兴趣的可以看下