1.github創(chuàng)建new repository

- 輸入repository name
- 描述
- 初始化README.md文件
- 添加.gitignore,可以在下拉框中選擇你的項(xiàng)目類型,比如Java,則可以獲得針對(duì)性的忽略文件.
- 添加協(xié)議
- 創(chuàng)建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正在運(yùn)行
# start the ssh-agent in the background
eval $(ssh-agent -s)
Agent pid 59566
- 添加你的ssh私鑰到ssh-agent.
ssh-add ~/.ssh/id_rsa
3.上傳公鑰到github
進(jìn)入github官網(wǎng),依次點(diǎn)擊頭像,Setting,SSH and GPG keys,然后點(diǎn)擊New SSH key

輸入title和
.ssh/id_rsa.pub里面的值
點(diǎn)擊
Add SSH key即可,重新登錄github賬戶,查看ssh key是否添加成功
4.本地確認(rèn)ssh key是否上傳成功
驗(yàn)證是否成功,在git bash下輸入
$ ssh -T git@github.com

5.新建本地項(xiàng)目
mkdir testpush
cd testpush
6.初始化git
git init

7.設(shè)置全局變量
接下來我們要做的就是把本地倉庫傳到github上去,在此之前還需要設(shè)置username和email,因?yàn)間ithub每次commit都會(huì)記錄他們
$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"
8.提交上傳
比如我上傳一個(gè)當(dāng)前文件夾下的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
%%中為你的項(xiàng)目地址.
還有一點(diǎn)需要注意,我們的項(xiàng)目url應(yīng)該選擇https協(xié)議的,選擇git協(xié)議的會(huì)提示沒有權(quán)限的錯(cuò)誤.
當(dāng)然了https協(xié)議的url每次提交都需要輸入用戶名和密碼.
而git協(xié)議的不需要,如何設(shè)置為git協(xié)議,后面會(huì)講.
當(dāng)然了,還是建議使用https協(xié)議,雖然麻煩一點(diǎn),但是更安全.
報(bào)錯(cuò):error: failed to push some refs to "%your repository's git url%"如何解決
當(dāng)要push代碼到git時(shí),出現(xiàn)提示:
! [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)的出現(xiàn)原因在于:git倉庫中已經(jīng)有一部分代碼,所以它不允許你直接把你的代碼覆蓋上去。于是你有2個(gè)選擇方式:
- 強(qiáng)推,即利用強(qiáng)覆蓋方式用你本地的代碼替代git倉庫內(nèi)的內(nèi)容
git push -f
- 先把git的東西fetch到你本地然后merge后再push.
$ git fetch
$ git merge
這2句命令等價(jià)于
$ git pull origin master
但是origin和master是需要設(shè)置的.
$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master
之后再重新git pull,同步之后再git push你的代碼.
這時(shí)候又報(bào)錯(cuò)了:fatal: refusing to merge unrelated histories
先git pull,因?yàn)楸镜貍}庫和遠(yuǎn)程倉庫中的內(nèi)容不同,發(fā)現(xiàn)refusing to merge unrelated histories,無法git pull
因?yàn)樗麄兪莾蓚€(gè)不同的項(xiàng)目,要把兩個(gè)不同的項(xiàng)目合并,git需要添加一句代碼,在git pull,這句代碼是在git 2.9.2版本發(fā)生的,最新的版本需要添加--allow-unrelated-histories
假如我們的源是origin,分支是master,那么我們 需要這樣寫git pull origin master --allow-unrelated-histories需要知道.

終于成功了!