1.github创建new repository
- 输入repository name
- 描述
- 初始化README.md文件
- 添加.gitignore,可以在下拉框中选择你的项目类型,比如Java,则可以获得针对性的忽略文件.
- 添加协议
- 创建repository
2.新建ssh key
- Open Git Bash
- 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.
- 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]*
- At the prompt, type a secure passphrase. For more information, see"Working with SSH key passphrases".
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
- 添加你的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
需要知道.
终于成功了!