美文网首页
Git相关安装与使用

Git相关安装与使用

作者: 码农工号9527 | 来源:发表于2023-08-20 16:09 被阅读0次

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分支了

相关文章

  • Git 的基本使用

    Git相关命令 安装Git 在Windows上安装Git (我是使用的Windows)在Windows上使用Git...

  • 如何使用Git

    如何使用Git 安装Git 首先,计算机中应该先把Git安装好,才可以使用Git相关的命令。 谁能告诉我,Mark...

  • MAC上Git打标签

    MAC上Git安装与GitHub基本使用这篇文章介绍了Git在MAC上安装与使用。同大多数 VCS 一样,Git ...

  • Git 安装与使用 Day48 2019-01-07

    Git 安装与使用 一、基本操作 1. 下载安装 下载对应的Git版本安装 2. 配置Git 打开Git bash...

  • git - 分支、标签(四)

    分支 标签 相关链接:  git - 安装(一)  git - 创建版本库(二)  git - 代码的上传与拉取(...

  • Git 命令以及配置使用

    记录一些git相关的使用,首先去官网下载好Git并安装好https://git-scm.com/downloads...

  • ios之常用软件的安装与使用

    好,先为大家准备的干货,尽管拿去吧! git相关视频 Git 安装与简单配置-Git 本地操作-极客学院 git的...

  • Windows上安装和使用git到gitoschina和gith

    Windows上安装和使用git到gitoschina和github上_亲测 git介绍与在windows上安装 ...

  • Android Studio上传代码到GitHub

    上传代码前需要先安装Git客户端Git的安装与使用可以看我的另一篇博客:Git的使用教程需要先在Git中设置好用户...

  • Mac github的本地配置

    安装git 配置用户名和邮件 配置秘钥 done! 参考: MAC上Git安装与GitHub基本使用

网友评论

      本文标题:Git相关安装与使用

      本文链接:https://www.haomeiwen.com/subject/okpsmdtx.html