Jenkins Git构建基本远程代码库

Git名词解释


几个专用名词的译名如下: Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库

 一. 新建代码库

# 在当前目录新建一个Git代码库
$ git init

# 新建一个目录,将其初始化为Git代码库
$ git init [project-name]

# 下载一个项目和它的整个代码历史
$ git clone [url]

二. 增加/删除文件 

# 添加指定文件到暂存区
$ git add [file1] [file2] ...

# 添加指定目录到暂存区,包括子目录
$ git add [dir]

# 添加当前目录的所有文件到暂存区
$ git add .

# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
$ git add -p

# 删除工作区文件,并且将这次删除放入暂存区
$ git rm [file1] [file2] ...

# 停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]

# 改名文件,并且将这个改名放入暂存区
$ git mv [file-original] [file-renamed]

 三. 代码提交

# 提交暂存区到仓库区
$ git commit -m [message]

# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]

# 提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a

# 提交时显示所有diff信息
$ git commit -v

# 将add和commit合为一步
$ git commit -am 'message'

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m [message]

# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...

四.远程同步 

# 下载远程仓库的所有变动
$ git fetch [remote]

# 显示所有远程仓库
$ git remote -v

# 显示某个远程仓库的信息
$ git remote show [remote]

# 增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]

# 取回远程仓库的变化,并与本地分支合并
$ git pull [remote] [branch]

# 上传本地指定分支到远程仓库
$ git push [remote] [branch]

# 强行推送当前分支到远程仓库,即使有冲突
$ git push [remote] --force

# 推送所有分支到远程仓库
$ git push [remote] --all

 

为Jenkin构建远程代码仓库


(1)安装Git

[root@localhost ~]# yum install git -y

(2)创建Git用户并且设置密码

[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
Changing password for user git.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully

(3)创建仓库

[root@localhost ~]# su -git
su: group it does not exist
[root@localhost ~]# su - git
[git@localhost ~]$ mkdir repos
[git@localhost ~]$ cd repos/    #这个目录存放项目分支


[git@localhost repos]$ mkdir app.git  #这个是第一个目录
[git@localhost repos]$ cd app.git/

[git@localhost app.git]$ git --bare init  #初始化git项目仓库,如果不初始化,那么目录仅仅是目录而已,初始化之后目录里面就会提供仓库的一些信息了(在当前目录新建一个Git代码库)
Initialized empty Git repository in /home/git/repos/app.git/

[git@localhost app.git]$ ls   #这些信息就是用来支持代码版本控制的
branches  config  description  HEAD  hooks  info  objects  refs

好了之后找一个看看可不可以往该仓库里面提交代码。

[root@localhost ~]# yum install git -y
测试机安装git是为了测试从上面仓库拉取代码,主要充当客户端
[root@localhost test]# git clone git@192.168.179.104:/home/git/repos/app.git
Cloning into 'app'...
The authenticity of host '192.168.179.104 (192.168.179.104)' can't be established.
ECDSA key fingerprint is SHA256:222NBCna+TCSJ4bx8WE3Dd4SLhrrcrrfRweJn7Rmx0c.
ECDSA key fingerprint is MD5:e3:bb:90:13:6e:bd:21:3d:4a:aa:ea:0e:f8:32:9c:29.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.179.104' (ECDSA) to the list of known hosts.
git@192.168.179.104's password: 
warning: You appear to have cloned an empty repository.
[root@localhost test]# ls
App

提交到本地版本仓库当中,因为本地的也是版本控制仓库。先提交本地,然后从本地合并到git服务器上面。

[root@localhost app]# cd /root/test/app/
[root@localhost app]# ls
index.html

#添加到本地版本仓库当中
[root@localhost app]# git add .
[root@localhost app]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   index.html
#


#这就提交到了本地仓库
[root@localhost app]# git commit -m "first commit"
[master (root-commit) f86e528] first commit
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 index.html


[root@localhost app]# git status  #可以看到提交到本地仓库当中
# On branch master
nothing to commit, working directory clean

默认创建的分支为master分支,amster分支一般是主分支作为线上发布项目用的,其他下面的分支一般用于功能性开发,最后合并到主分支

#这样就推送到git服务器上面了
[root@localhost app]# git push origin master
git@192.168.179.104's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.179.104:/home/git/repos/app.git
 * [new branch]      master -> master

秒免密钥

#可以看到免密钥拉取代码
[root@localhost test]# ssh-copy-id -i ~/.ssh/id_rsa.pub "git@192.168.179.104"
[root@localhost ~]# mkdir -p test2
[root@localhost ~]# cd test2
[root@localhost test2]# git clone git@192.168.179.104:/home/git/repos/app.git
Cloning into 'app'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

 可以看到之前推送代码成功

[root@localhost test2]# ls app/ 
index.html

待会这些拉取操作都会在jenkins去做,简单的总结上面步骤如下

Git安装与使用

1、安装Git # yum install git
2、创建Git用户并设置密码
#useradd git 
#passwd git 
3、创建仓库
#su- git
# mkdir-r repos/app.git 
#cd repos/app.git 
# git --bare init 
4、配置sSH免密码认证
5、提交代码
#gite192.168.0.214:/home/git/repos/ap.git 
#git add.
#git commit -m "1" 
#git push origin master

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值