安裝git
<pre>
$ git
The program 'git' is currently not installed. You can install it by typing:sudo apt-get install git
</pre>
上述表明沒有安裝git安裝 git:sudo apt-get install git
安裝完成后配置信息:
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
配置 SSH查看本地是否已經(jīng)生成了ssh key(包含了id_rsa
以及id_rsa.pub ~/.ssh
沒有就需要生成ssh key:
$ ssh-keygen -t rsa -C "youremail@example.com"
然后一直回車(默認是不要密碼的,需要則輸入密碼)查看是否配置成功:
$ cd ~/.ssh$ ls$ id_rsa id_rsa.pub
如果有id_rsa
和id_rsa.pub
兩個文件則生成了ssh key,然后通過
cat ~/.ssh/id_rsa.pub
查看公鑰內(nèi)容,然后添加到github上的SSH Keys 中;
創(chuàng)建本地版本倉庫1.在合適的位置創(chuàng)建一個空目錄
$ mkdir learngit
$ cd learngit$ pwd/Users/desk/learngit
也可以選擇一個已有的項目文件夾,cd
到當前文件夾下
2.通過git命令git init
把當前目錄變成git可以管理的倉庫:
$ git init
Initialized empty Git repository **in** /Users/desk/learngit/.git/
提交文件到倉庫
$ git add readme.txt
$ git commit -m "提交了一個文件"
添加全部:git add .
添加遠程倉庫1.先在github上創(chuàng)建一個倉庫并拷貝地址如:
git@github.com:MrXiaxia/test.git
2.然后關(guān)聯(lián)本地倉庫:
$ git remote add origin git@github.com:MrXiaxia/test.git
3.把本地庫的內(nèi)容推送到遠程庫:
$ git push -u origin master
4.首次提交推送需加上-u
,以后不用;
5.如出現(xiàn)以下錯誤:
git push -u origin masterTo git@github.com:MrXiaxia/test.git
! [rejected] master -> master (non-fast-forward)error: failed to push some refs to 'git@github.com:MrXiaxia/test.git'hint: Updates were rejected because the tip of your current branch is behindhint: its remote counterpart. Integrate the remote changes (e.g.hint: 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.
可以嘗試用:
$ git push --force origin master
常見錯誤(mac和windows)
如果出現(xiàn)以下錯誤:
提示出錯信息:fatal: remote origin already exists.
解決辦法如下:
- 1.先輸入
$ git remote rm origin
- 2.再輸入
$ git remote add origin git@github.com:MrXia/gitdemo.git
就不會報錯了! - 3.如果輸入
$ git remote rm origin
還是報錯的話,error: Could not remove config section 'remote.origin'. 我們需要修改gitconfig文件的內(nèi)容 - 4.找到你的github的安裝路徑,我的是C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc
- 5.找到一個名為gitconfig的文件,打開它把里面的[remote "origin"]那一行刪掉就好了!
如果輸入$ ssh -T git@github.com
出現(xiàn)錯誤提示:Permission denied (publickey).
因為新生成的key不能加入ssh就會導(dǎo)致連接不上github。
解決辦法如下:
1、先輸入$ ssh-agent
,再輸入$ ssh-add ~/.ssh/id_key
,這樣就可以了。
2、如果還是不行的話,輸入ssh-add ~/.ssh/id_key
命令后出現(xiàn)報錯Could not open a connection to your authentication agent.解決方法是key用Git Gui的ssh工具生成,這樣生成的時候key就直接保存在ssh中了,不需要再ssh-add命令加入了,其它的user,token等配置都用命令行來做。
3、最好檢查一下在你復(fù)制id_rsa.pub文件的內(nèi)容時有沒有產(chǎn)生多余的空格或空行,有些編輯器會幫你添加這些的。
如果輸入$ git push origin master提示出錯信息:
error:failed to push som refs to .......
解決辦法如下:
1、先輸入$ git pull origin master //先把遠程服務(wù)器github上面的文件拉下來
2、再輸入$ git push origin master
-
3、如果出現(xiàn)報錯 fatal:
Couldn't find remote ref master或者fatal: 'origin' does not appear to be a git repository 以及fatal: Could not read from remote repository.
則需要重新輸入$ git remote add origingit@github.com:djqiang/gitdemo.git