Git入门实验

本文是一篇关于Git的入门实验,介绍了如何在CentOS 7.3环境下通过Docker安装Git,讲解了Git的基本结构,包括工作区、暂存区、本地库和远程仓库。并详述了一个涉及多个用户的Git实验流程,包括创建用户、生成SSH密钥、克隆仓库、分支管理、合并冲突和打标签等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.git试验环境安装

操作系统centos 7.3,使用的docker 安装

docker run --name gitlab-postgresql -d \
  --env 'DB_NAME=gitlabhq_production' \
  --env 'DB_USER=gitlab' --env 'DB_PASS=password' \
  sameersbn/postgresql:9.4-12

docker run --name gitlab-redis -d sameersbn/redis:latest

docker run --name gitlab -d \
--link gitlab-postgresql:postgresql --link gitlab-redis:redisio \
--publish 10022:22 --publish 10080:80 \
--env 'GITLAB_PORT=10080' --env 'GITLAB_SSH_PORT=10022' \
--env 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' \
-v /_gitlab/data:/home/git/data \
-v /_gitlab/log:/home/git/log \
sameersbn/gitlab:8.4.4

2.git的基本结构

引用了网上的一张图,这张图清晰表达git的架构。workspace是工作区,可以用编辑器直接编辑其中的文件;Index/Stage是暂存区,编辑后的文件可以添加到(add)暂存区;Repository是本地库,暂存区的文件提交(commit)到本地库,本地库中文件可以检出(checkout)到工作空间;Remote是远程仓库,本地仓库的变化可以push到远程仓库,远程仓库用于多人共享。

3.git实验

1)建立1个组gitdemo, 4个用户,scm(配置管理)、ci01(持续集成)、dev01(开发人员1)和dev02(开发人员2)。

2)用ssh-keygen为每一用户生成ssh key,在gitlab上创建对应的4个用户,并配置ssh key 免密码登录。

3)scm用户登录gitlab,创建一个新的工程,创建完后,通过界面可查询到这个工程。gitlab是远程仓库。

4)scm用户登录操作系统,克隆远程仓库到本地仓库,因为在同一台机器上,用的是ssh://git@localhost:10022/GitDemo/gitflowdemo如果不在同一台机器上,localhost替换为实际的ip地址或域名。

 

[scm@gitserver myproject]$ git clone ssh://git@localhost:10022/GitDemo/gitflowdemo.git
正克隆到 'gitflowdemo'...
The authenticity of host '[localhost]:10022 ([::1]:10022)' can't be established.
ECDSA key fingerprint is 85:c8:4d:1d:a0:25:f5:a6:95:7e:68:0a:ed:04:50:98.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:10022' (ECDSA) to the list of known hosts.
warning: 您似乎克隆了一个空版本库。
[scm@gitserver myproject]$ ls
demo  gitflowdemo
[scm@gitserver myproject]$ cd gitflowdemo
[scm@gitserver gitflowdemo]$ ls
[scm@gitserver gitflowdemo]$ 

 编辑.gitignore

.gitignore
*.out
*.o
bin

编辑hello.c文件

#include<stdlib.h>
#include<stdio.h>
int main(int arg, char* argv[]){
    printf("inited by SCM.\n");
}

将工作区的文件添加到暂存区、添加到本地仓库、push到远程仓库。

[scm@gitserver gitflowdemo]$ git config --global user.name "Administrator"
[scm@gitserver gitflowdemo]$ git config --global user.email "admin@example.com"

[scm@gitserver gitflowdemo]$ git add .
[scm@gitserver gitflowdemo]$ git commit -m 'init'
[master(根提交) c4d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值