Git基础

Git简介:

Git 是目前世界上最先进的分布式版本控制系统。(作者:林纳斯 托瓦兹)

版本控制系统分类:

集中式:代码集中存储在中央服务器,开发者的客户端只有部分自己的代码,假如中央服务器出问题,会出现数据丢失。传统的版本控制系统:CVS SVN

分布式:每台服务器都拥有所有代码,任意一台服务器崩溃,从其他服务器复制过来就好。如:git

实践:

安装git(一般默认已经安装了)

[root@git ~]# yum -y install epel-release.noarch 
[root@git ~]# yum -y install git

声明自己的命令和邮箱

[root@git ~]# git config --global user.name "qin"
[root@git ~]# git config --global user.email "xxxxxxxxxx@163.com"

创建版本库目录

[root@git ~]# mkdir /data
[root@git ~]# cd /data/
[root@git data]# git init  //初始化当前目录为版本库
Initialized empty Git repository in /data/.git/
[root@git data]# ls -a   //生成后会在当前目录生成.git目录(隐藏文件)
.  ..  .git
[root@git data]# cd .git/
branches  config  description  HEAD  hooks  info  objects  refs

branches :分支目录
config :定义目录特有的配置选项
description: 仅供git web使用
HEAD :指定当前的分支
hooks :git钩子文件
info : 包含一个全局排除文件(exclude)
objects : 存放所有的数据内容
refs :指针文件
index :暂存区文件


上传文件到版本库
1.上传文件到暂存区

[root@git ~]# echo "111111" > readme.txt
[root@git data]# git add readme.txt //上传到暂存区
[root@git data]# git status         //查看状态
[root@git data]# git rm --cached readme.txt  //从从暂存区删除
[root@git data]# git checkout -- readme.txt  //已经提交到暂存区,之后再修改内容出错,想回到上次暂存区版本(一般不会用到)

2.从暂存区上传到版本库

[root@git data]# git commit -m "add 1" //-m选项是给予上传文件的解释

修改readme.txt内容,使用diff命令查看异同

[root@git data]# echo "22222" >> readme.txt 
[root@git data]# git diff readme.txt

查看版本状态

[root@git data]# git reflog  //回滚动作命令日志
[root@git data]# git log --oneline  //查看历史记录显示摘要信息
[root@git data]# git log   //查看历史记录显示详细信息
[root@git data]# git status  //查看状态

版本回滚

[root@git data]# git reset --hard HEAD  //滚到上个版本,HEAD^^上上版本,HEAD~10回滚到上10个版本
HEAD is now at 260c094 add 2
[root@git data]# git reflog --oneline   //查看回滚的日志(查看回滚的id)
260c094 HEAD@{0}: reset: moving to HEAD^
540ea2f HEAD@{1}: commit: add 3
260c094 HEAD@{2}: commit (initial): add 2
[root@git data]# git reset --hard 260c094    //通过git log 的版本号码回滚,仅写前7位就可
HEAD is now at 260c094 add 2

git config 常用配置选项

git config -e 编辑配置文件
git config --local -e 编辑仓库级别配置文件
git config --global -e 编辑用户级别配置文件
git config --system -e 编辑系统级别配置文件

git config 添加别名

[root@git data]# git config --global -e
添加:
[alias]
        st = status
        co = checkout
        br = branch
        mg = merge
        ci = commit
        md = commit --amend
        dt = difftool
        mt = mergetool
        last = log -1 HEAD
        cf = config
        line = log --oneline

分支:

[root@git data]# git branch  //查看分支
* master
[root@git data]# git branch ops  //创建分支
[root@git data]# git checkout ops  //进入分支ops
Switched to branch 'ops'
[root@git data]# git checkout -b dev  //创建分支并进入
Switched to a new branch 'dev'
[root@git data]# git checkout master   //进入master分支(主分支)
Switched to branch 'master'

合并分支

[root@git data]# git checkout ops
Switched to branch 'ops'
[root@git data]# ls
readme.txt
[root@git data]# echo "eeeeeee" > test1.txt  //在ops分支创建新的文件
[root@git data]# git add test1.txt  
[root@git data]# git commit -m "add 33"  //添加到版本库
[ops 79c8ed5] add 33
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt


[root@git data]# git checkout master  //切换到主分支
Switched to branch 'master'
[root@git data]# git merge ops      //合并分支
Updating 260c094..79c8ed5  
Fast-forward
 test1.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt

[root@git data]# git log  //合并后查看在ops分支上的文件已经上传到版本库中了。
commit 79c8ed513850e547834ec2606275ad26cf5a7974
Author: qin <xxxxxxxxxx@163.com>
Date:   Fri Jul 17 05:22:37 2020 +0800

    add 33

删除分支

[root@git data]# git branch -d dev
Deleted branch dev (was 260c094).

分支合并冲突
制造冲突:先在分支修改文件,提交;然后回到master,再修改文件相应内容,提交;最后,合并分支,出现冲突。
删除冲突文件内容:带>>>>>>>>> ================= 行,保留想要的内容,再commit
一般不会出现,注意就好。

posted @ 2023-04-18 09:52  乱七八糟博客备份  阅读(13)  评论(0编辑  收藏  举报