当前位置: 代码迷 >> Eclipse >> Git/Github的使用并与Eclipse调整
  详细解决方案

Git/Github的使用并与Eclipse调整

热度:661   发布时间:2016-04-23 00:13:17.0
Git/Github的使用并与Eclipse整合

Git/Github的使用并与Eclipse整合

Git简介

? ? ? Git是一个免费的、分布式的版本控制工具,或是一个强调了速度快的源代码管理工具。每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,不依赖于网络和中心服务器。

? ? ? ?Git 在管理项目时,在本地会有三个工作区域:Git 的本地数据目录,工作目录以及暂存区域。如下图所示:

因此对于任何一个文件,在 Git 内都只有三种状态:已提交(committed),已修改(modified)和已暂存(staged)。

?

安装及配置Git

终端运行:
[plain]?view plaincopy
  1. sudo?apt-get?install?git?git-core??

首先去https://github.com/ 注册一个账户,当然是free and opensource的用户了。

?
根目录下创建git目录。
[plain]?view plaincopy
  1. mkdir?~/git??

?初始化两个参数:
[plain]?view plaincopy
  1. git?config?--global?user.name?=?"username"??
  2. ??
  3. git?config?--global?user.email?=?"your_email@example.com"??

因为本机是通过ssh链接github的,所以先创建ssh密钥。
?
看一下本机是否已经有ssh的密钥。
[plain]?view plaincopy
  1. cd?~/.ssh??

提示没有文件或者目录的话说明本机还没有创建过,继续执行
[plain]?view plaincopy
  1. ssh-keygen?-t?rsa?-C?"your_email@example.com"??

一路Enter,在根目录下面就会生产.ssh文件夹(隐藏,查看隐藏文件夹的话按Ctrl+H),里面有生产的密钥文件。
?
用gedit打开~/.ssh/id_rsa.pub,将文件内容拷贝到剪切板,(最好用gedit,其他的工具可能出现换行空格)
?
回到github的页面,点击Acount setting,
?
?
?
SSH Keys,
?
title随便取,key里面把之前复制的公钥的内容粘进去
?
?
Add
?
?
终端测试一下:
?
[plain]?view plaincopy
  1. ssh?-T?git@github.com??

成功连接的结果就想这样:
?
如果出现
[plain]?view plaincopy
  1. Agent?admitted?failure?to?sign?using?the?key.??
  2. Permission?denied?(publickey).??

执行
[plain]?view plaincopy
  1. ssh-add??
再进行连接就没问题了。
?

使用git

最简单的一种使用方式就是从github上下载别人的开源项目。
打开一个开源工程的页面
?
直接点击左上角的zip包就可以下载工程了。
更方便一些的做法是在终端运行命令来下载源码。
终端运行:
[plain]?view plaincopy
  1. git?clone?https://github.com/SimonVT/android-menudrawer.git??

会自动下载整个工程到当前的目录。
?
接下来要实现的是分享自己的项目到github上,并进行版本控制。
?
首先在github上面创建工程。
在个人主页上点击Responsitories选项卡的New
?
?
填写一些信息,点Create respository
?
github上的项目就创建好了。
?
接下来从服务器下载工程及配置文件到本地。
?
终端运行:
[plain]?view plaincopy
  1. mkdir?~/git/repos??
  2. cd?~/git/repos??
  3. git?clone?git@github.com:***/***.git??

执行完毕后,repos文件夹下就拷贝好了github上的项目。
?
本地进行项目编辑,比如添加一个文件之后,
可以通过下面的命令来更新项目了。
[plain]?view plaincopy
  1. git?add?.?//往暂存区域添加已添加和修改的文件,不处理删除的文件??
  2. git?status?//比较本地数据目录与暂存区域的变化??
  3. git?commit?-m?"commit?directions"?//提到代码到本地数据目录,并添加提交说明??
?
有可能你和其他人改的是同一个文件,那么冲突的情况是在所难免的,那么在提交之后再获取一下代码,就会提示代码冲突的文件,我们需要做的就是处理这些冲突,并再次提交:
[plain]?view plaincopy
  1. git?pull?????//更新代码??
  2. 根据提示修改冲突文件中的代码??
  3. git?add?.??
  4. git?commit?-m?"commit?directions"??


当做完以上的步骤的时候,你需要做的是把本地数据目录的版本库的数据同步到GitHub服务器上去
[plain]?view plaincopy
  1. git?push??
?
或者这样

Create a new repository on the command line

touch README.md

git init

git add README.md

git commit -m "first commit"

git remote add origin https://github.com/user_name/repository_name.git

git push -u origin master

?
?
提交完成之后刷新github的页面,发现文件已经更新好了。
?
?
可能遇到的问题
pull 报错
You asked me to pull without telling me which branch you?
want to merge with, and 'branch.master.merge' in?
your configuration file does not tell me, either. Please?
specify which branch you want to use on the command line and?
try again (e.g. 'git pull <repository> <refspec>').?
See git-pull(1) for details.?
?
解决方法:通过git config进行如下配置.
git remote add -f origin git@192.168.21.44:rest.git?
git config branch.master.remote origin?
git config branch.master.merge refs/heads/master
?
用meld来merge
在多人开发中,可能会发生代码冲突,meld是一个图形化代码比对工具,可以很方便的merge
sudo pat-get install meld
git config --global diff.external meld
?
每次都要输入用户名和密码
Linux 用户可以使用 ‘cache’ 认证助手包来缓存认证信息,运行下面的命令来启用凭据缓存,启用后每次输入密码将保存10小时(36000秒):
git config --global credential.helper 'cache --timeout 3600'

在Eclipse中整合git

首先安装git插件,在Eclispe中,
Help->Install New Software.
?
输入地址
http://download.eclipse.org/egit/updates
?
接着一路下一步,插件就安装好了。
?
在Eclipse中创建一个工程。
然后在工程上右击->Team->Share project->git->next.
?
?
选择Use or create repository in parent folder of project,如下图,点finish。
?
?
执行完成之后项目文件夹下就会创建好一个.git的文件夹,仓库就创建好了。
?
由于项目中的有一些文件没有必要上传,比如bin文件夹,需要在项目中设置好。
展开对应的项目,有小箭头的表示需要上传到服务器的目录,在不想要上传的目录上右击,Team->Ignoe,该目录就会被忽略了。
?
下面来commit代码到本机一下。
项目上右击Team->commit。
?
选中要commit的文件,点commit,完成之后,代码就上传到本地的服务器了。
?
接下来把代码上传到github的服务器。
?
命令行中进入到项目的文件夹,运行
[java]?view plaincopy
  1. git?remote?add?origin?https://github.com/SilangQuan/LinearCompiler.git??

再执行
[plain]?view plaincopy
  1. git?push?-f??
成功执行后就像这样:
?
查看github的项目主页,发现文件已经上传好。
?
之后在Eclipse中修改好项目代码之后,commit之后直接在项目上右击Team->push就可以进行代码提交。
?
?
上传到github上之后,团队中另外的成员可以通过Eclipse->File->Import->Project from Git->URI来提取工程。
?
在团队开发中,通常在新的一天的开始工作之前,把最新的代码fecth下来。
?
直接在项目想右击->Team->fetch
再执行->Team->Merge.
?
服务器上的新文件就会添加进来了。
?
?

参考:

?
git/github初级运用自如 -?http://www.cnblogs.com/fnng/archive/2012/01/07/2315685.html
?
Git 参考手册 -?http://gitref.cyj.me/zh/index.html

?git所需安装包下载 http://pan.baidu.com/s/1mgh0dRi

  相关解决方案