Git相关安装与使用

Git

1.安装Git

准备两个虚拟机 hyperv_node1(当作git的client),hyperv_node2(当作git的server)
分别执行 yum -y install git-core gitweb 安装git

2.仓库创建

hyperv_node2上执行

#创建用户
[root@localhost ~]# useradd git 
#设置密码为git,看自己需要定密码成什么样
[root@localhost ~]# passwd git
更改用户 git 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
#新建目录
[root@localhost ~]# mkdir /git_root && cd /git_root
[root@localhost git_root]# git init --bare test_repo.git
初始化空的 Git 版本库于 /git_root/test_repo.git/
[root@localhost git_root]# ls
test_repo.git
[root@localhost git_root]# ls test_repo.git/
branches  config  description  HEAD  hooks  info  objects  refs

git init 和 git init --bare 的区别:
使用--bare选项时,不在生成.git目录,而只生成.git目录下的版本历史记录文件,这些版本历史记录文件也不再存在.git目录下面,而是直接存放在版本仓库的根目录下面。
用“git init”初始化的版本库用户也可以在该目录下执行所有git方面的操作。但别的用户在#将更新push上来的时候容易出现冲突。
使用“git init --bare”方法创建一个所谓的裸仓库,之所以叫裸仓库是因为这个仓库只保存#git历史提交的版本信息,而不允许用户在上面进行各种git操作,如果你要操作的话,只会得到“This operation must be run in a work tree”这个错误,这个就是最好把远程仓库初始化为bare仓库的原因。

修改权限,切到git用户,生成密钥

[root@localhost git_root]# chown -R git:git test_repo.git/
[root@localhost git_root]# su - git
[git@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa): 
Created directory '/home/git/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:s0SlIy8ql153pxv2FJjVQA/svqEUBLwFDN3/UFxEhfw git@localhost.HyperV-node2
The key's randomart image is:
+---[RSA 2048]----+
|      .=o+.o+o =*|
|        +o+ .==  |
|      . ++ o..o. |
|       +...++   E|
|      . S oo.o   |
|     o o o. o..  |
|  . + . o.+.oo   |
|   + . . o.*.    |
|    .     o..    |
+----[SHA256]-----+
[git@localhost ~]$ cd .ssh/
[git@localhost .ssh]$ ls
id_rsa  id_rsa.pub
[git@localhost .ssh]$ cp id_rsa.pub authorized_keys
[git@localhost .ssh]$ chmod 600 authorized_keys
[git@localhost .ssh]$ logout 
#修改git用户的shell环境
[root@localhost git_root]# usermod -s /usr/bin/git-shell git

hyperv_node1上执行

[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1Zhk///O6a/uZ+qVYqLSR4EEE1pq1e9xef3KZAy36VM root@localhost.HyperVNode1
The key's randomart image is:
+---[RSA 2048]----+
|       *+ o      |
|      = .= =     |
|     +  . * o . .|
|    .    o +.+...|
|        S . ++oo.|
|           o  *.E|
|        . .. B +o|
|       . ...o *o=|
|        ...  +BX*|
+----[SHA256]-----+

然后将生成的 /root/.ssh/id_rsa.pub 内容复制追加进 hyperv_node2 的 /home/git/.ssh/authorized_keys 文件

注意:为了安全考虑我们需要禁用ssh登录到我们的shell,防止别人登录到shell之后对我们的电脑做增删改
ssh服务器,默认ssh登录shell改为git-shell,这个git提供的shell程序,一旦登录会自动秒退

测试连接

[root@localhost ~]# ssh git@192.168.137.11
Last login: Mon Aug 21 14:19:03 2023 from 192.168.137.10
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.137.11 closed.

3. 仓库测试

hyperv_node1上执行

[root@localhost project]# git clone git@192.168.137.11:/git_root/test_repo.git
正克隆到 'test_repo'...
warning: 您似乎克隆了一个空版本库。
[root@localhost project]# cd test_repo/
#随便写入点内容
[root@localhost test_repo_1]# cat test.txt 
123
[root@localhost test_repo]# vim test.txt
[root@localhost test_repo]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#   test.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost test_repo]# git add test.txt 
[root@localhost test_repo]# git config --global user.email "lpb@qq.com"
[root@localhost test_repo]# git config --global user.name "lpb"
[root@localhost test_repo]# git commit -m "first commit"
[master(根提交) 5959bd9] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@localhost test_repo]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.137.11:/git_root/test_repo.git
 * [new branch]      master -> master
[root@localhost test_repo]# cd ..
[root@localhost project]# git clone git@192.168.137.11:/git_root/test_repo.git test_repo_1
正克隆到 'test_repo_1'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收对象中: 100% (3/3), done.
[root@localhost project]# ls
test_repo  test_repo_1
[root@localhost project]# cd test_repo_1/
[root@localhost test_repo_1]# ls
test.txt
[root@localhost test_repo_1]# cat test.txt 
123

4. Git命令行命令操作

4.1 安装配置

yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel git git-all git-core

4.2 配置

Git 提供了一个叫做 git config 的工具,专门用来配置或读取相应的工作环境变量。

这些环境变量,决定了 Git 在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:

  • /etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用 git config 时用 --system 选项,读写的就是这个文件。
  • ~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global 选项,读写的就是这个文件。
  • 当前项目的 Git 目录中的配置文件(也就是工作目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量

Git 用户信息
配置个人的用户名称和电子邮件地址:

[root@qfedu.com ~]# git config --global user.name "qfedu"
[root@qfedu.com ~]# git config --global user.email test@qq.com

如果用了 --global 选项,那么更改的配置文件就是位于你用户主目录下的那个,以后你所有的项目都会默认使用这里配置的用户信息。

如果要在某个特定的项目中使用其他名字或者电邮,只要去掉 --global 选项重新配置即可,新的设定保存在当前项目的 .git/config 文件里。

文本编辑器

设置Git默认使用的文本编辑器, 一般可能会是 Vi 或者 Vim。如果你有其他偏好,比如 Emacs 的话,可以重新设置

[root@qfedu.com ~]# git config --global core.editor emacs

差异分析工具

还有一个比较常用的是,在解决合并冲突时使用哪种差异分析工具。比如要改用 vimdiff 的话:

[root@qfedu.com ~]# git config --global merge.tool vimdiff

Git 可以理解 kdiff3,tkdiff,meld,xxdiff,emerge,vimdiff,gvimdiff,ecmerge,和 opendiff 等合并工具的输出信息。

当然,你也可以指定使用自己开发的工具

查看配置信息

要检查已有的配置信息,可以使用 git config --list 命令:

[root@qfedu.com ~]# git config --list
http.postbuffer=2M
user.name=runoob
user.email=test@runoob.com

有时候会看到重复的变量名,那就说明它们来自不同的配置文件(比如 /etc/gitconfig 和 ~/.gitconfig),不过最终 Git 实际采用的是最后一个。

这些配置我们也可以在 ~/.gitconfig/etc/gitconfig 看到,如下所示:

[root@qfedu.com ~]# vim ~/.gitconfig 

显示内容如下所示:

[http]
    postBuffer = 2M
[user]
    name = git
    email = test@qfedu.com.com

也可以直接查阅某个环境变量的设定,只要把特定的名字跟在后面即可,像这样:

[root@qfedu.com ~]# git config user.name
git
4.3 常用命令
4.3.1 ssh 链接

客户机上产生公钥上传到git的SSH-Keys里,git clone下载和git push上传都没问题,这种方式很安全

4.3.2 http 链接(比较少用,暂不说明了)
4.3.3 命令集
git init # 初始化 
git add main.cpp # 将某一个文件添加到暂存区 
git add . # 将文件夹下的所有的文件添加到暂存区 
git commit -m 'note' # 将暂存区中的文件保存成为某一个本 
git log # 查看所有的版本日志 
git status # 查看现在暂存区的状况
git diff # 查看现在文件与上一个提交-commit版本的区别
git reset --hard HEAD^ # 回到上一个版本 
git reset --hard XXXXX # XXX为版本编号,回到某一个版本 
git pull origin master # 从主分支pull到本地 
git push -u origin master # 从本地push到主分支 
git pull # pull默认主分支 
git push # push默认主分支 ...

# 版本回退
# 用 git log 命令查看:
# 每一个提交的版本都唯一对应一个 commit 版本号,
# 使用 git reset 命令退到上一个版本:
git reset --hard HEAD^
git reflog                    # 查看命令历史,以便确定要回到哪个版本
git reset --hard commit_id    # 比如git reset --hard 3628164(不用全部输入,输入前几位即可)

# 分支管理
# 1、创建分支
git checkout -b dev     #创建dev分支,然后切换到dev分支
git branch              #命令查看当前分支

# 2、分支切换
git checkout master     #切换回master分支

# 3、合并分支
git merge dev           #把dev分支的工作成果合并到master分支上

# 3、删除分支
git branch -d dev       #删除dev分支了
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容