Git

Git

Git是Linux创始人Linus发起的,2005年发布,最初目的是更好管理Linux内核代码。

Git和SVN不同在于Git不需要依赖服务端就可以工作,即Git是分布式的。

git服务端只是用来合并各个最终版本的代码,平时的小改动由我们自己电脑里面的控制中心来管理,git服务端不用关心。

1.单机使用Git

[root@antong ~]#  yum install -y git   //使用yum安装git
[root@antong ~]# git config --global user.name "antong"    //配置基本信息
[root@antong ~]# git config --global user.email "1172734664@qq.com"
[root@antong ~]# mkdir /data/gitroot
[root@antong ~]# cd /data/gitroot
[root@antong gitroot]# git init
Initialized empty Git repository in /data/gitroot.git/
[root@antong gitroot]# echo "hello world" > 1.txt	 //创建一个新文件
[root@antong gitroot]# git add 1.txt		//把1.txt添加到仓库
[root@antong gitroot]# git commit -m "add new file 1.txt"   //add完了必须要commit才算真正把文件提交到git仓库里
 1 file changed, 1 insertion(+), 7 deletions(-)

[root@antong gitroot]# echo "asd">1.txt //再次更改1.txt
[root@antong gitroot]# git status  //查看当前仓库中的状态,比如是否有改动的文件
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
# 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
#
no changes added to commit (use "git add" and/or "git commit -a")

[root@antong gitroot]# git diff 1.txt    //可以对比1.txt本次修改了什么内容,相比较仓库里面的版本
diff --git a/1.txt b/1.txt
index 3b18e51..d72af31 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1 @@
-hello world
+asd
//版本回退
//多更改几次1.txt,然后add,commit
[root@antong gitroot]# git log		//查看所有提交记录
commit 5ada56217c9632205d5668f4f58c7bcb54881c63
Author: antong <1172734664@qq.com>
Date:   Sat Aug 28 11:00:56 2021 -0400

    add new file 1.txt
[root@antong gitroot]# git log --pretty=oneline		//一行显示
5ada56217c9632205d5668f4f58c7bcb54881c63 add new file 1.txt

[root@antong gitroot]# git reset --hard 5ada562 	//回退版本,其中后面跟的字符串是简写
 HEAD is now at 5ada562 add new file 1.txt

//撤销修改
[root@antong gitroot]# rm -f 1.txt		//不小心删除了1.txt
[root@antong gitroot]# git checkout -- 1.txt		//恢复1.txt
如果1.txt文件修改,add后但没有commit,再想回退到上一次提交的状态,可以使用git reset HEAD 1.txt,再执行git checkout -- 1.txt。
[root@antong gitroot]# git reflog		 //查看所有历史版本
5ada562 HEAD@{0}: commit (initial): add new file 1.txt

//删除文件 
[root@antong gitroot]# echo -e "11111111111\n2222222222" > 2.txt
[root@antong gitroot]# git add 2.txt
[root@antong gitroot]# git commit -m "add new file 2.txt"
[master 74515d8] add new file 2.txt
 1 file changed, 2 insertions(+)
 create mode 100644 2.txt

[root@antong gitroot]# git rm 2.txt 
rm '2.txt'
[root@antong gitroot]# git commit -m "rm 2.txt"
[master 79b7556] rm 2.txt
 1 file changed, 2 deletion(-)
 delete mode 100644 2.txt

2.Git远程仓库

①建立远程仓库

首先在浏览器中输入网址https://gitee.com/注册一个账号,创建自己的Git

添加key:单击右上角自己的头像,在下拉菜单中选择“设置”选项。

在跳转的页面中左侧菜单栏选择SSH公钥,把Linux机器上的~/.ssh/id_rsa.pub内容粘贴到公钥中,标题根据自己需求起。

随后可以在gitee使用远程仓库了。

3.Git分支

①分支操作
[root@antong gitroot]# git branch 		//查看分支
* master

[root@antong gitroot]# git branch antong  	//创建分支
[root@antong gitroot]# git checkout lsk 	//切换到了lsk分支下
Switched to branch 'antong'

 //再用git branch查看,会看到有两个分支master和lsk,当前使用的分支前面会有一个*在lsk分支下 ,编辑2.txt,并提交到新分支
[root@antong gitroot]# git branch
* antong
  master
[root@antong gitroot]# echo "askdfjlksadjflk" >  2.txt
[root@antong gitroot]# git add 2.txt
[root@antong gitroot]# git commit -m "laksjdflksjdklfj"
[antong 834be26] laksjdflksjdklfj
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt

   //切换回master分支
[root@antong gitroot]# git checkout master	 //此时cat 2.txt发现并没有更改内容
Switched to branch 'master'
②分支的合并
[root@antong gitroot]# git checkout master	 //合并分支之前,先切换到目标分支 
Switched to branch 'master'

[root@antong gitroot]# git merge antong     //把lsk分支合并到了master
Updating 0f8bca1..834be26
Fast-forward
 2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
③分支删除
[root@antong gitroot]# git  branch -d antong	//删除分支
Deleted branch lsk (was 89b88e8).
 //如果分支没有合并,删除之前会提示,那就不合并,强制删除

[root@antong gitroot]# git branch -D lsk
Deleted branch lsk (was 89b88e8).
④使用分支的原则

对于分支的应用,建议大家以这样的原则来:

① master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。

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

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

dev分支合并bob分支的命令是:

[root@antong gitroot]# git checkout dev   //先切换到dev分支,然后
Switched to branch 'dev'

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

4.Git标签与别名

①Git标签

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

[root@antong gitroot]# git checkout master
Switched to branch 'master'
[root@antong gitroot]# git tag v1.0
[root@antong gitroot]# git show v1.0
commit 834be26e54d1a43e0553fc73c8ad67cc1b4a0d48
Author: antong <1172734664@qq.com>
Date:   Sat Aug 28 11:35:43 2021 -0400

    laksjdflksjdklfj

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

[root@antong gitroot]# git tag
v1.0
//Tag是针对commit来打标签的,所以可以针对历史的commit来打tag
[root@antong gitroot]# git log --pretty=oneline --abbrev-commit //查看历史的commit
834be26 laksjdflksjdklfj
0f8bca1 add new file 1.txt
[root@antong gitroot]# git tag v0.9 0f8bca1    //针对历史commit打标签
[root@antong gitroot]# git tag -a v0.8 -m "tag just v1.1 and so on" 834be26
[root@antong gitroot]# git tag -d v0.8     //删除标签
Deleted tag 'v0.8' (was 343919b)
[root@antong gitroot]# git push origin v1.0   //推送指定标签到远程
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:lisikai1999/gitroot.git
 * [new tag]         v1.0 -> v1.0

[root@antong gitroot]# git push --tag origin   //推送所有标签
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:lisikai1999/gitroot.git
 * [new tag]         v.09 -> v.09

 //如果本地删除了一个标签,远程也想要删除需要这样操作:
[root@antong gitroot]# git tag v1.0 -d    //删除本地标签
Deleted tag 'v1.0' (was ea53299)

[root@antong gitroot]# git push origin :refs/tags/v1.0   //删除远程标签
To git@github.com:lisikai1999/gitroot.git
 - [deleted]         v1.0

②Git别名

git commit这个命令有点长,用别名可以提高我们的工作效率。

[root@antong gitroot]# git config --global alias.ci commit
[root@antong gitroot]# git config --global alias.co  checkout
[root@antong gitroot]# git config --global alias.br  branch
 //查看git别名使用命令
[root@antong gitroot]# git config --list |grep alias
alias.ci=commit
alias.co=checkout
alias.br=branch

 //查询log小技巧:
[root@antong 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@antong gitroot]# git config --global --unset alias.br 
posted @ 2021-08-28 23:59  殇黯瞳  阅读(88)  评论(0编辑  收藏  举报