我是靠谱客的博主 受伤小鸭子,最近开发中收集的这篇文章主要介绍Git 安装使用 本地Git 服务器的使用 Github 远程仓库,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、Git 安装使用

1、 Git 安装配置

1、Yum 安装 Git
  • Centos/RedHat 安装命令
[root@localhost ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@localhost ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
# yum -y install git-core

[root@localhost ~]#  git --version
git version 1.8.3.1

2、源码安装
  • 最新源码包下载地址:https://git-scm.com/download

  • 安装系统的依赖包:

[root@qfedu.com ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
  • 解压下载的源码包:
[root@qfedu.com ~]# tar -zxf git-2.9.5.tar.xz
[root@qfedu.com ~]# cd git-2.9.5
[root@qfedu.com ~]# make prefix=/usr/local all
[root@qfedu.com ~]# make prefix=/usr/local install

2、Git 配置

1、Git 用户信息
  • 配置个人的用户名称和电子邮件地址:
[root@localhost ~]# git config --global user.name "liqiufeng"
[root@localhost ~]# git config --global user.email 1922857947@qq.com
2、文本编辑器
  • 设置Git默认使用的文本编辑器, 一般可能会是 Vi 或者 Vim。如果你有其他偏好,比如 Emacs 的话,可以重新设置::
[root@qfedu.com ~]# git config --global core.editor emacs
3、查看配置信息
  • 要检查已有的配置信息,可以使用 git config --list 命令:
[root@localhost ~]# git config --list
user.name=liqiufeng
user.email=1922857947@qq.com
core.editor=emacs

二、安装本地 Git 服务器

1、创建用户初始化仓库

[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
更改用户 git 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# mkdir /git-root/
[root@localhost ~]# cd /git-root/
[root@localhost git-root]# git init --bare shell.git
初始化空的 Git 版本库于 /git-root/shell.git/

2、给项目添加权限推送公钥

[root@localhost git-root]# chown -R git:git /git-root/
[root@localhost git-root]# ll
总用量 0
drwxr-xr-x 7 git git 119 9月  25 20:13 shell.git
[root@localhost git-root]# ssh-keygen
[root@localhost git-root]# ssh-copy-id git@192.168.122.11

3、拉取项目推送内容测试

[root@localhost git-root]# cd /opt/
[root@localhost opt]# ls
[root@localhost opt]# git clone git@192.168.122.11:/git-root/shell.git/
正克隆到 'shell'...
warning: 您似乎克隆了一个空版本库。
[root@localhost opt]# ls
shell
[root@localhost opt]# cd shell/
[root@localhost shell]# vim test.sh
[root@localhost shell]# git add .
[root@localhost shell]# git commit -m 'first commit'
[master(根提交) 3376488] first commit
 1 file changed, 2 insertions(+)
 create mode 100644 test.sh
[root@localhost shell]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 235 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.122.11:/git-root/shell.git/
 * [new branch]      master -> master

2、Git 使用

我们在客户机进行操作,首先安装git:

[root@node-1 ~]# yum -y install git-core
1、ssh 链接
  • 客户机上产生公钥上传到gitlab的SSH-Keys里,git clone下载和git push上传都没问题,这种方式很安全
2、 执行命令设置登录用户和密码
#cd到根目录,执行
[root@node-1 ~]# git config --global credential.helper store  # 执行之后会在.gitconfig文件中多添加以下选项
  [credential]         
  		helper = store
# cd到项目目录,执行git pull命令,会提示输入账号密码。输完这一次以后就不再需要,并且会在根目录生成一个.git-credentials文件
### 3、设置身份验证

- **注意:**设定本机用户名,绑定邮箱,让远程服务器知道机器的身份

```shell
[root@node-1 ~]#  git config --global user.name "user-1" 
[root@node-1 ~]# git config --global user.email "test@qq.com"

3、本地项目与远程服务器项目之间的交互

1、如果你没有最新的代码,希望从头开始
[root@node-1 ~]#  git clone git@192.168.122.11:/git-root/shell.git      # 这里是项目的地址(可从项目主页复制),将远程服务器的内容完全复制过来 
[root@node-1 ~]# cd shell                   # clone 之后进入该项目的文件夹 
[root@node-1 shell]# echo "hello world" > README.txt           # 新建readme文件 
[root@node-1 shell]# ls
README.txt  test.sh
[root@node-1 shell]# git add README.md          # 将新的文件添加到git的暂存区 
[root@node-1 shell]# git commit -m 'Its note:add a readme file'
[master 5609349] Its note:add a readme file
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt
 # 将暂存区的文件提交到某一个版本保存下来,并加上注释 
[root@qfedu.com ~]# git push -u origin master  
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 294 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.122.11:/git-root/shell.git
   3376488..5609349  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
# 将本地的更改提交到远程服务器
2、如果你已经有一个新版代码,希望直接把本地的代码替换到远程服务器
[root@node-1 ~]# cd existing_folder          #进入代码存在的文件夹,或者直接在该文件夹打开
[root@node-1 ~]# git bash git init           # 初始化 
[root@node-1 ~]# git remote add origin git@XXX.git  #添加远程项目地址(可从项目主页复制) 
[root@node-1 ~]# git add .                   #添加该文件夹中所有的文件到git的暂存区 
[root@node-1 ~]# git commit -m ‘note’        #提交所有代码到本机的版本库 
[root@node-1 ~]# git push -u origin master   #将本地的更改提交到远程服务器
  • git 中 clone过来的时候,git 不会对比本地和服务器的文件,也就不会有冲突,

  • 建议确定完全覆盖本地的时候用 clone,不确定会不会有冲突的时候用 git pull,将远程服务器的代码download下来

三、Github 远程仓库

1、github.com 注册账户

2、github 上创建仓库

3、本地服务器生成 ssh 公钥

[root@localhost ~]# ssh-keygen -t rsa -C '1922857947@qq.com'  # 邮箱要与github上注册的相同
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:RiE6UR1BtzV5avyE2uz6TNPsVHa2D2eHprghrJEkd/g meteor@163.com
The key's randomart image is:
+---[RSA 2048]----+
|    ..oo=o. o.   |
|     o ..o o...  |
|    o   . .. +   |
|     . o    = .  |
|    . + S  = o  =|
|     + *  . oo.=o|
|      o E ..o B.+|
|       o . =.* +o|
|      .   +++ . .|
+----[SHA256]-----+

[root@localhost ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVThfq4brrlsPGtAknVB0TLPx+7Dd3qlxTbSIrUOsGC5Y8JuNqVTlIntZB4oNj8cSQrWvec9CKm0a8o7WwaJIiqpxurz+YpQHP2KbapftKIxsX4hPf/z+p0El1U6arQa35/xmNsq+cJLH/bDdRG+EMDhuCBmjVZOlLj/hEdeIT6s56AnnCkaWoF+sq58KCF7Tk54jRbs/YiyE4SN7FuA70r+07sA/uj0+lmuk4E190KtQUELhjX/E9stivlqiRhxnKvVUqXDywsjfM8Rtvbi4Fg9R8Wt9fpd4QwnWksYUoR5qZJFYXO4hSZrUnSMruPK14xXjDJcFDcP2eHIzKgLD1 1922857947@qq.com

4、 github 添加 ssh 公钥

  • 复制以上的公钥,在 github 中添加ssh key

5、测试连接

[root@localhost ~]# yum install git
........
[root@localhost ~]# ssh -T git@qfedu.comhub.com
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi meteor! You've successfully authenticated, but GitHub does not provide shell access.

6、连接远程仓库(创建一个测试存储库)

# 在 github 网站新建一个仓库,命名为linux
~~~
[root@localhost ~]# cd /opt
[root@localhost ~]# mkdir linux
[root@localhost ~]# cd linux
~~~
# git 初始化,然后做第一个基本的git操作(需要在github上创建存储库)
[root@localhost linux]# git init
[root@localhost linux]# touch README
[root@localhost linux]# git add README
[root@localhost linux]# git commit -m 'first commit'
[root@localhost linux]# git remote add origin git@liqiufeng.comhub.com:userhub/linux.git
~~~
# 若出现origin已经存在的错误,删除origin
[root@qfedu.com linux]# git remote rm origin
# 现在继续执行push到远端
~~~
[root@localhost linux]# git remote add origin git@liqiufeng.comhub.com:userhub/l[root@localhost ~]inux.git
[root@localhost linux]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 205 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@liqiufeng.comhub.com:fakehydra/linux-.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
# 注意
# 设置存储库链接
[root@localhost ~]# git remote set-url origin git@liqiufeng.comhub.com:userhub/linux.git
# 如果 push 失败,合并分支到 master 再 push
[root@localhost~]# git pull --rebase origin master

最后

以上就是受伤小鸭子为你收集整理的Git 安装使用 本地Git 服务器的使用 Github 远程仓库的全部内容,希望文章能够帮你解决Git 安装使用 本地Git 服务器的使用 Github 远程仓库所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部