Windows平台下Git服务器搭建

来源:互联网 发布:路径依赖的案例数据 编辑:程序博客网 时间:2024/05/21 09:17

前言:在windows平台下搭建git服务器需要下载两个软件,一个是git-1.9.5-preview20141217.exe,另一个是copssh_4.1.0_Installer.exe

 

我的Git安装目录:C:\ProgramFiles\Git

我的copssh安装目录:C\Program Files\ICW

下面图片中的安装目录不是我的,是我在别人的博客中盗的图。但是安装的界面都是一样的,我没有截屏,所以就只有用别人的图了。

一、Git的安装:

安装Msysgit

下一步


同意GNU协议


选择安装位置,下一步


选择TrueType  Front,下一步


不创建启动文件夹


默认Git Bash,就可以了


选择默认的Checkout Style


安装完成了


至此安装完成了。但是如果这里是要作为服务器使用,所以这里还需要配置Git.

生成你的Public Key(这个是官方文档)

当你安装git完成以后,单击桌面的git-bash.exe快捷键才能有下面的dos界面出现。

Generating SSH keys

SSH keys are a way to identify trusted computers,without involving passwords. The steps below will walk you through generatingan SSH key and adding the public key to your GitHub account.

We recommendthat you regularly review your SSH keys list and revoke any that haven't been used in a while.

Step1: Check for SSH keys

First, we needto check for existing SSH keys on your computer. Open the command line andenter:

ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

Check the directory listing to see if you already havea public SSH key. By default, the filenames of the public keys are one of thefollowing:

这里的~/.ssh是在C:\Documentsand Settings\Adminstrator\.ssh

·        id_dsa.pub

·        id_ecdsa.pub

·        id_ed25519.pub

·        id_rsa.pub

Step2: Generate a new SSH key

1.      With the command line still open, copy and paste the textbelow. Make sure you substitute in your GitHub email address.

2.  ssh-keygen –C “你的email地址 “ –t rsa (这个是我找到的另一个生成key的代码)
3.  # Creates a new ssh key, using the provided email as a label
4.  # Generating public/private rsa key pair.

5.      We strongly suggest keeping the default settings asthey are, so when you're prompted to "Enter a file in which to save thekey", just press Enter to continue.

6.  # Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

7.      You'll be asked to enter a passphrase.

8.  # Enter passphrase (empty for no passphrase): [Type a passphrase]
9.  # Enter same passphrase again: [Type passphrase again]

Tip: We strongly recommend a very good, secure passphrase.For more information, see "Working with SSH key passphrases".

10.  After you enter a passphrase, you'll be given thefingerprint, or id,of your SSH key. It will look something like this:

11.# Your identification has been saved in /Users/you/.ssh/id_rsa.
12.# Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
13.# The key fingerprint is:
14.# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

Step 3: Add your key to the ssh-agent

To configurethe ssh-agent program to use the SSH key you've generated:

1.      Ensure ssh-agent is enabled:

2.  # start the ssh-agent in the background
3.  eval `ssh-agent`  (是~键上的那个`,这里有一对反引号)
4.  # Agent pid 59566

5.      Add your generated SSH key to the ssh-agent:

6.  ssh-add ~/.ssh/id_rsa    

Step 4: Add your SSH key to your account(4,5步可以不看官方的,下面有详细介绍,这里如果你要把项目提交到github网站上可以做下面四五步)

To configure your GitHub account to use your SSH key:

1.      In your favorite text editor, open the ~/.ssh/id_rsa.pub file.

2.      Select the entire contents of the file and copy it toyour clipboard. Do not add any newlines or whitespace.

Warning: It's important to copy the key exactly withoutadding newlines or whitespace.

Add the copied key to GitHub:

1.      In the top right corner of any page, click .

2.      In the user settings sidebar, click SSH keys.

3.      Click Add SSHkey.

4.      In the Title field, add a descriptive label for thenew key. For example, if you're using a personal Mac, you might call this key"Personal MacBook Air".

5.      Paste your key into the "Key" field.

6.      Click Add key.

7.      Confirm the action by entering your GitHub password.

Step5: Test the connection

To make sure everything is working, you'll now try toSSH into . When you do this, you will be asked to authenticate this actionusing your password, which is the SSH key passphrase you created earlier.

1.      Open the command line and enter:

2.  ssh -T git@github.com
3.  # Attempts to ssh to GitHub

4.      You may see this warning:

5.  # The authenticity of host 'github.com (207.97.227.239)' can't be established.
6.  # RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
7.  # Are you sure you want to continue connecting (yes/no)?

Verify thefingerprint in the message you see matches the following message, then type yes:

# Hi username! You've successfully authenticated, but GitHub does not
# provide shell access.

8.      If the username in the message is yours, you'vesuccessfully set up your SSH key!

If you receivea message about "access denied," you can read these instructions for diagnosing the issue.

If you'reswitching from HTTPS to SSH, you'll now need to update your remote repositoryURLs. For more information, see Changing a remote's URL.

 

详细介绍:

设置Github帐号的PublicKey

如果你有一个github的账号的话,

登陆github.Com 网站,在SSH Public Keys 设置你的SSH Public Keys,这里的Key是在~/.ssh/id_rsa.pub 下是一个pub文件,可以用记事本打开。


创建一个数据仓库


设置相关的信息


设置你的EmailNickname(这里如果你是把windows作为服务器,这里必须要)

现在回到你的工作目录,可以选择设置你默认的emailnickname
输入命令

这个nickname,email都是可以随便配置的,但是将来在eclipse中提交项目需要这里的nicknameemail
git config –global user.email 你的email地址git config –global user.name "Arthur"
 
 

配置你的GithubUsernameToken(如果是不是将项目提交到github中,而是让自己电脑变为git服务器,那么此处可以省略。)

设置你的github 用户名(这个用户名是你在github注册的用户名)

git config –global github.user caijiamx  

设置你的git账户的token这一步可以省略,其实我这里也没感觉这里生成了token有什么作用,参看了其他人写的配置git中没有关于设置token这一项。

Git config –global github.token (your token)

这个tokengithub,comAccount Settings同等级处找到token,如果没有就自己生成一个右侧找到。

 

/*这里是在github网站上建立一个远程的数据仓库,就是可以将你本地项目提交到github中,但是现在这里是将你的电脑变成git服务器,所以项目不是上传到github中,而是上传到你的电脑里,所以这里可以省略。

git remote add origin git@github.com:directoo/Magento-Theme.git

这个origin是一个数据仓库的名字,可以随便取,下面是你在“创建一个数据仓库

 

”这个步骤中创建的数据仓库的地址(可以是https://.......的,当然也可以是你windows的IP地址(git@github.com这一域名可改,后面的directoo/Ma...这些是库文件所在位置,配合下面在本地创建仓库可以用于远程提交。))

提交你的更改到服务器,输入命令

git push origin master

*/

 

二、安装copssh

      这里对于copssh的安装应该算是很简单的,下面我还是找到了一份安装介绍,我就是按照这里来安装的:

1.   双击Copssh_4.0.4_Installer.exe,出现如下画面;


2.   选择next按钮,进入License Agreement页面后选择next按钮,进入Choose Install Location页面,选择安装路径;


3.   选择next按钮,进入Service Account页面,使用默认帐户及密码;


4.   选择Install按钮,开始安装;


5.   安装过程中将弹出一个确认框,直接选择确认按钮:


6.   等待安装结束;

7.   7


选择Close按钮,完毕。

 

三、copssh配置:

从开始菜单中找到安装好的copssh文件夹,打开其中的COPSSHControl Panel

切换到Users选项卡。若Activatedusers下为空,则点击旁边的“Add”按钮。

这里Add添加的是windows下的用户,所以你可以在windows下建立用户。(我不知道与前面在git中设置的username有没有关联,反正我是把两个用户名设一样了)

       windows下建立用户只需要在控制面板中的用户账户中添加。

在弹出的窗口中点击“Forward”,进入下一步,接着,在User中选择一个用户后,点击“Forward”,接着把options的第一个选项“Allow password authentication”的勾去掉,因为我们要用公私钥来登陆,不用密码登陆。接着点击“Forward”完成用户添加。添加后应该如下图。


 

接着,点击右下方的“Keys”按钮,弹出窗口。这里显示的就是ssh服务器中保存的公钥信息了。若有公钥信息,用户就可以使用对应的私钥来远程登陆到ssh服务器进行相应的操作了。第一次我们安装的时候,public key里应该是空的,我们现在要做的,就是把上一步中,我们自己生产的公钥的信息,添加到这里来,步骤如下。

找到COPSSH的安装目录,然后打开其中的home文件夹,可以看到里面有以我们刚加入的user的名字命名的文件夹,进入其中,打开.ssh文件夹。在里面新建一个名字为authorized_keys的文件。注意,这里没有任何后缀。然后用记事本打开,然后将刚才我们配置git时产生的公钥里的全部信息复制拷贝到这个文件里(就是那个XXX.pub可以用记事本打开的文件)。然后,保存退出。

       当然你可以点击上图的keys按钮,然后出现下图的界面,然后点击Add按钮,然后在界面中直接添加,添加完后会自动生成一个authorized_keys文件。

回到COPSSHControlPanel中,将点击“Keys”产生的窗口关闭后,再打开,会看到其中有了一个公钥信息了,如下图。



到了这里,我们已经可以使用我们的私钥,来登陆这个ssh服务器了。打开“GitBash”。在命令行里输入

ssh ccf@192.168.1.100

ccf就是在COPSSH中配置的用户名(就是你Windows添加到这里的用户名),192.168.1.100是我的ip地址,这些都根据你自己的设置,配置成自己的信息。登陆成功后如下图。



接下来你会发现,登陆成功以后,可以使用linux的命令,但是git的命令还是不能使用,别急,接下来我们解决这个问题。

我使用的解决方法很简单,如下。

 $ Git\libexec\git-core目录下的git.exe , git-receive-pack.exe ,git-upload-archive.exe , git-upload-pack.exe复制到$ICW\bin目录下

$Git\bin目录下的libiconv-2.dll复制到$ICW\bin目录下

$Git下的share目录整个拷贝到$ICW下。

其中,$Git是指msysgit的安装目录  ,同理$ICWCOPSSH的安装目录。

 

至此,配置完毕,接下来,开始使用。

 

3git服务器的测试

打开“Git Bash”,使用命令

ssh ccf@192.168.1.100

连接到ssh服务器。这个命令上面有说明。

4.创建本地数据仓库(用于让项目提交进来的。如果是创建远程仓库,看前面介绍。)

然后依次使用下列命令

mkdir test.git  à会在copssh安装目录的home文件夹下的用户目录下又一个test.git目录

cd test.git

git init --bare

新建一个名为test.git的文件,并在其中建立一个纯git仓库。

然后使用命令exit退出ssh服务器

然后依次输入下面命令:

cd d:

mkdir test

cd test

git clone ccf@192.168.1.100:test.git. 

d盘下新建一个test文件夹并从服务器clone一个git仓库。

接着依次输入下列命令(我下面所做的修改是在cd到这个test.git仓库中做的,然后才能git status git add

touch a b c

git status

git add .  àadd后面还有一个点

git commit -m "just fortest"

git push origin master

一切正常,也能push到服务器上,至此,git服务器搭建并测试完毕。

 

四、在eclipse中安装egit

Eclipse上安装GIT插件EGit

 

      Eclipse的版本eclipse-java-helios-SR2-win32.zip(在Eclipse3.3版本找不到对应的 EGit插件,无法安装)

 

     

 


 

 

EGit插件地址:http://download.eclipse.org/egit/updates

 

OK,随后连续下一步默认安装就可以,安装后进行重启Eclipse

 

二、在Eclipse中配置EGit

 

准备工作:需要在https://github.com 上注册账号

 

Preferences > Team > Git > Configuration

 

 

这里的user.name 是你在https://github.com上注册用户名(如果是windows服务器,那么这里就是你在git配置时,这里设置的name,和email
git config –global user.email 你的email地址git config –global user.name "Arthur"

 

 

user.email是你在github上绑定的邮箱。在这里配置user.name即可

 

 

三、新建项目,并将代码提交到本地的GIT仓库中

 

1、新建项目 git_demo,并新建HelloWorld.java类

 

 

2、将git_demo项目提交到本地仓库,如下图

 

 



 

 

到此步,就成功创建GIT仓库。但文件夹处于untracked状态(文件夹中的符号”?”表示),下面我们需要提交代码到本地仓库(这里这个勾选我无法勾选,但是也可以进行下一步,并且可以提交),如下图

 

 

 



 

 

 

 

OK,这样代码提交到了本地仓库

 

 

四:将本地代码提交到远程的GIT仓库中

 

准备工作:在https://github.com上创建仓库

 

 

 

 

 

 

点击“Create repository” ,ok,这样在github上的仓库就创建好了。

 

注意创建好远程仓库后,点击进去,此时可以看到一个HTTP地址,如红线框,这个是你http协议的远程仓库地址(也就是仓库的地址,可以在浏览器网址中看到)


 

准备工作做好了,那开始将代码提交到远程仓库吧



       这里是提交到github网站上,那么URI就是你的github仓库所在的地址,如果是你的windows服务器上,那么提交的URI就是

此时的ssh访问地址如下,

分别由协议名称、用户名(你在copssh中设置的)、IP、端口、git仓库目录组成。

ssh://root@192.168.1.101:22/git/git.git


 

mkdir test.git

cd test.git

git init --bare

下面以我本地创建的这个仓库为例:(远程提交项目到这个windows服务器中)

ssh://ivanking@192.168.0.116:22/C:\Program Files\ICW\home\ivan_king\test.git(

C:\Program Files\ICW\home\ivan_king\test.gità这个是我建立的test.git仓库文件,eclipse中提交的时候要写全路径。)

 

 

 在上图的destination ref的框中是在git服务器中的位置,如果已经存在了master文件,那么这里就要修改为master01….其他等。

 


 

 

 

上面选中的那里必须是new branch 否则back,然后修改destination处的提交文件名

 

OK,这样提交远程GIT就完成了,可以在https://github.com核对一下代码是否已经提交

 

 

注意的问题

  如果是首次提交会第一步:先在本地建立一个一样的仓库,称本地仓库。

         第二步:在本地进行commit操作将把更新提交到本地仓库;

         第三步:将服务器端的更新pull到本地仓库进行合并,最后将合并好的本地仓库push到服务器端,这样就进行了一次远程提交。

 

  如果非首次提交同样的道理

          第一步:将修改的代码commit操作更新到本地仓库;

         第二步:第三步:将服务器端的更新pull到本地仓库进行合并,最后将合并好的本地仓库push到服务器端

 

eclipse从git服务器clone项目下来,

首先必须保证本地工作路径没有这个同名的项目,

1.import中选择 Git目录 Projects formGit

 

2.Clone URI

 

3.在URI中输入ssh://ivan_king(windows中的连接用户名)@IP:ssh端口/仓库路径

(ssh://ivan_king@192.168.0.116:22/C:\ProgramFiles\ICW\home\ivan_king\test.git)

如果ivan_king设置了密码,则在下面password处写上密码。

 

4.在next后,看到提交的destination处写的文件名,选择那个进行clone下来,然后选择一个路径来保存这个项目,

 

随便选个路径就可(

1. 我估计这里是要创建一个本地的仓库,然后以后提交的时候才可以提交上去,然后push到git服务器中

2. 或者是把这个路径作为eclipse这个项目的workspace,我觉得是这种,因为我在eclipse原workspace中没有看到这个项目,反而在这个路径下有这个项目。),反正最后还是要选择项目,然后项目会加载到eclipse中


PS:本博客参考自以下文档:

http://blog.csdn.net/ccf0703/article/details/7578276/

http://jingyan.baidu.com/article/3a2f7c2e16ca2a26afd611e2.html

http://blog.163.com/clevertanglei900@126/blog/static/11135225920101166432957/

http://jerry-chen.iteye.com/blog/1694947

http://jerry-chen.iteye.com/blog/1694947

http://yufenfei.iteye.com/blog/1750124

http://blog.csdn.net/liuzhushiqiang/article/details/13201259

http://www.cnblogs.com/hnrainll/archive/2013/03/23/2976797.html

http://blog.163.com/zlj_bj/blog/static/61468251201312011415683/

http://www.xbc.me/install-git-on-windows/

https://help.github.com/articles/generating-ssh-keys/

 http://blog.chinaunix.net/uid-22355887-id-3342499.html



0 0