GitHub使用入門

今天花了一個下午的時間來玩GitHub,遇到一些問題,基本靠官方wiki和Google解決了。現在把過程記錄下來。

一、註冊一個GitHub賬號

地址:https://github.com/


二、新建一個倉庫

每次向GitHub提交的代碼都會被放到一個倉庫(repo)。爲了把你的項目放到GitHub上,你需要有一個GitHub倉庫來“入住”。

點擊新倉庫

184803281.png

在新的頁面裏填上倉庫的名稱(因爲已經創建過了,所以爲提示衝突):

184758220.png

點擊創建後就OK了!

184801312.png

三、安裝和配置git

使用yum安裝

yum -y install git

完成後查看是否成功

[root@localhost ~]# git --version
git version 1.7.1

可以看到安裝成功了,如果使用源碼可以安裝最新版本的。

接着就要設置用戶名和Email了,Email最好和註冊時候的一樣。

$ git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit
$ git config --global user.email "[email protected]"
# Sets the default email for git to use when you commit

上面的內容都寫在配置文件~/.gitconfig裏了

恭喜,到這裏,基本Git和GitHub都配置好了!


四、創建和提交項目

這裏給官方提供的例子吧,Hello-World爲項目名稱。

$ mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World" in your user directory
$ cd ~/Hello-World
# Changes the current working directory to your newly created directory
$ git init
# Sets up the necessary Git files
# Initialized empty Git repository in /Users/you/Hello-World/.git/
$ touch README
# Creates a file called "README" in your Hello-World directory

如果已經有項目了,就只用切換到項目目錄,然後git init。

接着向Git提交修改

$ git add README
# Stages your README file, adding it to the list of files to be committed
$ git commit -m 'first commit'
# Commits your files, adding the message "first commit"

這裏所有的更改都只是在本地的。Push之後纔會提交到GitHub保存:

$ git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repository
$ git push origin master
# Sends your commits in the "master" branch to GitHub

成功之後就可以在網頁上看到添加的文件了:

Your README has been created


但是我這邊沒有這麼一帆風順,主要是在最後一步出了問題:

向GitHub提交時遇到403錯誤:

root@localhost InfoView4CentOS]# git remote add origin https://github.com/callmepeanut/InfoView4CentOS.git
[root@localhost InfoView4CentOS]# git push origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/callmepeanut/InfoView4CentOS.git/info/refs
fatal: HTTP request failed


根據《github初試-403錯誤》將地址由https改爲ssh方式

git remote set-url origin https://[email protected]/callmepeanut/InfoView4CentOS.git


Push之後出現新的錯

[root@localhost InfoView4CentOS]# git push origin master
(gnome-ssh-askpass:3422): Gtk-WARNING **: cannot open display:


Google了下,是SSH_ASKPASS這個環境變量出的問題,unset掉就OK了。

ho 'unset SSH_ASKPASS' >> ~/.bashrc && source ~/.bashrc


終於連接上了,輸入yes後居然又出現了問題

[root@localhost InfoView4CentOS]# git push origin master
The authenticity of host 'github.com (192.30.252.129)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly


繼續Google,按照《訪問[email protected]出現Permission denied (publickey).的解決辦法》成功得添加了SSH key,測試一下:

[root@localhost InfoView4CentOS]# ssh -T [email protected]
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
Hi callmepeanut! You've successfully authenticated, but GitHub does not provide shell access.

成功連接上了,繼續PUSH!沒想到還是未能成功:

[root@localhost InfoView4CentOS]# git push origin master
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
ERROR: Repository not found.
fatal: The remote end hung up unexpectedly

查看了官網的幫助文檔:https://help.github.com/articles/error-repository-not-found ,最大的可能是拼寫不正確,去主頁看了下,確實InfoView4CentOS的V小寫了,於是把原來的倉庫刪掉,重新建立了一個,接着PUSH:

[root@localhost InfoView4CentOS]# git push origin master
Counting objects: 6, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 1.32 KiB, done.
Total 6 (delta 0), reused 0 (delta 0)
To ssh://[email protected]/callmepeanut/InfoView4CentOS.git
 * [new branch]      master -> master

噢耶,成功了!


更詳細的使用方法請看官方文檔,寫的很詳細而且是中文的:http://git-scm.com/book/zh/

附:《如何高效利用GitHub》



.


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