学习代码管理之Git的安装与使用

单机使用Git

一、安装和配置
安装Git

[root@localhost ~]# yum install -y git

配置基本信息

[root@localhost ~]# git config --global user.name "lsk"
[root@localhost ~]# git config --global user.email "examlple@qq.com"

创建目录进去

[root@localhost ~]# mkdir /data/gitroot
[root@localhost ~]# cd /data/gitroot

初始化仓库

[root@localhost gitroot]# git init
Initialized empty Git repository in /data/gitroot/.git/

二、使用
1、新建一个文件,然后把它添加到仓库,可以查看仓库状态,和如何修改了文件

在一个新的文件里添加内容
[root@localhost gitroot]# echo "hello world">1.txt
把文件添加到仓库里
[root@localhost gitroot]# git add 1.txt
[root@localhost gitroot]# ls -la
total 8
drwxr-xr-x. 3 root root   29 Nov  6 22:07 .
drwxr-xr-x. 4 root root   34 Nov  6 22:07 ..
-rw-r--r--. 1 root root   12 Nov  6 22:07 1.txt
drwxr-xr-x. 7 root root 4096 Nov  6 22:08 .git
必须使用commit才是真正的添加到仓库
[root@localhost gitroot]# git commit -m "add new file 1.txt"
[master (root-commit) 226fea5] add new file 1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt
看一下文件的内容
[root@localhost gitroot]# cat 1.txt 
hello world
追加新的内容
[root@localhost gitroot]# echo "new" >> 1.txt 
在查看下
[root@localhost gitroot]# cat 1.txt 
hello world
new
查看仓库的状态,例如是否有文件改动
[root@localhost gitroot]# git status 1.txt 
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   1.txt  ##  这里表示1.txt发生了变化
#
no changes added to commit (use "git add" and/or "git commit -a")
对比仓库里的版本,修改了什么内容
[root@localhost gitroot]# git diff 1.txt 
diff --git a/1.txt b/1.txt
index 3b18e51..3a4b9d0 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
 hello world
+new   ## 这里的+ 号代表刚刚新添加的

2、版本回退

查看所有提交记录
[root@localhost gitroot]# git log
commit 068c67a6a71f10fefa7e206bd8edc54cadb1528b
Author: lsk <examlple@qq.com>
Date:   Fri Nov 6 22:17:35 2020 +0800

    1.txt change 1

commit 226fea5d45c459c719b0bd686a2b890fbcfdafd2
Author: lsk <examlple@qq.com>
Date:   Fri Nov 6 22:14:07 2020 +0800

    add new file 1.txt
    显示的太多,可以简便点
[root@localhost gitroot]# git log --pretty=oneline
068c67a6a71f10fefa7e206bd8edc54cadb1528b 1.txt change 1
226fea5d45c459c719b0bd686a2b890fbcfdafd2 add new file 1.txt

删除后再回退到之前的版本

删除文件
[root@localhost gitroot]# rm -f 1.txt 
[root@localhost gitroot]# ls -la
total 4
drwxr-xr-x. 3 root root   17 Nov  6 22:30 .
drwxr-xr-x. 4 root root   34 Nov  6 22:07 ..
drwxr-xr-x. 8 root root 4096 Nov  6 22:26 .git
[root@localhost gitroot]# git log --pretty=oneline
068c67a6a71f10fefa7e206bd8edc54cadb1528b 1.txt change 1
226fea5d45c459c719b0bd686a2b890fbcfdafd2 add new file 1.txt
恢复1.txt
[root@localhost gitroot]# git checkout -- 1.txt
[root@localhost gitroot]# ls
1.txt

3、如果1.txt文件修改,add后但没有commit,再想回退到上一次提交的状态,可以使用git reset HEAD 1.txt,再执行git checkout – 1.txt。

新建一个文件并且添加内容
[root@localhost gitroot]# echo -e "222222/n4345444">3.txt
使用add提交,但是不用commit
[root@localhost gitroot]# git add 3.txt
在添加一些内容,对文件做出一些改变
[root@localhost gitroot]# echo "333">>3.txt
查看修改的内容
[root@localhost gitroot]# git diff 3.txt
diff --git a/3.txt b/3.txt
index cdab7a7..4d76a5d 100644
--- a/3.txt
+++ b/3.txt
@@ -1 +1,2 @@
 222222/n4345444
+333
并且查看修改后的
[root@localhost gitroot]# cat 3.txt 
222222/n4345444
333
因为没有使用commit提交,所以查找不出提交记录
[root@localhost gitroot]# git log --pretty=oneline
226fea5d45c459c719b0bd686a2b890fbcfdafd2 add new file 1.txt
恢复以前版本
[root@localhost gitroot]# git reset HEAD 3.txt
[root@localhost gitroot]# git log --pretty=oneline
226fea5d45c459c719b0bd686a2b890fbcfdafd2 add new file 1.txt
[root@localhost gitroot]# git checkout -- 1.txt
[root@localhost gitroot]# git checkout -- 3.txt
[root@localhost gitroot]# cat 3.txt 
222222/n4345444
[root@localhost gitroot]# 

4、删除文件

[root@localhost gitroot]# git rm 2.txt
rm '2.txt'

在这里插入图片描述
三、git分支
理解分支
https://www.cnblogs.com/matengfei123/p/8252128.html

1、分支的操作
查看分支(有*号的代表当前所在分支)
当前只有一个master分支

[root@localhost ~]# git branch
* master

创建分支
(查看命令加上要创建的分支的名字)

[root@localhost ~]# git branch lsk

切换分支

[root@localhost ~]# git checkout lsk
Switched to branch 'lsk'

编辑分支
(在这个分支下添加一个文件并且上传)

[root@localhost gitroot]# ll
total 4
-rw-r--r--. 1 root root 5 Nov  6 21:06 1.txt
[root@localhost gitroot]# echo "jfioh">2.txt
[root@localhost gitroot]# git add 2.txt
[root@localhost gitroot]# git commit -m "huif"
[lsk be50f41] huif
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
[root@localhost gitroot]# ll
total 8
-rw-r--r--. 1 root root 5 Nov  6 21:06 1.txt
-rw-r--r--. 1 root root 6 Nov  6 21:12 2.txt

例如,在刚刚在lsk分支新建并且上传的2.txt文件,在master分支是没有的

切换到master分支
[root@localhost gitroot]# git checkout "master"
Switched to branch 'master'

查看当前所在分支
[root@localhost gitroot]# git branch
  lsk
* master

此分支下没有2.txt文件
[root@localhost gitroot]# ll
total 4
-rw-r--r--. 1 root root 5 Nov  6 21:06 1.txt

切换到lsk分支
[root@localhost gitroot]# git checkout "lsk"
Switched to branch 'lsk'
[root@localhost gitroot]# git branch
* lsk
  master

此分支有2.txt文件
[root@localhost gitroot]# ll
total 8
-rw-r--r--. 1 root root 5 Nov  6 21:06 1.txt
-rw-r--r--. 1 root root 6 Nov  6 21:13 2.txt

2、分支合并

//合并分支之前,先切换到目标分支
[root@localhost gitroot]# git checkout master
Switched to branch 'master'
[root@localhost gitroot]# git branch
  lsk
* master
[root@localhost gitroot]# git merge lsk    //把lsk分支合并到了master
Updating 0c42a31..be50f41
Fast-forward
 2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
[root@localhost gitroot]# ll
total 8
-rw-r--r--. 1 root root 5 Nov  6 21:06 1.txt
-rw-r--r--. 1 root root 6 Nov  6 21:41 2.txt

如果master分支和lsk分支都对2.txt进行了编辑,当合并时会提示冲突,需先解决冲突才可以继续合并。
解决冲突的方法是在master分支下,编辑2.txt,改为lsk分支里面2.txt的内容。然后提交2.txt,再合并lsk分支。
但是这样有一个问题,万一master分支更改的内容是我们想要的呢?此时可以编辑2.txt内容,改为想要的,然后提交。切换到lsk分支,然后合并master分支到lsk分支即可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。
3、删除分支

[root@localhost gitroot]# git branch
  lsk
* master
[root@localhost gitroot]# git branch -d lsk   #删除分支
Deleted branch lsk (was be50f41).
[root@localhost gitroot]# git branch 
* master
[root@localhost gitroot]# git branch lsk
[root@localhost gitroot]# git branch -D lsk   # 强制删除
Deleted branch lsk (was be50f41).
[root@localhost gitroot]# git branch
* master

4、使用分支的原则
对于分支的应用,建议大家以这样的原则来:
① master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。
② 创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master。
③ 开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己PC上)里面开发代码,然后合并到dev分支。
在这里插入图片描述
dev分支合并bob分支的命令是:

 # git checkout dev      //先切换到dev分支,然后
Switched to branch 'dev'

 # git merge bob
Updating 89b88e8..ea53299
Fast-forward
 ll | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 ll

5、远程分支
本地新建的分支如果不推送到远程,对其他人就是不可见的。
查看远程分支命令为git ls-remote origin,可以看到所有分支。
在这里插入图片描述
对于git push分支分两种情况:
① 当本地分支和远程分支一致时
git push会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用git push origin branch-name命令。
② 当本地分支比远程分支多
默认git push只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name命令如果推送失败,先用git pull抓取远程的新提交。
在这里插入图片描述
git clone的时候默认只把master分支克隆下来。如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name命令,需要注意本地和远程分支的名称要一致。
在这里插入图片描述
四、Git标签与别名
1、标签

先切换到master分支上
[root@localhost gitroot]# git checkout master
Already on 'master'

给master打一个标签v1.0
[root@localhost gitroot]# git tag v1.0

查看标签信息
[root@localhost gitroot]# git show v1.0
commit be50f412d5f9b214d1e68ceebb7418dff89484d8
Author: lsk <example@qq.com>
Date:   Fri Nov 6 21:12:34 2020 +0800

    huif

diff --git a/2.txt b/2.txt
new file mode 100644
index 0000000..78e5b0f
--- /dev/null
+++ b/2.txt
@@ -0,0 +1 @@
+jfioh

可以查看所有的标签
[root@localhost gitroot]# git tag
v1.0

查看历史的commit
[root@localhost gitroot]# git log --pretty=oneline --abbrev-commit
be50f41 huif
0c42a31 add new 1.txt

针对历史commit打标签
[root@localhost gitroot]# git tag v0.9 be50f41

对标签进行描述
[root@localhost gitroot]# git tag -a v0.8 -m "tag just v1.1 and so on" 0c42a31
[root@localhost gitroot]# git tag
v0.8
v0.9
v1.0

删除标签
[root@localhost gitroot]# git tag -d v0.8
Deleted tag 'v0.8' (was dfc5061)
[root@localhost gitroot]# git tag
v0.9
v1.0

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2、别名
创建别名

[root@localhost gitroot]# git config --global alias.ci commit
[root@localhost gitroot]# git config --global alias.co checkout
[root@localhost gitroot]# git config --global alias.br branch

查看git别名使用命令

[root@localhost gitroot]# git config --list | grep alias
alias.ci=commit
alias.co=checkout
alias.br=branch

查看log小技巧

[root@localhost gitroot]# git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

取消别名

[root@localhost gitroot]# git config --global --unset alias.br
[root@localhost gitroot]# git config --list | grep alias
alias.ci=commit
alias.co=checkout
alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值