代码管理工具git gitlab

1:代码管理平台:代码版本管理工具,工作中用于代码更新,代码发布统计,代码版本控制;(频繁更新代码的版本记录),方便查阅历史变更,协同开发及合并;

版本管理工具发展:   cvs(编码兼容不太好)   ---->   svn    ----->   git  

svn(subversion),是一个开源版本控制系统,始于2000年;  C/S架构,要依赖于网络;

git是有linux创始人linus发起,目的用于更好的管理linux内核的代码;

git和svn的区别是:git是分布式,不需要依赖服务端就可以工作,

github是基于git的在线web页面的代码管理平台,可以选择付费服务;

gitlab可以认为是一个开源的github,两者没有直接关系;

2gitlab的使用

单机上使用git;git 时分布式的,分别再各自的电脑上使用,不需要把代码更新到服务器上去,仅仅再本地操作就可以了;

安装 git :   yum     install    -y   git

创建一个git目录,并进入到这个目录:   mkdir   -p  /data/gitroot ;   cd /data/gitroot

初始化 gitroot 目录 :  git   init

注释:初始化完成ad后,会再当前目录生成一个 .git 的隐藏目录;

[root@localhost_02 gitroot]# ls -la
drwxr-xr-x 7 root root 111 Apr  4 03:22 .git
[root@localhost_02 gitroot]# ls .git/
branches  config  description  HEAD  hooks  info  objects  refs

再当前库里面创建一个文件,然后并添加到仓库里去;

echo -e "123\n2342fsakdl;fds\sadfsdfsdf\sadf\sdf\sdf\\" > 1.txt

git  add   1.txt          把 1.txt 添加到仓库;

git  commit  -m "add   1.txt"      注释:上条命令add提交后,还需要使用commit命令才是正式提交到git仓库里;

[root@localhost_02 ~]# yum  install -y git
[root@localhost_02 ~]# mkdir -p /data/gitroot
[root@localhost_02 ~]# cd /data/gitroot/
[root@localhost_02 gitroot]# git init
初始化空的 Git 版本库于 /data/gitroot/.git/
[root@localhost_02 gitroot]# git add 1.txt
[root@localhost_02 gitroot]# git commit -m "add 1.txt"
[master(根提交) 90c3021] add 1.txt
 1 file changed, 5 insertions(+)
 create mode 100644 1.txt

2、 git   status   查看当前仓库的状态;比如是否改动过文件等;

[root@localhost_02 gitroot]# git commit -m "add 1.txt"
[master(根提交) 90c3021] add 1.txt
 1 file changed, 5 insertions(+)
 create mode 100644 1.txt
[root@localhost_02 gitroot]# git status          #第一次查看状态
# 位于分支 master
无文件要提交,干净的工作区
[root@localhost_02 gitroot]# vim 1.txt
[root@localhost_02 gitroot]# git status          #修改文件后不提交并再次查看状态
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#	修改:      1.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

注释:如上图例:如果修改了文件,但是没有使用git add 和git commit 提交,会提示你文件有改动,并且教你如何去变更;

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

当然,我们可以做一个对比的;  对比相比仓库的1.txt文件,本次修改了那些内容;     git   diff  1.txt

[root@localhost_02 gitroot]# git diff 1.txt
diff --git a/1.txt b/1.txt
index 9497e67..190a180 100644
--- a/1.txt
+++ b/1.txt
@@ -1,5 +1 @@
 123
-asdf
-f234
-234fjdsf
-23j4dsfsdfk2j323

3、版本的回退;需要依赖到 git  log 查看到的版本 ID 来回退了;

git  log  查看所有的提交记录;               git  log  --pertty=oneline  这个命令可用来一行显示      

注释:如下 git  commit  -m "里面是注释信息 文件名称"
[root@localhost_02 gitroot]# git add 1.txt
[root@localhost_02 gitroot]# git commit -m "fenye 1.txt agin"
[root@localhost_02 gitroot]# git log
commit 7c49e57c7dd633038a4a8e4590f77d3334869c19
Author: yuanhh <yuanhh104@163.com>
Date:   Thu Apr 4 03:54:54 2019 +0800
    add 1.txt agin
commit 90c3021d1ed26413158c3eec5960ababb62b0603
Author: yuanhh <yuanhh104@163.com>
Date:   Thu Apr 4 03:39:09 2019 +0800
   add 1.txt
[root@localhost_02 gitroot]# git log --pretty=oneline
7c49e57c7dd633038a4a8e4590f77d3334869c19 add 1.txt agin
90c3021d1ed26413158c3eec5960ababb62b0603 add 1.txt

注释:如上图例中的 author 作者 等信息是再/root/.gitconfig里定义的;

[root@localhost_02 gitroot]# cat /root/.gitconfig 
[user]
	email = yuanhh104@163.com
	name = yuanhh

版本回退; 最下面的最早的版本,依次往上;           git   reset   --hard   7c49e57c(版本ID的前几位)

[root@localhost_02 gitroot]# git log --pretty=oneline
6f96a3b7d4a5154dc299bb47ef324cc98278c73c fenye 1.txt             #最近修改的版本
7c49e57c7dd633038a4a8e4590f77d3334869c19 add 1.txt agin
90c3021d1ed26413158c3eec5960ababb62b0603 add 1.txt               #最早的版本

如果我们想要第一哥版本回退到第二个版本:     git    reset   --hard   7c49e57c                            #注意下面的提示

[root@localhost_02 gitroot]# git log --pretty=oneline
6f96a3b7d4a5154dc299bb47ef324cc98278c73c fenye 1.txt           #最近的版本
7c49e57c7dd633038a4a8e4590f77d3334869c19 add 1.txt agin
90c3021d1ed26413158c3eec5960ababb62b0603 add 1.txt             #最早的版本 
[root@localhost_02 gitroot]# git reset --hard 7c49e57c7
HEAD 现在位于 7c49e57 add 1.txt agin

撤销回退,如果有时候我们回退了后,才发现回退错了,那么可以撤销回退; 有如下两个方法:

如果我们还记得版本ID,可以通过这个命令:                    git     reset     --hard      版本ID

如果忘记了这个版本ID,可以通过 git   reflog 查看之前所对应的版本ID,然后再更改;      git   reset   --hard    版本ID

[root@localhost_02 gitroot]# git reset --hard 7c49e57c7
HEAD 现在位于 7c49e57 add 1.txt agin
[root@localhost_02 gitroot]# git  reflog
7c49e57 HEAD@{0}: reset: moving to 7c49e57c7
6f96a3b HEAD@{1}: commit: fenye 1.txt
7c49e57 HEAD@{2}: commit: add 1.txt agin
90c3021 HEAD@{3}: commit (initial): add 1.txt
#注释,第一列则为版本ID号;
[root@localhost_02 gitroot]# git reset --hard 6f96a3b
HEAD 现在位于 6f96a3b fenye 1.txt

如果不小心删除掉了/data/gitroot/下的文件1.txt,那么如何找回了;    get   checkout  -- 1.txt

[root@localhost_02 gitroot]# rm 1.txt
rm:是否删除普通文件 "1.txt"?y
[root@localhost_02 gitroot]# ls
[root@localhost_02 gitroot]# git checkout -- 1.txt
[root@localhost_02 gitroot]# ls
1.txt

如果文件 1.txt 的内容更改, add 后但是没有 commit,再想回退到上一次的状态,可以使用  git   reset   HEAD  1.txt   再执行  git  checkout  -- 1.txt

[root@localhost_02 gitroot]# vim 1.txt
[root@localhost_02 gitroot]# git add 1.txt
[root@localhost_02 gitroot]# git reset HEAD 1.txt        #撤销 git add 的操作;
重置后撤出暂存区的变更:
M	1.txt
[root@localhost_02 gitroot]# cat 1.txt
23j4dsfsdfk2j323
[root@localhost_02 gitroot]# git checkout -- 1.txt      #回退到这样的版本;
[root@localhost_02 gitroot]# cat 1.txt
123
asdf
f234
234fjdsf
23j4dsfsdfk2j323

git 删除一个文件:  也是 git  rm  1.txt 先删除后,然后再  git  commit  "rm  1.txt"  这样在库里才可以删除这个文件;

[root@localhost_02 gitroot]# git rm 1.txt
rm '1.txt'
[root@localhost_02 gitroot]# ls
[root@localhost_02 gitroot]# git commit -m "rm 1.txt"
[master a995b36] rm 1.txt
 1 file changed, 5 deletions(-)
 delete mode 100644 1.txt
[root@localhost_02 gitroot]# git checkout -- 1.txt
error: pathspec '1.txt' did not match any file(s) known to git.

注释:这时候使用 git  checkout -- 1.txt 已经无法恢复了;

但是呢,还可以使用 git log --pretty=oneline 来查看到它的版本ID,然后使用 git  reset  --hard  版本ID  来恢复文件;

[root@localhost_02 gitroot]# git log --pretty=oneline
a995b362d86357a2bd40278aae277be982750895 rm 1.txt
90c3021d1ed26413158c3eec5960ababb62b0603 add 1.txt
[root@localhost_02 gitroot]# git reset --hard 90c3021d
HEAD 现在位于 90c3021 add 1.txt
[root@localhost_02 gitroot]# ls
1.txt

 

转载于:https://my.oschina.net/yuanhaohao/blog/3031197

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值