今天花了一个下午的时间来玩GitHub,遇到一些问题,基本靠官方wiki和Google解决了。现在把过程记录下来。
一、注册一个GitHub账号
二、新建一个仓库
每次向GitHub提交的代码都会被放到一个仓库(repo)。为了把你的项目放到GitHub上,你需要有一个GitHub仓库来“入住”。
点击新仓库
在新的页面里填上仓库的名称(因为已经创建过了,所以为提示冲突):
点击创建后就OK了!
三、安装和配置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 "your_email@example.com" # 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
成功之后就可以在网页上看到添加的文件了:
但是我这边没有这么一帆风顺,主要是在最后一步出了问题:
向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://callmepeanut@github.com/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,按照《访问git@github.com出现Permission denied (publickey).的解决办法》成功得添加了SSH key,测试一下:
[root@localhost InfoView4CentOS]# ssh -T git@github.com 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://git@github.com/callmepeanut/InfoView4CentOS.git * [new branch] master -> master
噢耶,成功了!
更详细的使用方法请看官方文档,写的很详细而且是中文的:http://git-scm.com/book/zh/
.