运维之道 | 通过 git 命令行从 github 或服务器上克隆、修改、删除和更新项目

一、安装 git

源码安装:

1、移除旧版本git
Centos自带Git,7.x版本自带git 1.8.3.1,安装新版本之前需要使用yum remove git卸载

[root@localhost ~]# git --version    ##查看git版本##
git version 1.8.3.1
[root@localhost ~]# yum remove git   ##卸载自带老版本git##

2、安装git的依赖包

[root@localhost ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

3、下载git源码

[root@localhost ~]# wget https://www.kernel.org/pub/software/scm/git/git-2.7.3.tar.gz

4、解压git源码

[root@localhost ~]# tar -xvf git-2.7.3.tar.gz

5、配置编译安装

[root@localhost ~]# cd git-2.7.3   ##进入到git-2.7.3安装目录##
[root@localhost git-2.7.3]# make prefix=/root/git all	  
[root@localhost git-2.7.3]# make prefix=/usr/local/git install 

6、配置环境变量(或者创建软连接)
a、查看git安装路径:whereis git

[root@localhost git-2.7.3]# whereis git
git: /usr/local/git /usr/local/git/bin/git

b、配置环境变量:vim /etc/profile

export PATH=/usr/local/git/bin:$PATH:$MAVEN_HOME/bin

c、加载该配置:source /etc/profile

[root@localhost git-2.7.3]# source /etc/profile

7、创建软连接(或者配置环境变量)

[root@localhost bin]# pwd
/bin
[root@localhost bin]# ln -s /usr/local/git/bin/git git

8、查看版本号:git --version

[root@localhost git-2.7.3]# git --version
git version 2.7.3

二、建立 git 仓库,并创建远程库

1、创建工作目录
[root@localhost ~]# mkdir huashan
[root@localhost ~]# cd huashan/
2、初始化工作目录
[root@localhost huashan]# git init
初始化空的 Git 版本库于 /root/huashan/.git/
3、创建远程库
  • 岳不群(老板)在Github创建华山(huashan)远程库
    在这里插入图片描述
4、把本地库pull推送到远程库
  • 获取远程库HTTPS地址
    在这里插入图片描述
5、 回到Linux端huashan本地库,绑定远程库HTTPS地址
[root@localhost huashan]# git remote add origin https://自己的仓库url地址
[root@localhost huashan]# git remote -v      ///查看当前所有HTTPS地址
origin	https://github.com/Yuebuqun-666/huashan.git (fetch) ///写回
origin	https://github.com/Yuebuqun-666/huashan.git (push)  ///推送

三、服务器上传项目

1、在工作区创建项目
[root@localhost huashan]# vim jianfa.txt

华山剑法、填下第一!!!
令说:令狐冲才是最厉害的!!!
令说:我会孤独剑法!!!

大家:真厉害
东方不败:是吗?那我们来试试!!
2、将项目添加到暂存区
[root@localhost huashan]# git add jianfa.txt
3、将项目从暂存区添加到本地仓库
[root@localhost huashan]# git commit -m "2020-02-03" jianfa.txt
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       huashan/
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
4、将项目从仓库推送到远程仓库
[root@localhost huashan]# git push origin master

Username for 'https://github.com': VillianTsang
Password for 'https://[email protected]': 
5、查看远程仓库是否上传成功

在这里插入图片描述

扫描二维码关注公众号,回复: 8985974 查看本文章

四、从远程库克隆项目

1、获取远程库项目URL

在这里插入图片描述

2、将本地的仓库关联到github上
[root@localhost huashan]# git remote add origin https://自己的仓库url地址
3、查看关联信息
[root@localhost huashan]# git remote -v
origin  https://github.com/VillianTsang/huashan.git (fetch)
origin  https://github.com/VillianTsang/huashan.git (push)
4、拉取git项目到本地
[root@localhost huashan]# git clone https://自己的仓库url地址
5、查看拉取项目
[root@localhost huashan]# ls
huashan.txt  jianfa.txt

五、更新项目,并再次推送至远程库

1、修改项目内容
[root@localhost huashan]# vim jianfa.txt

----------2020-02-03----------

华山剑法、填下第一!!!
令说:令狐冲才是最厉害的!!!
令说:我会孤独剑法!!!

大家:真厉害
东方不败:是吗?那我们来试试!!

----------2020-02-03----------
2、将项目推送至工作区
[root@localhost huashan]# git add jianfa.txt
3、将项目推送至暂存区
[root@localhost huashan]# git commit -m "2020-02-03-15:40" jianfa.txt
4、将项目推送至远程仓库
[root@localhost huashan]# git push origin master
Username for 'https://github.com': 账号用户名
Password for 'https://[email protected]':密码
5、查看远程仓库内容是否更新

在这里插入图片描述


六、删除远程仓库项目

1、查看github远程仓库所在项目

在这里插入图片描述

2、将github远程仓库项目pull拉取至本地仓库(或直接用git clone + url克隆至本地仓库 )
[root@localhost huashan]# git pull origin master

[root@localhost huashan]# ls
huashan.txt  jianfa.txt
3、删除指定项目文件
[root@localhost huashan]# git rm -rf --cached huashan.txt
rm 'huashan.txt'
4、提交删除说明
[root@localhost huashan]# git commit -m "2020-02-03-delete"
5、更新远程库
[root@localhost huashan]# git push -u origin master
Username for 'https://github.com': 用户名
Password for 'https://[email protected]':密码
6、查看远程库huashan.txt文件项目是否已删除

在这里插入图片描述

发布了118 篇原创文章 · 获赞 13 · 访问量 9429

猜你喜欢

转载自blog.csdn.net/VillianTsang/article/details/104156929