推送本地项目到github

1.github创建new repository

  • 输入repository name
  • 描述
  • 初始化README.md文件
  • 添加.gitignore,可以在下拉框中选择你的项目类型,比如Java,则可以获得针对性的忽略文件.
  • 添加协议
  • 创建repository

2.新建ssh key

    1. Open Git Bash
    1. Paste the text below, substituting in your GitHub email address.
ssh-keygen -t rsa -b 4096 -C "*your_email@example.com*"

This creates a new ssh key, using the provided email as a label.

 Generating public/private rsa key pair.
    1. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
Enter a file in which to save the key (/c/Users/*you*/.ssh/id_rsa):*[Press enter]*
Enter passphrase (empty for no passphrase): *[Type a passphrase]*
Enter same passphrase again: *[Type passphrase again]*

3.添加你的ssh key到ssh-agent

  • 1.确保ssh-agent正在运行
# start the ssh-agent in the background
eval $(ssh-agent -s)
Agent pid 59566
    1. 添加你的ssh私钥到ssh-agent.
ssh-add ~/.ssh/id_rsa

3.上传公钥到github

进入github官网,依次点击头像,Setting,SSH and GPG keys,然后点击New SSH key


输入title和.ssh/id_rsa.pub里面的值

点击Add SSH key即可,重新登录github账户,查看ssh key是否添加成功

4.本地确认ssh key是否上传成功

验证是否成功,在git bash下输入

$ ssh -T git@github.com

5.新建本地项目

mkdir testpush
cd testpush

6.初始化git

git init

7.设置全局变量

接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们

$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"

8.提交上传

比如我上传一个当前文件夹下的src目录和pom.xml文件

git add ./src/*
git add pom.xml
git commit -m "first commit"
git remote add origin %https://github.com/youproaddress.git%
git push -u origin master

%%中为你的项目地址.
还有一点需要注意,我们的项目url应该选择https协议的,选择git协议的会提示没有权限的错误.
当然了https协议的url每次提交都需要输入用户名和密码.
而git协议的不需要,如何设置为git协议,后面会讲.
当然了,还是建议使用https协议,虽然麻烦一点,但是更安全.

报错:error: failed to push some refs to "%your repository's git url%"如何解决
当要push代码到git时,出现提示:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/FDUAccessControlForBDA/LetUsFindIt.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.

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

  • 强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容
git push -f
  • 先把git的东西fetch到你本地然后merge后再push.
$ git fetch
$ git merge

这2句命令等价于

$ git pull origin master

但是origin和master是需要设置的.

$ git config branch.master.remote origin 
$ git config branch.master.merge refs/heads/master 

之后再重新git pull,同步之后再git push你的代码.

这时候又报错了:fatal: refusing to merge unrelated histories

先git pull,因为本地仓库和远程仓库中的内容不同,发现refusing to merge unrelated histories,无法git pull
因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在git pull,这句代码是在git 2.9.2版本发生的,最新的版本需要添加--allow-unrelated-histories

假如我们的源是origin,分支是master,那么我们 需要这样写git pull origin master --allow-unrelated-histories需要知道.


终于成功了!

参考文章

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. 安装 Github 查看是否安装git: $ git config --global user.name "...
    Albert_Sun阅读 13,935评论 9赞 163
  • 第一章 安装Git工具 下载GitHub for Windows,直接点击安装,安装完成后,可以看到“Git Sh...
    不圆的石头阅读 12,218评论 5赞 63
  • 呦呦呦呦呦 没想到我们俩吵来吵去吵了三百多天了 刚刚看纪念日 看了这个 还有点小惊讶呢 爸爸竟然跟你在一起快一年了...
    郝紫倩阅读 213评论 0赞 3
  • 一、基础介绍: 正向解析:通过域名查找IP;反向解析:通过IP查找域名; 一、相关配置文件详解 程序包:yum l...
    扎啤阅读 1,689评论 0赞 1
  • 第三十二章 嘿嘿嘿 “谁啊,大早上的就打电话。”原玥把自己乱糟糟的鸡窝头埋进了被窝里面,闷闷的声音从被窝的缝隙中间...
    chief风阅读 631评论 0赞 0

友情链接更多精彩内容