【图文教程】代码管理平台 git

1. 单机上使用git

git是分布式的仓库,我们不需要把代码上传或更新到某个特定的服务器上,所以它不需要依赖网络,我们可以在本地创建一个git仓库。

  • git安装命令
yum install -y git
  • 创建git仓库
mkdir /data/gitroot/
  • git 初始化仓库
1. cd /data/gitroot/

2. git init         #初始化git仓库
Initialized empty Git repository in /data/gitroot/.git/  #初始化空的 Git 版本库于 /data/gitroot/.git/

3. [root@test01 gitroot]# ls -la .
total 0
drwxr-xr-x. 3 root root  18 Nov 23 14:31 .
drwxr-xr-x. 5 root root  49 Nov 23 14:30 ..
drwxr-xr-x. 7 root root 119 Nov 23 14:31 .git

4. [root@test01 gitroot]# ls .git/
branches  config  description  HEAD  hooks  info  objects  refs
  • 创建文件,输入代码,并添加到仓库中
[root@test01 gitroot]# vim 1.txt
[root@test01 gitroot]# git add 1.txt               #把1.txt添加到仓库
[root@test01 gitroot]# git commit -m "add 1.txt"   #add完了必须要commit才算真的添加到仓库

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@test02.(none)')


# 为了避免老是打印提示信息,可以随便设置一下这两项信息
[root@test01 gitroot]# git config --global user.name "sc" 
[root@test01 gitroot]# git config --global user.email sc@none.com
  • 修改文件中的内容,然后进行提交
[root@test01 gitroot]# echo 888 >1.txt
[root@test01 gitroot]# git add 1.txt
[root@test01 gitroot]# git commit -m "add 1.txt agin"
[master a2d4181] add 1.txt agin
 1 file changed, 1 insertion(+), 6 deletions(-)
  • git status 命令可以查看当前仓库中的状态,比如是否有改动的文件等
[root@test01 gitroot]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   1.txt
#
# 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
  • git diff 命令可以对比某个文件本次修改了什么内容,相比较仓库里面的版本
[root@test01 gitroot]# git diff 1.txt
diff --git a/1.txt b/1.txt
index dc6a988..190a180 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1 @@
-888
+123
  • git log 查看所有的提交记录
1. [root@test01 gitroot]# git log
commit 90dc23420b39ffb3153aca5ed2d327c81b524c19
Author: sc <sc@none.com>
Date:   Mon Nov 23 16:43:43 2020 +0800

    add 1.txt

commit 8de29fe07e3b1199d5ccec3ef1ff6d648cf75422
Author: sc <sc@none.com>
Date:   Mon Nov 23 16:43:02 2020 +0800

    add 1.txt agin


2. [root@test01 gitroot]# git log --pretty=oneline        #一行显示提交记录
90dc23420b39ffb3153aca5ed2d327c81b524c19 add 1.txt
8de29fe07e3b1199d5ccec3ef1ff6d648cf75422 add 1.txt agin
  • 回退版本,其中后面跟的字符串是简写
1. [root@test01 gitroot]# git log --pretty=oneline   
3f5eccf4e0c55b679db5a6ce4adf7452ca774adb ad 1.txt
90dc23420b39ffb3153aca5ed2d327c81b524c19 add 1.txt
8de29fe07e3b1199d5ccec3ef1ff6d648cf75422 add 1.txt agin


2. [root@test01 gitroot]# git reset --hard 8de2      #回退版本,后面跟字符串简写
HEAD is now at 8de29fe add 1.txt agin


3. [root@test01 gitroot]# cat 1.txt
adfsdaf
a
a
a
a
a
a


4. [root@test01 gitroot]# git log --pretty=oneline
8de29fe07e3b1199d5ccec3ef1ff6d648cf75422 add 1.txt agin
  • 如果回退版本后,发现不合适,想要回退到新版本或者其他历史版本上,可以使用 git reflog 命令查看所有历史版本
[root@test01 gitroot]# git reflog           #查看所有历史版本
8de29fe HEAD@{0}: reset: moving to 8de2
3f5eccf HEAD@{1}: commit: ad 1.txt
90dc234 HEAD@{2}: commit: add 1.txt
8de29fe HEAD@{3}: commit (initial): add 1.txt agin
  • 通过git可以恢复删除的文件,前提是你已经将文件提交到了仓库中。如果不小心把某个文件删除了,而这个文件已经存储在仓库中的话,就可以从仓库恢复这个文件
[root@test01 gitroot]# ls
1.txt
[root@test01 gitroot]# rm -fr 1.txt
[root@test01 gitroot]# ls
[root@test01 gitroot]# git checkout -- 1.txt     #恢复删除的仓库文件
[root@test01 gitroot]# ls
1.txt
  • 如果某个文件进行了修改,add后但没有commit,再想回退到上一次提交的状态,可以使用 git reset HEAD filename,再执行git checkout – filename
[root@test01 gitroot]# vim 1.txt
[root@test01 gitroot]# git add 1.txt
[root@test01 gitroot]# git reset HEAD 1.txt   #退回上一次提交状态
Unstaged changes after reset:
M	1.txt
[root@test01 gitroot]# cat 1.txt
a
a
a
a
a
a
[root@test01 gitroot]# git checkout -- 1.txt  #恢复库文件
[root@test01 gitroot]# cat 1.txt
adfsdaf
a
a
a
a
a
a
  • 删除仓库中的文件
[root@test01 gitroot]# git rm 1.txt
rm '1.txt'
[root@test01 gitroot]# git commit -m "delete 1.txt"
[master 3272657] delete 1.txt
 1 file changed, 7 deletions(-)
 delete mode 100644 1.txt
  • 即便删除了仓库中的文件,也是可以通过版本id来恢复的
[root@test01 gitroot]# git log --pretty=oneline
3272657e568a05095a392d66ed74b1ef52e15c4a delete 1.txt
8de29fe07e3b1199d5ccec3ef1ff6d648cf75422 add 1.txt agin
[root@test01 gitroot]# git reset --hard 8de29
HEAD is now at 8de29fe add 1.txt agin
[root@test01 gitroot]# ls
1.txt
[root@test01 gitroot]# cat 1.txt
adfsdaf
a
a
a
a
a
a

2. 建立远程仓库

  • 首先到登陆 https://github.com 注册账号

请添加图片描述

请添加图片描述

  • 登录之后,点击右上角,头像旁边的 + 图标,创建一个自己的repository(仓库)

请添加图片描述

  • 填写仓库的相关信息

请添加图片描述

  • 创建完成,如下,远程仓库就创建好了

请添加图片描述

  • 可以把GitHub上创建的仓库,添加密钥,作为我们的远程服务端

请添加图片描述

请添加图片描述

[root@test01 ~]# 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:EDwTQZlpr7oM0C0vh7hztaqFrz2AUZPJnof6NlVfV2Q root@test01
The key's randomart image is:
+---[RSA 2048]----+
| . o o=*     .E  |
|  *   O.     ..  |
| o + ..+     .   |
|..+.. ... . .    |
|ooo... oS. .     |
|++ +o . .        |
|oo=ooo           |
|.=*=o            |
|+*++o.           |
+----[SHA256]-----+
[root@test01 ~]# ls -l .ssh/
total 8
-rw-------. 1 root root 1679 Nov 24 14:12 id_rsa
-rw-r--r--. 1 root root  393 Nov 24 14:12 id_rsa.pub
[root@test01 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwoaC8+9H4twVx4kQuFsYY/m55270B2PMHUF+D2adhGr0LUKgqy0X9UeOOSNXlWspqHBqQRI0uy4y2uaQSMv7J8QuKsEwJdLaZKOy0Kyz6vSt4htG0cgPJfS4i9/JAcRTtaTs9VHh8F8bRKJTFXs1ErKtSeXBkzzzCKEH2CimcEqFcOUQ8pRmaLE69P5LEPz91bGxLxIf+KofX3O6f4LBrlUQQECCFO80T/Z545bu1+7EkYIUm+SfDlPP7ObgWqkWKTZhiCAQi7QgBqqE0cTip+pwaN36EBIvnlOvXOPwbceY/Pm9wGA90HorYiyhCz/xsiZ9X88no8a6g32rW7U+j root@test01

请添加图片描述

在这里插入图片描述

以上已经在GitHub上创建好了一个远程仓库,并且也添加了密钥认证,现在我们就可以在本地上连接这个仓库了。

  • 创建一个目录,用于存放和上传仓库文件,也相当于是一个本地仓库
1. [root@test01 ~]# mkdir /tmp/test


2. [root@test01 ~]# cd !$
cd /tmp/test
  • 根据GitHub的操作示例进行仓库的初始化
1. [root@test01 test]# echo "# test" >> README.md


2. [root@test01 test]# git init
Initialized empty Git repository in /tmp/test/.git/


3. [root@test01 test]# ls -la
total 4
drwxr-xr-x.  3 root root  35 Nov 24 14:30 .
drwxrwxrwt. 10 root root 256 Nov 24 14:25 ..
drwxr-xr-x.  7 root root 119 Nov 24 14:30 .git
-rw-r--r--.  1 root root   7 Nov 24 14:29 README.md


4. [root@test01 test]# git add README.md


5. [root@test01 test]# git commit -m "first commit"
[master (root-commit) 449a715] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
  • 将本地文件推送到远程仓库上
1. [root@test01 test]# git remote add origin git@github.com:2584765560/test.git


2. [root@test01 test]# 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@github.com:2584765560/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
  • 然后再创建一个文件,再次进行推送
1. [root@test01 test]# vim 1.txt


2. [root@test01 test]# git add 1.txt


3. [root@test01 test]# git commit -m "1.txt"
[master 8a7e879] 1.txt
 1 file changed, 3 insertions(+)
 create mode 100644 1.txt


4. [root@test01 test]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:2584765560/test.git
   449a715..8a7e879  master -> master

3. 克隆远程仓库

我们也可以将远程仓库给克隆到本地机器上

  • 复制远程仓库的URL链接

在这里插入图片描述

  • 然后到本地机器上执行命令进行克隆

在这里插入图片描述

1. [root@test01 test]# cd /home/


2. [root@test01 home]# git clone git@github.com:2584765560/test.git
Cloning into 'test'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 0), reused 9 (delta 0), pack-reused 0
Receiving objects: 100% (9/9), done.

#公开的仓库是任何人都可以进行克隆的,但是只能克隆不可以对仓库进行写操作。
  • 对克隆的文件进行更改,然后再推送到远程的仓库,因为我们是该仓库的所有者,可以进行写操作
1. [root@test01 test]# cd /home/


2. [root@test01 home]# ls
sc01  test


3. [root@test01 home]# cd test/


4. [root@test01 test]# ls
1.txt  2.txt  README.md


5. [root@test01 test]# vim README.md 


6. [root@test01 test]# git add README.md 


7. [root@test01 test]# git commit -m "change README.md"
[master bde1c0a] change README.md
 1 file changed, 6 insertions(+), 3 deletions(-)


8. [root@test01 test]# git push
  • 我现在在GitHub上更改这个文件的内容,更改之后同样可以在本地把新内容拉下来

在这里插入图片描述

在这里插入图片描述

[root@test01 test]# git pull            #把更新内容拉下来
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:2584765560/test
   bde1c0a..84d1280  master     -> origin/master
Updating bde1c0a..84d1280
Fast-forward
 README.md | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

4. git本地分支管理

  • 分支管理是git比较重要的一个概念,平时用的也比较多。我们先来在本地的仓库里操作一下分支
1. [root@test01 ~]# cd /data/gitroot/


2. [root@test01 gitroot]# git branch              #查看当前分支的仓库都有哪些
* master                                          # *号代表当前所在的分支是那个


3. [root@test01 gitroot]# ls
1.txt
  • 创建分支
1. [root@test01 gitroot]# git branch aming        #创建分支


2. [root@test01 gitroot]# git branch              #查看都有那些分支
  aming
* master
  • 切换分支
1. [root@test01 gitroot]# git checkout aming      #切换分支
Switched to branch 'aming'


2. [root@test01 gitroot]# git branch              #查看分支
* aming
  master

#分支跟分支之间是相互隔离开的,在当前分支下进行的操作不会影响到其他分支
  • 合并分支

如果想要别的分支也同步当前分支的操作,可以对分支进行合并,合并分支之前,需要先切换到目标分支

1. [root@test01 gitroot]# git checkout master     #先切换到目标分支
Switched to branch 'master'


2. [root@test01 gitroot]# git branch              #查看当前分支
  aming
* master


3. [root@test01 gitroot]# git merge aming         #合并分支
Updating 8de29fe..7f1e2fe
Fast-forward
 11.txt | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 11.txt


4. [root@test01 gitroot]# git branch              #查看当前分支
  aming
* master


5. [root@test01 gitroot]# ls                      #已经合并成功了
11.txt  1.txt

如果master分支和aming分支都对2.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。

解决冲突的方法是在master分支下,编辑2.txt,改为aming分支里面2.txt的内容。 然后提交2.txt,再合并aming分支。

但是这样有一个问题,万一master分支更改的内容是我们想要的呢? 可以编辑2.txt内容,改为想要的,然后提交。切换到aming分支,然后合并master分支到aming分支即可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。

  • 删除分支的命令
1. [root@test01 gitroot]# git branch
  aming
* master


2. [root@test01 gitroot]# git branch -d aming      #删除aming分支
Deleted branch aming (was 7f1e2fe).
  • 如果分支没有合并,删除之前会提示,那就不合并,可以使用以下命令进行强制删除
[root@test01 gitroot]# git branch -D aming         #强制删除

5. 使用分支的原则

  • 对于分支的应用,建议大家以这样的原则来
1. master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。


2. 创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master


3. 开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到dev分支

在这里插入图片描述

  • dev分支合并bob分支的命令是
1. git checkout dev //先切换到dev分支,然后


2. git merge bob

6. git远程分支管理

  • 在 github上创建一个 dev 分支

请添加图片描述

请添加图片描述

请添加图片描述

[root@test01 test]# git clone git@github.com:2584765560/test.git
Cloning into 'test'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 24 (delta 5), reused 14 (delta 2), pack-reused 0
Receiving objects: 100% (24/24), done.
Resolving deltas: 100% (5/5), done.
[root@test01 test]# ls
1.txt  2.txt  README.md  test
[root@test01 test]# cd test
[root@test01 test]# ls
1.txt  2.txt  README.md

#如上可以看到,当我们使用git clone命令克隆远程仓库的时候默认只会把master分支克隆下来,而不会克隆其他的分支
  • 查看远程仓库所有分支的命令
[root@test01 test]# git ls-remote origin
84d128068fb50115584908e97c368c185339a81f	HEAD
4bb38130df9b25353bfa14926c069b174034d024	refs/heads/dev
84d128068fb50115584908e97c368c185339a81f	refs/heads/master
a82ef83e1060ad89f83a7b36ec8365b617951864	refs/heads/sc
  • 现在我们需要把dev分支给克隆下来,需要自己先手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称要一致
1. [root@test01 test]# git checkout -b dev origin/dev     #手动创建 本地和远程对应分支
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'


2. [root@test01 test]# git branch
* dev
  master


3. [root@test01 test]# vim 88.txt


4. [root@test01 test]# git add 88.txt 


5. [root@test01 test]# git commit -m "ch 88.txt"
[dev 0926ca8] ch 88.txt
 1 file changed, 4 insertions(+)
 create mode 100644 88.txt


6. [root@test01 test]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:2584765560/test.git
   4bb3813..0926ca8  dev -> dev

在这里插入图片描述

  • 当本地分支和远程分支一致时,git push默认会把所有本地分支的变更一同推送到远程,如果想只推送某一个分支,可以使用git push origin branch-name命令
1. [root@test01 test]# vim 123.txt


2. [root@test01 test]# git add 123.txt


3. [root@test01 test]# git commit -m "add 123.txt"
[dev 2888266] add 123.txt
 1 file changed, 2 insertions(+)
 create mode 100644 123.txt


4. [root@test01 test]# git push origin dev
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:2584765560/test.git
   0926ca8..2888266  dev -> dev
  • 当本地分支比远程分支多,默认git push 只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name 命令, 如果推送失败,先用git pull抓取远程的新提交
1. [root@test01 test]# git branch linux


2. [root@test01 test]# git branch
* dev
  linux
  master


3. [root@test01 test]# git checkout linux
Switched to branch 'linux'


4. [root@test01 test]# git branch
  dev
* linux
  master


5. [root@test01 test]# vim yy.txt


6. [root@test01 test]# git add yy.txt 


7. [root@test01 test]# git commit -m "add yy.txt"
[linux 2b69204] add yy.txt
 1 file changed, 2 insertions(+)
 create mode 100644 yy.txt


8. [root@test01 test]# git push origin linux
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 259 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: 
remote: Create a pull request for 'linux' on GitHub by visiting:
remote:      https://github.com/2584765560/test/pull/new/linux
remote: 
To git@github.com:2584765560/test.git
 * [new branch]      linux -> linux

在这里插入图片描述

  • 在本地删除远程分支
1. git push origin :远程分支名称

2. git push origin --delete 远程分支名称

3. [root@test01 test]# git push origin --delete sc
To git@github.com:2584765560/test.git
 - [deleted]         sc
  • git 删除本地文件和远程文件
删除文件: git rm 文件名

删除文件夹: git rm -r 文件夹

 

然后:

有修改的文件就执行git add .

如果没有修改的文件直接跳过add,执行下一步

git commit -m "备注"

git push origin 分支名

7. git标签管理

标签类似于虚拟机的快照功能,可以给版本库打一个标签,记录某个时刻库的状态,也可以随时恢复到该标签标记的状态。

  • 给master打一个标签v1.0
1. [root@test01 test]# git branch
* master
  sc


2. [root@test01 test]# git tag v1.0       #给master打一个标签v1.0


3. [root@test01 test]# git tag            #可以查看所有标签
v1.0


4. [root@test01 test]# git show v1.0      #查看标签信息
commit 84d128068fb50115584908e97c368c185339a81f
Author: 2584765560 <74949085+2584765560@users.noreply.github.com>
Date:   Tue Nov 24 16:04:39 2020 +0800

    Create README.md

diff --git a/README.md b/README.md
index 4f8f4a5..c8d9d64 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,3 @@
 # test
 linux
-python
-nginx
-mysql
-tomcat
-php-fpm
+
  • 针对commit来打标签的
1. [root@test01 test]# git log --pretty=oneline --abbrev-commit    #查看简写的commit
84d1280 Create README.md
bde1c0a change README.md
a293b3f add README.md
5eb8628 2.txt
8a7e879 1.txt
449a715 first commit


2. [root@test01 test]# git tag v0.8 8a7e879                        #针对历史记录commit打标签


3. [root@test01 test]# git tag                                     #查看所有标签
v0.8
v1.0
  • 可以对标签进行描述
1. [root@test01 test]# git log --pretty=oneline --abbrev-commit      
84d1280 Create README.md
bde1c0a change README.md
a293b3f add README.md
5eb8628 2.txt
8a7e879 1.txt
449a715 first commit


2. [root@test01 test]# git tag -a v0.1 -m "1234567890" 5eb8628        #针对commit打标签,进行描述


3. [root@test01 test]# git show v0.1                                  #查看标签信息
tag v0.1  
Tagger: sc <sc@none.com>
Date:   Wed Nov 25 15:52:25 2020 +0800

1234567890

commit 5eb86283c1bb6ae09a87b9c9da7edfbfbfdbd324
Author: sc <sc@none.com>
Date:   Tue Nov 24 15:05:14 2020 +0800

    2.txt

diff --git a/2.txt b/2.txt
new file mode 100644
index 0000000..23d6110
--- /dev/null
+++ b/2.txt
@@ -0,0 +1,3 @@
+sdfsafdsf
+asdfdsafsadf
+sadfsadfsda
  • 删除标签
[root@test01 test]# git tag -d v0.1      #删除标签
Deleted tag 'v0.1' (was 477fcc0)
  • 推送某个指定的标签到远程仓库上

在这里插入图片描述

[root@test01 test]# git push origin v1.0       #推送指定标签到远程
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:2584765560/test.git
 * [new tag]         v1.0 -> v1.0

在这里插入图片描述

  • 推送所有标签到远程仓库上
[root@test01 test]# git push --tag origin
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:2584765560/test.git
 * [new tag]         v0.8 -> v0.8

在这里插入图片描述

  • 如果本地删除了一个标签,远程也想要删除需要这样操作
1. [root@test01 test]# git tag -d v1.0      #删除本地标签
Deleted tag 'v1.0' (was 84d1280)


2. [root@test01 test]# git push origin :refs/tags/v1.0     #远程删除标签
To git@github.com:2584765560/test.git
 - [deleted]         v1.0

在这里插入图片描述

  • 根据标签还原到具体版本
1. [root@test01 test]# git show v0.8               #查看标签信息
commit 8a7e879f528910a9440b977964dc3e2be9d792ba
Author: sc <sc@none.com>
Date:   Tue Nov 24 14:48:00 2020 +0800

    1.txt

diff --git a/1.txt b/1.txt
new file mode 100644
index 0000000..dfb9eee
--- /dev/null
+++ b/1.txt
@@ -0,0 +1,3 @@
+sdfdsaf
+adsfsadf
+asdfasdfsad


2. [root@test01 test]# git reset --hard 8a7e87     #根据标签信息找到commit 回退代码
HEAD is now at 8a7e879 1.txt

8. git别名

  • 创建别名
1. [root@test01 test]# git config --global alias.ci commit  
#git config --global alias. 为固定格式  ci 为别名  commit为原指令


2. [root@test01 test]# ls
1.txt  README.md


3. [root@test01 test]# vim 3.txt


4. [root@test01 test]# git add 3.txt


5. [root@test01 test]# git ci -m "add 3.txt"       #ci为别名
[master 045e68f] add 3.txt
 1 file changed, 3 insertions(+)
 create mode 100644 3.txt


6. [root@test01 test]# ls
1.txt  3.txt  README.md
  • 其他别名设置
1. [root@test01 test]# git config --global alias.co  checkout    #设置别名为 co


2. [root@test01 test]# git co sc
Switched to branch 'sc'


3. [root@test01 test]#  git config --global alias.br  branch     #设置别名为 br


4. [root@test01 test]# git br
  master
* sc
  • 查看git命令的别名
1. [root@test01 test]# git config --list |grep alias   #查看 git 所有别名
alias.ci=commit
alias.co=checkout
alias.br=branch


2. [root@test01 test]# git config --list               #查看所有配置
user.name=sc
user.email=sc@none.com
alias.ci=commit
alias.co=checkout
alias.br=branch
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:2584765560/test.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.sc.remote=origin
branch.sc.merge=refs/heads/sc


3. [root@test01 test]# cat /root/.gitconfig            #查看配置相关参数,想加新的别名,直接在配置文件里加就行了
[user]
	name = sc
	email = sc@none.com
[alias]
	ci = commit
	co = checkout
	br = branch
  • 查询log小技巧
1. [root@test01 test]# 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"
#使用如下命令配置log命令的别名,可以让log查询的结果更易读


2. [root@test01 test]# git lg
* c0817b6 - (HEAD, origin/sc, sc) Create sc.txt (4 hours ago) <2584765560>
* 84d1280 - (origin/master) Create README.md (25 hours ago) <2584765560>
* bde1c0a - change README.md (25 hours ago) <sc>
* a293b3f - add README.md (25 hours ago) <sc>
* 5eb8628 - 2.txt (26 hours ago) <sc>
* 8a7e879 - (tag: v0.8) 1.txt (26 hours ago) <sc>
* 449a715 - first commit (26 hours ago) <sc>
#输入以上命令配置log命令的别名后,现在使用这个命令查看日志效果如下
  • 取消别名
1. [root@test01 test]# git config --global --unset alias.ci        #取消别名
2. [root@test01 test]# git config --global --unset alias.co        #取消别名
3. [root@test01 test]# git config --global --unset alias.br        #取消别名


4. [root@test01 test]# cat /root/.gitconfig 
[user]
	name = sc
	email = sc@none.com
[alias]
	lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit