git命令行添加本地仓库到github网站以及删除、更新操作

1.在github上建好仓库

2.sudo apt-get install git

3.生成非对称秘钥,将公钥放到github账户设置中

4.验证能否成功连接到github

5.设置username和email

以上内容在 ubuntu上传代码文件到github 博客中写得很明白。

2019.12.13添:
下面将以上内容的代码部分写下来(毕竟自己经常忘):
生成密钥命令: ssh-keygen -t rsa -C "youremail@dns"
复制.ssh文件夹中的id_rsa.pub文件,粘贴至github网站Account Setting-SSH Keys-Add SSH
最后命令: ssh -T git@github.com
若github不认识你的话,以下命令作为登录的备用选项(需要的时候git也会提醒你):
git config --global user.name "name"
git config --global user.email email@dns

2020.7.14重新整理密钥管理与github连线的操作,整理自Github创建密钥

打开git bash,输入命令

git config --global user.name "username"
git config --global user.email "emailname"
ssh-keygen -t rsa -C "emailname"(命令下的问题直接按回车)
eval $(ssh-agent -s)(git命令行能识别这个命令,然后开始ssh-agent的建立?上面的操作没有这个步骤,最后并没有完成密钥认证)
ssh-add /c/Users/tellw/.ssh/id_rsa
clip < /c/Users/tellw/.ssh/id_rsa.pub
到github网站用户Settings界面SSH and GPG keys栏目中将粘贴板中的公钥添加进去
最后,可以测试一下:ssh -T git@github.com得到Hi tellw! You've successfully authenticated, but GitHub does not provide shell access.结果说明添加成功。

若你的github账号永远都是如此,则以上操作只用执行一次。

平常使用git的命令过程

删除操作:

git clone yourRepositoryURL
cd yourRepository
git rm -r theDirectory/          (删除目录)
git rm theDirectory/theFile      (删除文件)
git commit -m 'del'
git push origin master

参考链接:使用 Git 上传、更新、删除 GitHub 仓库文件

远端更新(将本地代码更新至github服务器)操作:(延续删除操作的风格)

git status
git add *
git commit -m update
git pull origin
git push origin master

参考链接:将本地代码使用Git上传/更新至Github,并从Github下载代码

本地操作:

git pull origin

下面举例说明:

git下载fork的一个Information-Maximizing-GAN项目

在github网站上上传一个hello.py文件,开始进行本地更新

在本地创建world.py文件后上传更新到github服务器中

已更新到github网站端

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-----------------------------------以下是在掌握上述精简命令前学习的git操作,只是用于个人备忘。---------------------------------------

下面的操作是要你每次添加本地代码到远程服务器中都要进行的。

6.接下来cd到本地仓库目录中,执行以下命令:

git init
git add .
git commit -m "your commit comment" (此行也可以写git commit, 
不过命令回车之后会进入vi界面让你写comment,没有像前面那条写一个随意的comment速度快)
git commit -a
git remote add yourRemoteName git@github.com:yourUserName/yourRepositoryName.git
git push yourRemoteName master

若以上最后一条操作出现如下报错:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:yourUserName/yourRepositoryName.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

执行以下命令:

git fetch yourRemoteName
git merge yourRemoteName/master
git push yourRemoteName master

若第二条命令出现报错:
fatal: refusing to merge unrelated histories
执行git merge yourRemoteName/master --allow-unrelated-histories

以上应该可以实现本地仓库上传到github仓库中。

第6条操作参考链接:

更多git使用命令

出现“因为您当前分支的最新提交落后于其对应的远程分支”等类似错误后操作(之前这个链接已经引用过)

fatal: refusing to merge unrelated histories报错处理

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值