Git分布式文件管理工具与使用GitHub托管项目

jopen 10年前

Git是一个分布式的版本控制系统,GitHub可以托管各种git库,并提供一个web界面,但与其它像 SourceForge或Google Code这样的服务不同,GitHub的独特卖点在于从另外一个项目进行分支的简易性。为一个项目贡献代码非常简单︰首先点击项目站点的“fork”的按 钮,然后将代码检出并将修改加入到刚才分出的代码库中,最后通过内建的“pull request”机制向项目负责人申请代码合并。

1、下载Git客户端:
http://code.google.com/p/msysgit/downloads/list
2、使用Git客户端设置Git:

可以参考:https://help.github.com/articles/set-up-git

设置用户名:

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

设置Email,可以不用真实的Email,只是作为一个标示:

$ git config --global user.email "your_email@youremail.com" # Sets the default email for git to use when you commit

设置密码缓存时间:

$ git config --global credential.helper cache# Set git to use the credential memory cache $ git config --global credential.helper 'cache --timeout=3600'' # Set the cache to timeout after 1 hour (setting is in seconds)
3、在服务器上创建一个仓库:

可以参考:https://help.github.com/articles/create-a-repo

4、初始化本地仓库:
$ 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/
5、添加文件:
$ touch README# Creates a file called "README" in your Hello-World directory
6、提交文件到本地仓库:
$ 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"
7、生成SSH Key:

使用GitHub首先要创建SSH Key。SSH将用来加密本机与远端服务器之间的通信。同时也是识别你对代码所做的变更的方法。SSH Key可以使用Git命令行来产生。首先打开Git Bash命令行,输入:

ssh-keygen -C "username@email.com" -t rsa

说明:username@email.com 为你初始化Git的设置的Email地址。

之后Git Bash命令行中,会进行一些提示:

保存位置:注意rsa key pair要生成到root directory: ~/.ssh/目录下,如生成到C:\Users\arthinking\.ssh\id_rsa.pub。
pass phrase:如果本机安全,也可以不用输入。

找到生成的id_rsa.pub文件,把里面的内容复制到GitHub -> Account Settings -> SSH Keys –>Add SSH key的key输入框中,标题自定义,进行添加一个SSH Key。

8、把本地仓库提交到服务器:
$ git remote add origin git@github.com:username/Hello-World.git# Creates a remote named "origin" pointing at your GitHub repo $ git push origin master# Sends your commits in the "master" branch to GitHub

注意,这里的Hello-World.git是根据你在服务器上创建的仓库名称决定的,如果服务器创建了一个Itzhai仓库,则这里为Itzhai.git。