实习生 Git 不熟练,还没脸去问是种什么体验

请添加图片描述

文章目录

尴尬

昨儿个我导师给了我个 Git 相关的小任务,不得不说我导师是真的好,他怕我不会还演示了一遍,奈何我只会 git clone。。。
还好他演示的时候把命令都留在终端上了,折腾了半小时我也算学的有模有样吧。
那半小时我坐立不安呐,又不会,又没脸去问,真尴尬。。。

这不,下班回来就卷了一晚上,搞出这么一份手册来。


Git 概述

Git 是一个免费的、开源的分布式版本控制系统,可以快速高效地处理从小型到大型的各种
项目。

Git 的特色是分支,人家图标上都敢这么画了。

对了,这里要提一下,Git 和 Linux 是同一个爸爸,所以 Linux 系统上能跑的原生指令 Git 上面也都可以,反之也一样。


git 分区原理

工作区。新文件没被add到暂存区,显红色。

暂存区。进仓库前暂时存放区域,未对本地仓库生效。对暂存区文件修改,显蓝色。第一次创建并add到暂存区的文件,由于远程仓库没同步,显绿色。(注:存放在 “.git/index目录” )

本地仓库 。暂存区commit代码放入本地仓库,通过 push 推送到远程仓库。(注:存在“.git”目录。)

远程仓库。在前面的话,仓库里都有些什么东西只有你自己知道,但是一旦把本地仓库推到远程仓库之后,仓库里有什么东西大家就都知道了。


Git 常用指令

在这里插入图片描述

(本篇讲解包括但不限于这些命令)

设置用户签名

开头两个命令是用来设置用户签名的,这个一般就用一次就完事儿了,因为你要提交代码的时候是需要责任跟踪的。

这个 name,email 啥的,人家也不会去认证,不过组里面肯定是会有规范的,也不会让你想起啥名就随便起吧。


初始化本地库

新手一般就老老实实 git init 起手嘛,昨天我也是这么问我导师的,他说不用,直接 git clone 就好了。我想想也是,团队开发嘛。

查看本地库状态

git status

首次查看,工作区是没有什么文件的:

$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)

当我们新增了一个文件之后,再查看状态, 会检测到未追踪的文件:

$ git status
On branch master
No commits yet
Untracked files:
 (use "git add <file>..." to include in what will be committed)
 hello.txt
nothing added to commit but untracked files present (use "git add" 
to track)

添加暂存区

git add 文件名


$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working 
directory.

再次查看状态(检测到暂存区有新文件:)

$ git status
On branch master
No commits yet
Changes to be committed:
 (use "git rm --cached <file>..." to unstage)
 new file: hello.txt

提交本地库

git commit -m "日志信息" 文件名


$ git commit -m "my first commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working 
directory.
[master (root-commit) 86366fa] my first commit
1 file changed, 16 insertions(+)
create mode 100644 hello.txt

再查看状态(没有文件需要提交):

$ git status
On branch master
nothing to commit, working tree clean

修改文件

修改一下文件之后再查看状态:

$ 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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")

那么接下来就需要再次将修改的文件提交到暂存区,然后提交到本地仓库。


查看历史版本

git reflog 查看版本信息
git log 查看版本详细信息

如果是想快速浏览版本信息,可以使用 reflog 即可,毕竟公司项目版本迭代信息可能已经很多了,想看详细日志也得看你有没有那个精力了。

$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit

版本

以前我只知道版本回退,现在我知道原来还能向新。

git reset --hard 版本号

版本号是什么?版本号,查看一下日志就能看到了。

$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit


--切换到 86366fa 版本,也就是我们第一次提交的版本
$ git reset --hard 86366fa
HEAD is now at 86366fa my first commit


--切换完毕之后再查看历史记录,当前成功切换到了 86366fa 版本
$ git reflog
86366fa (HEAD -> master) HEAD@{0}: reset: moving to 86366fa
087a1a7 HEAD@{1}: commit: my third commit
ca8ded6 HEAD@{2}: commit: my second commit
86366fa (HEAD -> master) HEAD@{3}: commit (initial): my first commit
--然后查看文件 hello.txt,发现文件内容已经变化

Git 切换版本,底层其实是移动的 HEAD 指针。


Git 分支操作

在这里插入图片描述

在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本。(分支底层其实也是指针的引用)

在这里插入图片描述

分支基本操作

在这里插入图片描述

查看分支

小伙子,不要一上来就想着创建分支嘛。先看看前辈们已经达到了哪个高度了嘛。

git branch -v


$ git branch -v
* master 087a1a7 my third commit (*代表当前所在的分区)

创建分支

git branch 分支名


$ git branch hot-fix
$ git branch -v
hot-fix 087a1a7 my third commit (刚创建的新的分支,并将主分支 master
的内容复制了一份)
* master 087a1a7 my third commit

分支开发

接下来就可以在分支上进行开发了,开发完之后记得将代码提交到本地仓库。


切换分支

git checkout 分支名


$ git checkout hot-fix
Switched to branch 'hot-fix'
--发现当先分支已由 master 改为 hot-fix

合并分支

git merge 分支名

这里有个要注意的点:不仅是可以把旁支合并到主支上,主支也可以被合并到分支上,分支之间也可以互相合并。

反正你爱怎么玩怎么玩,都是在你本地仓库上,又没有推送到远程仓库。

那现在我们想把旁支合并到主支上要怎么做呢?

1、切换到主支
2、git merge 旁支名

这样就万事大吉了吗?不一定的。脑子清醒的话很容易想到一个问题:主支修改了一个文件,分支也修改了同一个文件,现在以谁的为准?

脑子更清醒的人就要说了:你傻呀,我 clone 下来就切到分支去操作了,在我本地仓库我又不会去动主支的。

脑子再清醒一点的人已经懒的跟他说了:大哥,咱这是团队开发,咱的本地仓库早晚是都要合并到主线上的。


合并冲突解决

冲突产生的表现:后面状态为 MERGING

打开冲突文件可以看到冲突的地方:

hello git! hello atguigu!
<<<<<<< HEAD
hello git! hello atguigu! master test
hello git! hello atguigu!
=======
hello git! hello atguigu!
hello git! hello atguigu! hot-fix test
>>>>>>> hot-fix

HEAD 到 === 的部分是主支的,后面是 hot-fix 的

冲突产生的原因:
合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git 无法替我们决定使用哪一个。必须人为决定新代码内容。

解决办法:

1、删掉冲突中不要的部分,留下最终的部分
2、git add 添加到暂存区
3、 git commit -m 注意,此时的 commit 后面不能带文件名。带文件名是几个意思呢?到底用哪个呢?
然后就会发现合并成功了。

Git 团队协作机制

团队协作

在这里插入图片描述

跨团队协作

在这里插入图片描述


远程仓库

这里不说 github,因为还有 gitlab,公司内部代码怎么能放到广域网上呢!!!
在这里插入图片描述

别名

为什么要别名呢?其实起不起别名都无所谓,主要是有个别名方便操作。你说是记一个网址容易还是记一个自己起的名字容易嘞?

$ git remote -v
$ git remote add ori https://github.com/xxx.git
$ git remote -v
ori https://github.com/xxx.git (fetch)
ori https://github.com/xxx.git (push)

推送本地仓库到远程仓库

当文件还在本地仓库的时候,咱怎么玩都行。但是一旦推送上去了,那就产生历史版本了,大家都看得到了。
不过这个推送啊,也不是咱想推送就能推送的,还得管理员给咱授权,这个不难理解吧。

git push 别名 分支


$ git push ori master
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': atguiguyueyue
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/atguiguyueyue/git-shTest.git
* [new branch] master -> master

克隆远程仓库到本地

这不用说吧,这个很简单。咱虽然说之前不会推送,但是拉取还是没有问题的。

clone 会做如下操作。1、拉取代码。2、初始化本地仓库。3、创建别名。

会创建别名,你 clone 之后用 git remote -v 看一下,一般是 origin。


拉取远程库内容

这个有两个办法,昨天我用的是先 clone,在 checkout 的方式选择我要的分支。

也可以直接 pull

git pull 远程库地址别名 远程分支名


$ git pull ori master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/atguiguyueyue/git-shTest
* branch master -> FETCH_HEAD
 7cb4d02..5dabe6b master -> ori/master
Updating 7cb4d02..5dabe6b
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

写了这些,github 也可以做管理,不过这些目前不是我们该关心的,就先不放出来了。我再回头看看有没有要补充的。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>