gitlab 搭建与使用

安装

安装过程可参考gitlab官网。

下面以cent os 为例,简单说明其安装过程。

  1. 安装和配置必须的packages

    On CentOS, the commands below will also open HTTP and SSH access in the system firewall.

    sudo yum install -y curl openssh-server openssh-clients cronie
    sudo lokkit -s http -s ssh

    If you would like outbound email support, install Postfix or configure an SMTP server.

  2. 安装gitlab package

    curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    sudo yum install -y gitlab-ce

    If you are not comfortable installing the repository through a piped script, you can find the entire script here and select and download the package manually and install using

    curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/6/gitlab-ce-XXX.rpm/download
    rpm -i gitlab-ce-XXX.rpm

    安装完成后如图所示:

    这里写图片描述

  3. 配置和启动GitLab

    sudo gitlab-ctl reconfigure

    Reconfiguring GitLab will take a couple of minutes, as components are set up and started. A log is displayed of all actions, which will include green and grey lines.

  4. 登录

    On your first visit, you’ll be redirected to a password reset screen to provide the password for the initial administrator account. Enter your desired password and you’ll be redirected back to the login screen.

    The default account’s username is root. Provide the password you created earlier and login. After login you can change the username if you wish.

    在浏览器中输入http://localhost 或者直接IP地址http://192.168.1.100

    第一次访问,会提示提示输入密码,此时的用户名是root。密码设置 完成后,自动跳转到登录界面。

    这里写图片描述

    输入用户名root和密码,即可看到主页:

    这里写图片描述

    也可以注册一个新用户,点击Register。

    这里写图片描述

    输入用户名,邮箱,密码,注册完成后,即可跳转到gitlab主页。

新建项目

点击主页”New Project”,输入名字,新建代码库,例如 yang_code。
完成后,就在gitlab上成功创建了一个项目。

本地配置

安装git

  • Linux

    对于Cent os or Federa,可以使用 yum安装

    $ yum install git

    基于Debian的Linux,可以使用

    $ sudo apt-get install git
  • Windows

    对于Windows, 安装包下载链接https://git-scm.com/download/win
    下载完成,安装即可。

配置

$ git config --global user.name "lanyang"
$ git config --global user.email "lanyang@gmail.com"

生成ssh key

$ ssh-keygen

一直回车,最后在用户目录(/home/lanyang or C:\Users\lanyang\)下会出现.ssh 目录,里面包括生成的公钥和秘钥:id_rsa、id_rsa.pub

将.ssh/id_rsa.pub的内容拷贝到gitlab settings中,如图所示:

这里写图片描述

ssh key添加完成后,拉取和推送代码就不需要输入密码,非常便捷。

本地与gitlab的协同

在gitlab上新建仓库库后,本地如何gitlab进行协同工作?

一般情况下,本地的工作方式有3种:

  1. 新建仓库

    本地为空,直接从远程仓库克隆。

    git clone http://localhost/lanyang/yang_code.git
    cd yang_code
    touch README.md
    git add README.md
    git commit -m "add README"
    git push -u origin master

    首先是克隆gitlab上的代码库到本地,然后在本地新建或修改文件,最后提交并推送到gitlab。

  2. 文件夹已存在

    如果文件夹已存在,需要将其内容推送到gitlab 远程仓库,本地操作如下:

    cd existing_folder
    git init
    git remote add origin http://localhost/lanyang/yang_code.git
    git add .
    git commit -m "Initial commit"
    git push -u origin master

    首先,将文件夹初始化为git 仓库,并将gitlab 仓库的远程地址添加到本地仓库,最后提交、推送代码到gitlab远程仓库。

  3. Git仓库已存在

    本地已经有一个Git仓库,需要将其推送到刚才创建的gitlab仓库中:

    cd existing_repo
    git remote add origin http://localhost/lanyang/yang_code.git
    git push -u origin --all
    git push -u origin –tags

    首先,将远程gitlab仓库地址添加到本地仓库中,然后将本地仓库的所有分支和tag推送过去。

以第一种情况为例:

  1. 克隆仓库

    $ git clone git@192.168.1.108:lanyang/yang_code.git
    Cloning into 'yang_code'...
    The authenticity of host '192.168.1.108 (192.168.1.108)' can't be established.
    RSA key fingerprint is SHA256:S54B50c3PkIM3JM7f0JqbF+R9GKJW7umzAp95bRNxzQ.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.1.108' (RSA) to the list of known hosts.
    warning: You appear to have cloned an empty repository.
    Checking connectivity... done.
  2. 添加内容

    $ cd yang_code/
    $ touch README.md
    
    $ ll
    total 0
    -rw-r--r-- 1 zhangyunyang 1049089 0 Aug 26 15:04 README.md
    
    $ echo "Hello World" > README.md
  3. 提交和推送

    $ git add README.md
    warning: LF will be replaced by CRLF in README.md.
    The file will have its original line endings in your working directory.
    
    $ git commit -m "add README"
    [master (root-commit) 3e81cf3] add README
    warning: LF will be replaced by CRLF in README.md.
    The file will have its original line endings in your working directory.
     1 file changed, 1 insertion(+)
     create mode 100644 README.md
    
    $ git push -u origin master
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 225 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To git@192.168.1.108:lanyang/yang_code.git
     * [new branch]      master -> master
    Branch master set up to track remote branch master from origin.
  4. 查看gitlab

    此时在gitlab 项目仓库中就可以看到刚才推送的内容
    http://192.168.1.108/lanyang/yang_code

    这里写图片描述

参考

https://about.gitlab.com/installation/

https://docs.gitlab.com/ce/install/installation.html

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值