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 [email protected]
若github不认识你的话,以下命令作为登录的备用选项(需要的时候git也会提醒你):
git config --global user.name "name"
git config --global user.email email@dns

若你的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 [email protected]:yourUserName/yourRepositoryName.git
git push yourRemoteName master

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

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]: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报错处理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章