linux下安装git、git命令汇总

前言

环境:centos7.9、git version 2.43.0
git官网:https://git-scm.com/

安装git

# 使用yum安装的git版本是1.8版本的,较为旧
yum install git -y

# 如果需要安装新的git版本,可以去官网下载编译安装git(这里安装git2.43版本)
git的官网:https://git-scm.com/
# 下载git的源码包:https://mirrors.edge.kernel.org/pub/software/scm/git/
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.43.0.tar.gz
tar xf git-2.43.0.tar.gz && cd git-2.43.0
./configure --prefix=/usr/local/git
make && make install
/usr/local/git/bin/git --version
# 做软链接
ln -s /usr/local/git/bin/git  /usr/bin/git
#查看git的版本
git --version	

git全局个人身份设置

git是分布式版本控制系统,所以每个开发者都应该在自己的笔记本上安装git,然后在git上设置自己的个人身份信息。

# git config --global 进行全局设置,如果不需要全局设置,可以去掉global表示当前目录的git设置
#设置全局用户姓名
git config --global user.name "lisi"
#设置全局邮箱
git config --global user.emain "456789123@qq.com"
#enable ui颜色
git config --global color.ui true
#查看配置
git config --list
user.name=lisi
user.emain=456789123@qq.com
color.ui=true

git的三大核心框架

工作区域(Working Directory):就是开发者平时存放项目代码的目录;
暂存区域(Stage):用于临时存放开发者代码的改动,事实上它只是一个文件,保存即将提交的文件列表信息;
git仓库(Repository):就是安全存放数据的位置,git仓库有开发者提交的所有版本的数据代码,其中,HEAD 指向最新放入仓库的版本。

在这里插入图片描述
git的工作流程一般是:

1、在工作目录中添加、修改代码文件;
2、将需要进行版本管理的代码文件放入暂存区域;
3、将暂存区域的文件提交到 git仓库。

因此,git管理的文件有三种状态:已修改(modified)、已暂存(staged)和已提交(committed),依次对应上边的每一个流程。

查看git命令帮助

[root@dev-node02 RuoYi-App]# git --help
usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--config-env=<name>=<envvar>] <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone     克隆一个远程仓库到当前目录
   init      初始化仓库,创建一个空的Git存储库或重新初始化一个现有的Git存储库
work on the current change (see also: git help everyday)
   add       添加一个文件到暂存区
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       显示提交的logs
   show      Show various types of objects
   status    显示暂存区的状态
grow, mark and tweak your common history
   branch    列出, 创建, 或删除分支
   commit    提交工作区内容到git仓库
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    切换分支
   tag       创建, 列出, 删除或验证用GPG签名的标签对象
collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      推送本地仓库到远程仓库

初始化一个git仓库

# 先创建一个项目目录,进入项目目录
mkdir /root/test_project/	&& cd /root/test_project/	
# 初始化目录,即初始化git仓库,生成.git隐藏目录
git init
# git init初始化命令就是生成了.git目录,这个.git目录就是git仓库,以后在当前目录下做的所有操作就可以受到git版本控制了

添加文件到暂存区

# 进入项目目录
cd /root/test_project/	
# 添加或修改一个文件
echo "test" >> test.sh
#
# 当我们修改了一个文件,此时文件还未被git追踪其状态,需要使用git add命令把文件的修改状态存入暂存区。
[root@git test_project]# git status		#查看状态
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test.sh
nothing added to commit but untracked files present (use "git add" to track)
#解释:Untracked files 说明存在未跟踪的文件(下边红色的那个)
#所谓的“未跟踪”文件,是指那些新添加的并且未被加入到暂存区域或提交的文件。它们处于一个逍遥法外的状态,当你一旦将它们加入暂存区域或提交到 git仓库,它们就开始受到 git的“跟踪”了
[root@git test_project]# git add test.sh 					#用git add命令把文件提交暂存区
[root@git test_project]# git status         					#再次查看状态
# On branch master
#
# Initial commit
#
# Changes to be committed:										#意思是要提交的更改,等待提交
#   (use "gitrm --cached <file>..." to unstage)
#
#       new file:   test.sh									#暂存区已经有一个文件了
#

#git add命令把文件添加到暂存区
git add test.sh
#git add命令把文件添加到暂存区,星号匹配所有
git add *
#git add命令把文件添加到暂存区,--all表示所有,等价于*
git add --all

从暂存区中移出文件

# 当有一个文件已经使用git add命令提交到暂存区了
#此时可以使用git reset HEAD 命令将它移出暂存区
git reset HEAD dossier_4.sh

查看Git仓库的当前状态

#查看在上次提交之后是否有对文件进行再次修改
git status

提交暂存区全部文件到git仓库

git commit 是把暂存区的所有文件提交到git仓库,并不是指提交某个文件。

# 提交暂存区内的文件到git仓库,-m参数表示写的注释
[root@git test_project]# git commit -m "lisi add test.sh file"		
[master (root-commit) c2b3806] lisi add test.sh file
 1 file changed, 2 insertions(+)
 create mode 100644 test.sh
# 再次查看状态,提示暂存区没有要commit的,工作目录文件也没有编辑过
[root@git test_project]# git status				
# On branch master
nothing to commit, working directory clean

继续模拟版本2、版本3

#模拟版本2,模拟版本2为修改test_1.sh文件,新加一个test_2.sh文件
cd ~/test_project/
#修改了test_1.sh文件
echo "I am good test_1" >> test_1.sh
#新加test_2.sh文件
touch test_2.sh
echo "I am test_2" >> test_2.sh
#星号匹配所有(等价于--all),把所有文件提交暂存区
git add *
#git commit提交暂存区文件到本地git仓库
git commit -m "modified test_1.sh,add test_2.sh"	
#查看状态,三大区域已经一致
git status										

#继续模拟版本3,继续模拟版本3为新加一个test_3.sh文件
#新加一个test_3.sh文件并写入内容
touch test_3.sh && echo "I am test_3">> test_3.sh
#把test_3.sh提交到暂存区
git add test_3.sh 							
#把暂存区内容提交到git仓库
git commit -m "Add test_3.sh"
#查看状态,三大区域已经一致
git status

以上,我们就创建了3个版本,版本1为创建test_1.sh文件,版本2为修改test_1.sh文件,新加一个test_2.sh文件,模拟版本3为新加一个test_3.sh文件

查看提交的版本历史记录

git log 和git reflog 命令都可以查看git仓库中有哪些版本,但两者的区别在于git log只能看得到当前的版本,而git reflog可以看得到所有的版本,也就是说当你回退版本之后,git reflog可以查看得到回退之前的版本,所有一般使用git reflog即可。

#git log 查看git仓库里的当前版本			
git log
#git reflog 查看git仓库里的所有版本,包含回退的版本记录
git reflog

git reset --hard 回退到指定版本

#自由回退版本,先用git reflog 查看git仓库的版本号
git reset --hard 72cd563

#git reflog命令查看git仓库中有哪些版本,前面的字符就是版本号
[root@git test_project]# git reflog								
72cd563 HEAD@{0}: commit: Add test_3.sh
beff2ea HEAD@{1}: commit: modified test_1.sh,add test_2.sh
ae2c1b8 HEAD@{2}: commit (initial): Add test_1.sh

#回退到版本2,即修改test_1.sh文件,新加test_2.sh
[root@git test_project]# git reset --hard beff2ea
HEAD is now at beff2ea modified test_1.sh,add test_2.sh
#查看文件,已经没有了test_3.sh文件,说明
[root@git test_project]# ll
total 8
-rw-r--r-- 1 root root 35 Jan 31 17:56 test_1.sh
-rw-r--r-- 1 root root 15 Jan 31 17:56 test_2.sh
[root@git test_project]# cat test_1.sh 						#test_1.sh的最后一行也确实实在版本2时候添加的
I am test_1
I am good test_1
[root@git test_project]# git reset --hard ae2c1b8      			#回退到版本1
HEAD is now at ae2c1b8 Add test_1.sh
[root@git test_project]# ll
total 4
-rw-r--r-- 1 root root 15 Jan 31 18:38 test_1.sh					#已经回退到版本1
[root@git test_project]# cat test_1.sh 						#已经回退到版本1
I am test_1	
[root@git test_project]#
[root@git test_project]# git reflog								#git reflog命令可以看到你所有的版本已经回退版本的记录
ae2c1b8 HEAD@{0}: reset: moving to ae2c1b8
beff2ea HEAD@{1}: reset: moving to beff2ea
72cd563 HEAD@{2}: commit: Add test_3.sh
beff2ea HEAD@{3}: commit: modified test_1.sh,add test_2.sh
ae2c1b8 HEAD@{4}: commit (initial): Add test_1.sh
[root@git test_project]# git reset --hard 72cd563					#为了后面实验,现在还是回退到版本3吧
HEAD is now at 72cd563 Add test_3.sh
[root@git test_project]# ll 										#已经回退到版本3了
total 12
-rw-r--r-- 1 root root 35 Jan 31 18:44 test_1.sh
-rw-r--r-- 1 root root 15 Jan 31 18:44 test_2.sh
-rw-r--r-- 1 root root 15 Jan 31 18:44 test_3.sh
[root@git test_project]# 

撤销文件修改(文件未提交到暂存区)

开发工程师修改了某个文件,文件未提交到暂存区(如果已提交到暂存区可以从暂存区移出),需要撤销文件修改,可以通过删除文件内容达到恢复文件最开始状态,但如果是修改了很多,甚至连自己都不知道要删除哪些内容了,这时可以使用git checkout命令:

# 当有一个文件,你修改了很多内容,然后发现是错误的,现在需要还原,你当前可以一行一行的撤销你做的修改,
# 也可以直接使用最近一次提交中的文件来直接还原现在文件,这样当前文件内容就还原到最近一次提交的文件内容了
# 注意命令--后面是空格接文件名
git checkout -- test_3.sh		# 这是旧git版本使用的命令
git restore test_3.sh		# 这是新版本使用的命令

删除一个文件并提交到暂存区

[root@git test_project]# rm  -rf test_1.sh						#当文件不需要了直接rm -rf删除一个文件
[root@git test_project]# git status								#查看状态
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)	#这里其实已经提示你使用add或rm参数了
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    test_1.sh
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@git test_project]# 	gitrm 	test_1.sh					#提交暂存区
[root@git test_project]# 	git commit -m "delete test_1.sh"		#把暂存区内容commit提交git仓库

git分支管理

后面我们会讲到gitHub远程仓库,开发团队成员都需要把代码提交到远程仓库,那为什么需要分支?
设想一下,如果只有一个master主分支,每个团队成员都去远程仓库master分支克隆一份代码下载到自己电脑进行代码编写,如果成员A尚未完成代码编写,只开发了部分功能,但怕自己电脑出现问题导致开发代码丢失,所以A需要提交代码到远程仓库,难不成A直接把未完成的代码直接提交到远程的master主分支上?然后来了个新人F,F去下载未完成的master分支代码?
其实不是的,企业中,远程仓库一般会创建多个分支,至少有一个dev分支,A可以先提交代码到dev分支,新人F是从master分支克隆代码,其他人也是从master克隆分支,然后大家都提交到dev分支,最后由仓库管理员leader决定是否合并dev分支代码到master分支,master分支的代码才是最后向用户提供的软件代码,所以必须保证master分支的代码是最正确的。

#列出分支,只有一个master分支,星号表示当前也在master分支
[root@git test_project]# git branch						
* master
#
#创建一个dev分支
git branch dev
#切换到dev分支,切换分支使用git checkout 命令
git checkout dev
#查看分支,现在是在dev分支上
[root@git test_project]# git branch						
* dev
  master
# 在dev分支上创建文件并写入内容  
touch test_4.sh && echo "dev branch" >>test_4.sh 
# 查看状态
[root@git test_project]# git status						
# On branch dev												#现在添加的文件实在dev分支上
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test_4.sh
nothing added to commit but untracked files present (use "git add" to track)

#提交暂存区
git add test_4.sh
#提交仓库,注意提交到的是dev分支
git commit -m "add test_4.sh on dev branch"
[dev 00444bd] add test_4.sh on dev branch		# 日志也显示是dev分支
 1 file changed, 1 insertion(+)
 create mode 100644 test_4.sh
#切换到master分支
git checkout master
#没看到test_4.sh文件,因为master还没合并
[root@git test_project]# ll
-rw-r--r-- 1 root root 35 Jan 31 20:08 test_1.sh
-rw-r--r-- 1 root root 15 Jan 31 18:44 test_2.sh
-rw-r--r-- 1 root root 15 Jan 31 19:58 test_3.sh

# 得先切换到master分支,然后合并dev分支到master分支
[root@git test_project]# git merge dev
Updating 72cd563..00444bd
Fast-forward
 test_4.sh | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test_4.sh
[root@git test_project]# ll								#已经可以看到test_4.sh文件了
-rw-r--r-- 1 root root 35 Jan 31 20:08 test_1.sh
-rw-r--r-- 1 root root 15 Jan 31 18:44 test_2.sh
-rw-r--r-- 1 root root 15 Jan 31 19:58 test_3.sh
-rw-r--r-- 1 root root 11 Jan 31 21:25 test_4.sh
[root@git test_project]# git branch -d dev				#删除分支

git branch [-a]					#查看分支,-a表示查看全部的分支,包含远程分支
git branch name_dev				#创建分支
git checkout dev				#切换分支
git merge dev					#合并分支,当前在master分支下操作,合并dev分支的更改合并到master分支
git branch -d dev				#删除分支

远程分支

克隆

总结

1、git的安装
[root@git~]# yum install git										#git的安装,直接使用yum安装
[root@git~]# git --version											#查看git的版本
gitversion 1.8.3.1
2、创建项目目录并初始化
[root@git~]# mkdir test_project									#创建一个项目代码目录
[root@git~]# cd test_project/									#进入目录
[root@git test_project]# git init									#初始化目录,让目录受到git版本控制
[root@git test_project]# ll -a 									#.git目录就是git仓库
drwxr-xr-x  3 root root  78 Jan 31 18:04 .
dr-xr-x---. 5 root root 215 Jan 31 17:52 ..
drwxr-xr-x  8 root root 166 Jan 31 18:05 .git
[root@git test_project]# vim test.sh 						#创建一个代码文件并编辑保存代码
[root@git test_project]# git status 							#查看git状态
[root@git test_project]# git add test.sh						#git add命令把文件放入暂存区
[root@git test_project]# git add *								#git add命令把文件放入暂存区,星号匹配所有
[root@git test_project]# git add --all							#git add命令把文件放入暂存区,--all表示所有,等价于*
[root@git test_project]# git commit -m "Add test.sh"			#git commit表示提交暂存区全部文件到git仓库,-m参数接注释
[root@git test_project]# git log								#git log 查看git仓库里的当前版本
[root@git test_project]# git reflog 							#git reflog 查看git仓库里的所有版本,包含回退的版本记录
[root@git test_project]# git reset --hard 72cd563					#自由回退版本,先用git reflog 查看git仓库的版本号
[root@git test_project]# git checkout -- test_3.sh				#撤销文件修改,注意命令--后面是空格接文件名
[root@git test_project]# gitrm test_1.sh						#rm删除一个文件后,把删除的文件提交到暂存区,不是git add
[root@git test_project]# git branch								#查看分支
[root@git test_project]# git checkout dev							#切换分支
[root@git test_project]# git merge dev							#当前在master分支下操作,合并dev分支到master分支
[root@git test_project]# git branch -d dev						#删除分支

https://www.jianshu.com/p/e57a4a2cf077

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值