[github] add existing project to github

Background : Add an existing local project to github repository.
添加本地项目到github上
Although the github’s helping article Adding an existing project to GitHub using the command line is very complete, but it existing alwarys some errors.
And I will try to add some chinese translation some command just for understanding.

So the mainly procedure is 主要过程:
1. Create a new repository on GitHub.
现在github上创建一个仓库,说白了就是创建一个项目。

2. Initialize the local directory as a Git repository.
初始化本地项目。

git init

3. Add the files in your new local repository.
添加项目到本地仓库里。在没跟github上项目合并之前,本地和远程的算是相对独立的。

git add .

4. Commit the files that you’ve staged in your local repository.
提交项目到本地仓库。

git commit -m 'The comment for your commit'

One thing you have to know here is that, commit is just record your local changes to the project, but the changes hasn’t been combined with your remote project.
就跟刚才强调的一样,我们只是把项目提交到了本地的仓库里,这个时候它还没有被合并到远程项目里,所以如果你试着刷新一下github上的页面,会发现什么都没变。

5. Get the remote repository URL. For me, this’s https://github.com/cupidcb/cuiborails.git.
添加远程URL。

6. In Terminal, add the URL for the remote repository where your local repostory will be pushed.
这里是用命令行指定所要推送的远程项目的地址,如果有同学喜欢图形化界面,应该可以google到,git现在已经提供图形化的界面了。

## This gonna to set the remote repository "origin" 
git remote add origin https://github.com/cupidcb/cuiborails.git 

## see your local
git remote -v
## I got this :
#origin	https://github.com/cupidcb/cuiborails.git (fetch)
#origin	https://github.com/cupidcb/cuiborails.git (push)

7. Push the changes in your local repository to GitHub.
这个才是真正重要的,把本地的变更推送到远程服务器上去。如果成功,刷新下github上的页面,就可以看到变化了,包括我们commit的注释,而且默认是public,大家都能看到哦!

git push origin master 

This command will finally combine your local change of your branch “master” with remote repository “origin”

For more details, you can find the commands in github

what I want to mention is the no-fast-forward error, When I tried the last step push :
刚才说如果成功,那其实是因为也有可能不成功,比如我,我第一次就没成,为啥捏,因为我手动在github项目上添加了两个readme文件,而github会自动检测,如果你不是第一个修改咱project的人,并且在你push之前有别人修改过project,那你就不能直接push。咱得先把修改的内容在本地做同样的修改,也就是fetch+merge或是直接pull,然后再push,就OK了。

git push origin master 

Screenshot from 2014-06-01 16:43:20
This is probably because there is another change made in remote repository that you haven’t checkouted and merged in your local branch before you make changes.
So what we have to do is :

## Fetches updates made to an online repository
git fetch origin
## Merges updates made online with your local work
git merge origin branch

Or to do it simply, we can just do a “pull” which will do a fetch and merge in the same time.

git pull origin branch

Find github’s Dealing with non-fast-forward errors.

Finally I found a nice picture better explain git workflow from here:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s