git基础操作
git基础操作
代码文件 ---> 暂存区 ---> 缓冲区 ---> 远程仓库
1、安装git
[root@k8s-master-01 ~]# yum install -y git
#查看git版本
[root@k8s-master-01 ~]# git --version
git version 1.8.3.1
2、初始化代码仓库
[root@k8s-master-01 ~]# mkdir test
[root@k8s-master-01 ~]# cd test/
[root@k8s-master-01 test]# git init
Initialized empty Git repository in /root/test/.git/
[root@k8s-master-01 test]# ll -a
total 4
drwxr-xr-x 3 root root 18 Apr 13 15:17 .
dr-xr-x---. 10 root root 4096 Apr 13 15:16 ..
drwxr-xr-x 7 root root 119 Apr 13 15:17 .git
3、将代码提交到git暂存区
[root@k8s-master-01 test]# git add ddff.txt
#PS:
#在仓库中新建一个文件,但是不使用git add命令将其添加到git暂存区,查看时会提示异常
[root@k8s-master-01 test]# vim qln.txt
[root@k8s-master-01 test]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# qln.txt
nothing added to commit but untracked files present (use "git add" to track)
#将文件添加到暂存区后,再次查看,没有异常并提示加入了新文件
[root@k8s-master-01 test]# git add qln.txt
[root@k8s-master-01 test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: qln.txt
#
#对已经添加到暂存区的文件再次修改后,查看信息,会发现修改提示
[root@k8s-master-01 test]# vim qln.txt
[root@k8s-master-01 test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: qln.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: qln.txt
#
4、文件提交到git缓冲区
[root@k8s-master-01 test]# git commit -m 'test'
[master (root-commit) 6f2d032] test
1 file changed, 2 insertions(+)
create mode 100644 ddff.txt
#参数:
-m :添加注释(必须)
#新增文件后再次提交会提示
[root@k8s-master-01 test]# git commit -m 'test'
[master 04fee6b] test
1 file changed, 1 insertion(+)
create mode 100644 qln.txt
[root@k8s-master-01 test]# git status
# 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: qln.txt
#
5、回滚
#git log
#查看提交的历史版本
#git reset --hard [历史版本的commit号]
#回滚到指定的版本
[root@k8s-master-01 test]# cat qln.txt
ooouuu
sssderf
frgwergwefew
[root@k8s-master-01 test]# git log
commit 8598eff618d307209566d3658ea5e2c21780a48f
Author: qilinan <8513633+qilinan@user.noreply.gitee.com>
Date: Tue Apr 13 15:30:42 2021 +0800
test
commit 04fee6b33f39423d2e69977ca5c7e9ab71e61531
Author: qilinan <8513633+qilinan@user.noreply.gitee.com>
Date: Tue Apr 13 15:25:48 2021 +0800
test
commit 6f2d0327830df1371f5dc50f8799fc6ec6f5c15d
Author: qilinan <8513633+qilinan@user.noreply.gitee.com>
Date: Tue Apr 13 15:19:16 2021 +0800
test
[root@k8s-master-01 test]# git reset --hard 04fee6b33f39423d2e69977ca5c7e9ab71e61531
HEAD is now at 04fee6b test
[root@k8s-master-01 test]# cat qln.txt
ooouuu
6、远程仓库
6.1、链接远程仓库
#链接远程仓库后,可以基于该仓库进行代码文件的拉取和推送
[root@k8s-master-01 ddff]# git remote add origin [远程仓库地址]
[root@k8s-master-01 ddff]# git remote add origin git@gitee.com:qilinan/df.git
#查看远程连接
[root@k8s-master-01 ddff]# git remote -v
[root@k8s-master-01 ddff]# git remote -v
origin git@gitee.com:qilinan/df.git (fetch)
origin git@gitee.com:qilinan/df.git (push)
#删除远程链接
[root@k8s-master-01 ddff]# git remote rm origin
6.2、设置远程仓库免密
[root@k8s-node-02 ~]# ssh-keygen
[root@k8s-node-02 ~]# cat /root/.ssh/id_rsa.pub
#将公钥填写到远程仓库的SSH公钥列表中
#将远程仓库的地址写到git本地仓库的配置文件中
[root@k8s-master-01 df]# vim .git/config
url = git@gitee.com:qilinan/df.git
6.3、下载远程仓库
[root@k8s-master-01 ~]# git clone git@gitee.com:qilinan/df.git
Cloning into 'df'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 1), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (9/9), done.
Resolving deltas: 100% (1/1), done.
[root@k8s-master-01 ~]# ll
drwxr-xr-x 3 root root 51 Apr 13 16:04 df
6.4、将本地代码提交到远程仓库
[root@k8s-master-01 df]# git push -u origin [分支]
[root@k8s-master-01 df]# git push -u origin master
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 329 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To git@gitee.com:qilinan/df.git
7c25b17..1513431 master -> master
Branch master set up to track remote branch master from origin.
6.5、拉取远程仓库代码
[root@k8s-master-01 df]# git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From gitee.com:qilinan/df
1513431..20f913e master -> origin/master
Updating 1513431..20f913e
Fast-forward
test.txt | 1 +
1 file changed, 1 insertion(+)
7、分支
同一仓库不同分支之间相互隔离
7.1、查看本地分支
[root@k8s-master-01 df]# git branch
* master
#分支名前面有 * 表示,代表现在处于该分支
7.2、查看远程分支
[root@k8s-master-01 df]# git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
7.3、创建分支
#创建分支可以在网页创建、或者本地通过命令创建
[root@k8s-master-01 df]# git checkout -b test
Switched to a new branch 'test'
[root@k8s-master-01 df]# git branch
master
* test
7.4、切换分支
[root@k8s-master-01 df]# git branch
master
* test
[root@k8s-master-01 df]# git checkout master
Switched to branch 'master'
[root@k8s-master-01 df]# git branch
* master
test
7.5、将本地分支同步到远程
[root@k8s-master-01 df]# git branch
* master
test
[root@k8s-master-01 df]# git push -u origin test
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
remote: Create a pull request for 'test' on Gitee by visiting:
remote: https://gitee.com/qilinan/df/pull/new/qilinan:test...qilinan:master
To git@gitee.com:qilinan/df.git
* [new branch] test -> test
Branch test set up to track remote branch test from origin.
8、标签
标签是分支的一种
标签的特点是不能被更改,只能被删除(基于此特点,发布业务一般都是使用标签)
8.1、新建本地标签
git tag [选项] 标签名 -m '备注'
[root@k8s-master-01 df]# git tag -a test-df -m 'test'
#2、对当前分支打标签
[root@k8s-master-01 df]# git tag master-test -m 'test'
#3、对某次commit打标签
[root@k8s-master-01 df]# git tag log-test 7a3164c457208cb013d8e27d69d300f4154b3fbb -m 'test'
8.2、查看本地标签
#查看标签列表
[root@k8s-master-01 df]# git tag
log-test
master-test
test-df
#查看标签号
[root@k8s-master-01 df]# git show [标签名]
[root@k8s-master-01 df]# git show test-df
8.3、删除本地标签
[root@k8s-master-01 df]# git tag
log-test
master-test
test-df
[root@k8s-master-01 df]# git tag -d master-test
Deleted tag 'master-test' (was 868c7d6)
[root@k8s-master-01 df]# git tag
log-test
test-df
8.4、推送标签
[root@k8s-master-01 df]# git push origin --tags
[root@k8s-master-01 df]# git push origin --tags
Counting objects: 2, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 302 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To git@gitee.com:qilinan/df.git
* [new tag] test-df -> test-df
* [new tag] log-test -> log-test
8.5、拉取指定标签内容
git clone -b [标签名称|分支名称] [仓库地址]