git 底子操作
1. git 的安装 与 卸载
1.1. git 的安装
判定是否安装 git
git --version
安装 git:
centos: sudo yum -y install git
ubuntu: sudo apt-get install git -y
windows: 3.安装git和图形化界面工具_哔哩哔哩_bilibili
1.2. git 的卸载
判定是否安装 git
git --version
卸载 git:
ubuntu: sudo apt-get remove git -y
2. 初始化创建当地仓库
为什么要创建当地仓库?
要提前说的是,仓库是举行版本控制的一个文件目次。我们要想对文件举行版本控制,就必须先创建一个仓库出来。
创建当地仓库: git init
当前目次下多了一个.git的隐藏文件,.git目次是 git 来跟踪管理仓库的, 不要手动修改这个目次内里的文件,否则改乱了,就把gi仓库给破坏了。
3. 配置当地仓库
先说配置指令:
git config [--global] user.name "Your Name"
git config [--global] user.email "email@example.com"
git config [--global] --unset user.name
git config [--global] --unset user.email
// 如果你想全局配置就加 [--global] 选项.
// 如果你想取消 [--global] 配置的选项, 同样也需要带 [--global] 选项. 下面举行演示:
https://i-blog.csdnimg.cn/img_convert/7f8c5779bd024b571aefcc263966d0ed.png
https://i-blog.csdnimg.cn/img_convert/252bf8e9a88416776378ad80fd1c7d91.png
4. 熟悉工作区, 暂存区, 版本库
工作区:是在电脑上你要写代码或文件的目次。
暂存区:英文叫stage或index.一般存放在。 在。git目次下的index文件 (.git/index)中,我们把暂存区有时也叫作索引l(index)
版本库:又名仓库,英文名repository.工作区有一个隐藏目次。git,它不算工作区,而是gi的版本库。这个版本库内里的全部文件都可以被gt管理起来,每个文件的修改,删除,git都能跟踪,以便任何时刻都可以追踪汗青,或者在将来某个时刻可以"还原".
下面这个图展示了工作区,暂存区和版本库之间的关系:
https://i-blog.csdnimg.cn/img_convert/a25c6fb8b82ec62804ad36960f765b80.png
图中左侧为工作区,右侧为版本库。g的版本库里存了很多东西,其中最紧张的就是暂存区。
在创建git版本库时,git会为我们主动创建一个唯一的master分支,以及指向master的一个指针叫head.(分支和head的概念后面再说)
当对工作区修改(或新增)的文件执行git add命令时,暂存区目次树的文件索引会被更新。
当执行提交操作git commit时,master分支会做相应的更新,可以简朴明白为暂存区的目次树才会被真正写到版本库中。
对象库: 除了上面所说的.git 中存在的暂存区和版本库之外, 实际上 git 仓库真正存储文件的地方在对于对象库, 内里存放的是一个个对象, 每个对象内里的内容是你每次 add/commit 提交的全部文件的修改环境. 而对于暂存区和版本库实际上都是存储的是对象中文件索引的索引而已!
从上面我们可以总结出两点:
[*]工作区的文件并不为 git 所直接管理, git 是通过 add 和 commit 的方式把工作区修改的文件以对象的情势生存到 git 当中然后联合索引的方式举行管理的!
[*]git 仓库中, 真正存储文件内容的是在对象库, 暂存库和 master 库存放的只是索引而已!
5. 场景一: 添加文件
git add filename + git commit
我们先touch一个文件, 然后给内里加两行代码.
https://i-blog.csdnimg.cn/img_convert/a4ab9edb9a3f5be1e13bdb237fb175b6.png
然后, 我们给他add一下, 然后commit一下.
https://i-blog.csdnimg.cn/img_convert/7893daf4f8ff096ad50ac25e97e6725a.png
我们可以再来写三个文件, 然后 add , commit 一下.
https://i-blog.csdnimg.cn/img_convert/9b16869c9462179e5d2c33dbcce5fc04.png
https://i-blog.csdnimg.cn/img_convert/4383491f5306aad912075a924fd52e43.png
git log 打印日志(从近到远, 以每次 commit 为单位举行打印).
https://i-blog.csdnimg.cn/img_convert/679ea75b8df783d4b4e52a64a6931c84.png
https://i-blog.csdnimg.cn/img_convert/eddedb74821a9716a6d66e2e613df5f1.png
6. 查看第一次 commit 后, .git 的变化
[*]新增了 index
https://i-blog.csdnimg.cn/direct/4446a4a18d5a445895497f5f724b3a5a.png
[*]查看 head 指针, 该指针指向 rets/heads/master.
https://i-blog.csdnimg.cn/direct/ef89fb86378f4fd6ad9f6b80226dc530.png
我们再查看一下rets/heads/master是个什么东西:
https://i-blog.csdnimg.cn/direct/901145fe7ff4482b899154e734353a97.png
这个实际上是在我们.git仓库下的一个对象:
https://i-blog.csdnimg.cn/direct/0afc2668aea9494888ce8f960bec54d5.png
[*]查看 git 对象仓库内里的内容, 不能用 githttps://i-blog.csdnimg.cn/direct/29e964159eb9443e83a91d90fadc444a.png
我们可以用 git cat-file filename 来举行查看:
https://i-blog.csdnimg.cn/direct/b43b82d1165643509e63979fc4bf6df7.png
[*]这个 tree 是啥呢? 是一个 commit 时候我们暂存区的目次结构. 打印一下看看:
https://i-blog.csdnimg.cn/direct/e1d3929719c44b54b9f655b5d9bfb50b.png
打印出来了 4 行提示, 有 file1, 有 file2, file3, 和 file4.
我们来查看一下file1和file2中的内容:
https://i-blog.csdnimg.cn/direct/65430733090547068ead52898a6337af.png
[*]所以 git 仓库是通过对象来记载修改记载的!
tree 存放的内容.
你的明白根本正确。在 Git 中,commit 提交时,会将 暂存区(索引区)的内容 生成一个 tree 对象,这个 tree 记载了本次提交对应的 目次结构、文件列表、文件权限 等信息。
具体来说:
[*]暂存区的作用:用户通过 git add 将文件添加到暂存区,暂存区生存了即将提交的内容。
[*]tree 的生成:执行 git commit 时,Git 会基于暂存区的内容生成 tree 对象,它像一个“目次快照”,记载提交时刻的文件构造情势(包罗文件、子目次及其对应哈希值)。
因此,tree 确实是 commit 时暂存区内容所对应的目次结构记载,它是 Git 实现版本管理的核心数据结构之一。
7. git 修改文件
首先先说一个修改的定义: 修改包罗新增, 删除, 修改文件内容
之后, 我们的 git 是管理文件修改的, 而非文件本身
下面举行操作演示:
让我们将 file1.txt 文件举行一次修改:https://i-blog.csdnimg.cn/direct/e7524091be754ddbbdfe8dc21632a240.png
此时,仓库中的readme和我们工作区的readme是不同的,如何查看当前仓库的状态呢?git 状态呢?git
status命令用于查看在你前次提交之后是否有对文件举行再次修改。https://i-blog.csdnimg.cn/direct/261850445e2f415ea75a94dc2b26324f.png
我们发现, 上面提示我们在暂存区没有修改, 但是在工作区有个修改是 ReadMe 文件. 那修改了啥内容呢? 我们不清楚, 因此需要查询一下:
git diff命令用来表现暂存区和工作区文件的差异,表现的格式正是unix通用的diff 格式。也可以使用git diff HEAD -- 命令来查看版本库(这里的版本库指的是暂存区)和工作区文件的区别。
https://i-blog.csdnimg.cn/direct/565a6a95ff3849579e35026ecadaf458.png
之后, 我们再把这次修改提交到版本库(暂存区)中.
https://i-blog.csdnimg.cn/direct/e8c2e12bcb6041b48c555dfc32258144.png
git add之后,就没有看到上面no changes added to commit (use "gita dd" and/or "git commit -a" )的消息了。不过他提示我们暂存区和 master 中有区别, 接下来让我们继续git commit即可:
https://i-blog.csdnimg.cn/direct/e67f91d20d2a493f8c9458092e89f692.png
小结:
git status -> 用来查看工作区 与 暂存区, 暂存区 与 master 分支哪些文件发生了修改
git diff -> 命令用来表现暂存区和工作区文件的内容差异
8. 版本回退
之前我们也提到过,gt能够管理文件的汗青版本,这也是版本控制器紧张的能力。如果有一天你发现之前前的工作做的出现了很大的问题,需要在某个特定的汗青版本重新开始,这个时候,就需要版本回退的功能了。
执行gitreset命令用于回退版本,可以指定退回某一次提交的版本。要解释一下"回退"本质是要将版本库中的内容举行回退,工作区或暂存区是否回退由命令参数决定。
git reset命令语法格式为:git reset [--soft | --mixed | --hard]
[*]--mixed
[*]
[*]为默认选项,使用时可以不消带该参数。该参数将暂存区的内容退回到指定提交版本内容,工作区文件保持不变。
[*]--soft
[*]
[*]参数对于工作区和暂存区的内容都不变,只是将版本库回退到某个指定版本。
[*]--hard
[*]
[*]参数将暂存区与工作区都退回到指定版本。切记工作区有未提交的代码时不要用这个命令,由于工作区会回滚,你没有提交的代码就再也找不回来了,所以使用该参数前肯定要慎重。
[*]HEAD说明:
[*]
[*]可直接写成commit id,表示指定退回的版本
[*]HEAD表示当前版本
[*]HEAD^上一个版本
[*]HEAD^^上上一个版本
[*]以此类推...
[*]可以使用~数字表示:
[*]
[*]
[*]HEAD~0表示当前版本
[*]HEAD~1上一个版本
[*]HEAD^2上上一个版本
[*]以此类推...
操作演示详解
为了方便测试回退功能, 我们下面先提交三个版本, 然后举行回退操作:
[*]第一次修改提交
[*]
[*]查看ReadMe文件内容:
hyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1
[*]
[*]添加ReadMe文件到暂存区:
hyb@139-159-150-152:~/gitcode$ git add ReadMe
[*]
[*]提交修改:
hyb@139-159-150-152:~/gitcode$ git commit -m"add version1"
add version1
1 file changed, 1 insertion(+)
[*]第二次修改提交
[*]
[*]再次查看ReadMe文件内容:
hyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2
[*]
[*]添加ReadMe文件到暂存区:
hyb@139-159-150-152:~/gitcode$ git add ReadMe
[*]
[*]提交修改:
hyb@139-159-150-152:~/gitcode$ git commit -m"add version2"
add version2
1 file changed, 1 insertion(+)
[*]第三次修改提交
[*]
[*]再次查看ReadMe文件内容:
hyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3
[*]
[*]添加ReadMe文件到暂存区:
hyb@139-159-150-152:~/gitcode$ git add ReadMe
[*]
[*]提交修改:
hyb@139-159-150-152:~/gitcode$ git commit -m"add version3"
add version3
1 file changed, 1 insertion(+)
[*]查看汗青提交记载
[*]
[*]使用git log查看提交记载:
hyb@139-159-150-152:~/gitcode$ git log --pretty=oneline # 打印日志
d95c13ffc87a855a25a3d04e22abfc7d2e31383 (HEAD -> master) add version3
14c12c32464d6ead7159f5c24e786ce450c89ddd add version2
cff9d1e01933318156f8c7d356a78c9e949ae6b7 add version1
现在,如果我们在提交完version3后,发现version3编写错误,想回退到version2,重新基于version2开始编写。由于我们在这里希望的是将工作区的内容也回退到version 2版本,所以需要用到--hard参数,示例如下:
[*]执行git reset命令
[*]
[*]使用--hard参数将工作区回退到version2版本:
hyb@139-159-150-152:~/gitcode$ git reset --hard 14c12c32464d6ead7159f5c24e786ce450c89ddd
[*]
[*]此时HEAD指向add version2:
HEAD is now at 14c12c3 add version2
[*]查看ReadMe文件内容
[*]
[*]查看ReadMe文件,发现内容已经回退到version2:
hyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2
[*]查看提交日志
[*]
[*]使用git log查看提交日志,发现HEAD指向version2:
hyb@139-159-150-152:~/gitcode$ git log --pretty=oneline
14c12c32464d6ead7159f5c24e786ce450c89ddd (HEAD -> master) add version2
cff9d1e01933318156f8c7d356a78c9e949ae6b7 add version1 文中还提到,惊奇地发现ReadMe文件的内容已经回退到version2,并且HEAD也指向了version2。
不过, 我突然又想归去了, 那有机会归去吗? 如果你执行的 git 操作比较少的话, 是有机会归去的, 但是如果在此底子上你又执行了大量的 git 操作, 就很难回到之前的版本了. 由于我们上面完全没有执行任何操作, 所以可以归去的, 如何归去看下面演示即可.
以下是提取的代码和笔墨:
到这里一般回退功能就演示完了,但现在如果我悔恨了,想再回到version3怎么办?我们可以继续使用git reset命令,回退到version 3版本,但我们必须要拿到version 3的commit id去指定回退的版本。
但我们看到了git log并不能打印出version 3的commit id,运气好的话我们可以从终端上去找找之前的记载,运气欠好的话commit id已经被我们搞丢了=。=
Git还提供了一个git reflog命令能调停一下,该命令用来记载当地的每一次操作。
代码部分:
[*]使用git reflog查看操作记载:
hyb@139-159-150-152:~/gitcode$ git reflog
14c12c32464d6ead7159f5c24e786ce450c89ddd (HEAD -> master) HEAD@{0}: reset: moving to 14c12c32464d6ead7159f5c24e786ce450c89ddd
d95c13ffc87a855a25a3d04e22abfc7d2e31383 HEAD@{1}: commit: add version3
14c12c32464d6ead7159f5c24e786ce450c89ddd (HEAD -> master) HEAD@{2}: commit: add version2
cff9d1e01933318156f8c7d356a78c9e949ae6b7 HEAD@{3}: commit: add version1
94da6950d27e26230368b22f1ffc4bbf761b5b00 HEAD@{4}: commit: add Modify ReadMe file
23807c5693698cd868c4fb264997ca575756eed HEAD@{5}: commit: add 3 files
c61428926f38534d4ec6dde9044150be6c1abc6c HEAD@{6}: commit (initial): commit my first file
[*]回退到version3:
# 回退到v3
hyb@139-159-150-152:~/gitcode$ git reset --hard d95c13f
HEAD is now at d95c13f add version3
[*]查看工作区:
# 查看工作区hyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3
[*]查看log:
# 查看log
hyb@139-159-150-152:~/gitcode$ git log --pretty=oneline
d95c13ffc87a855a25a3d04e22abfc7d2e31383 (HEAD -> master) add version3
14c12c32464d6ead7159f5c24e786ce450c89ddd add version2
cff9d1e01933318156f8c7d356a78c9e949ae6b7 add version1
git 为什么版本更换非常快? 几乎不需要时间? 这与他的设计结构有关, 由于 git 是记载的是修改本身, 而非文件本身:
https://i-blog.csdnimg.cn/img_convert/9642c6bb1785dd412e9effaaa96e0b8f.png
9. 撤销修改
9.1. 环境一:没有add,也没有commit
命令: git checkout --
情景举例
# 向ReadMe中新增一行代码hyb@139-159-150-152:~/gitcode$ git statusOn branch masterNothing to commit, working tree cleanhyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3This piece of code is like shit #新增代码hyb@139-159-150-152:~/gitcode$ git statusOn branch masterChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory) modified: ReadMeno changes added to commit (use "git add" and/or "git commit -a")# 直接删除代码hyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3hyb@139-159-150-152:~/gitcode$ git statusOn branch masternothing to commit, working tree clean 幸亏我们工作服从不高,才写了一行代码就发现不行了,要是你写了3天,不绝都没有提交,该怎么删掉呢?你本身都忘了本身新增过哪些,有同学说,我可以git diff xxx一下,看看差别在哪啊,那你肯定又要花3天时间删代码了,并且很大的概率还会改错。一周过去了,你怎么向你的老板交代呢?
Git其实还为我们提供了更好的方式,我们可以使用git checkout -- 命令让工作区的文件回到迩来一次add或commit时的状态。要留意git checkout -- 命令中的--很紧张,切记不要省略,一旦省略,该命令就变为其他意思了,后面我们再说。示例如下:
# 向ReadMe中新增一行代码hyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3This piece of code is like shit #新增代码hyb@139-159-150-152:~/gitcode$ git statusOn branch masterChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory) modified: ReadMeno changes added to commit (use "git add" and/or "git commit -a")# 规复到上一次add或commithyb@139-159-150-152:~/gitcode$ git checkout -- ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3 9.2. 环境二:已经add,但没有commit
add 之后生存到了暂存区, 如何撤销呢?
我们下面来举个例子:
# 向ReadMe中新增一行代码hyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3This piece of code is like shit #新增代码# add 存入暂存区hyb@139-159-150-152:~/gitcode$ git add ReadMehyb@139-159-150-152:~/gitcode$ git statusOn branch masterChanges to be committed:(use "git restore --staged <file>..." to unstage) modified: ReadMe 让我们往返想一下学过的`git reset`回退命令,该命令如果使用`--mixed`参数,可以将暂存区的内容退回到指定的版本内容,但工作区文件保持不变。那我们就可以回退下暂存区的内容了!!!
以下是提取的代码和笔墨:
# --mixed是默认参数,使用时可以省略
hyb@139-159-150-152:~/gitcode$ git reset HEAD ReadMe
Unstaged changes after reset:
MReadMe
# 用 git status 查看一下,发现现在暂存区是干净的,工作区有修改。
# 查看状态
hyb@139-159-150-152:~/gitcode$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: ReadMe
no changes added to commit (use "git add" and/or "git commit -a") 还记得如何丢弃工作区的代码吗?
hyb@139-159-150-152:~/gitcode$ git checkout -- ReadMehyb@139-159-150-152:~/gitcode$ git statusOn branch masternothing to commit, working tree cleanhyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3 9.3. 已经 add 并且也已经 commit 了
不要担心,我们可以`git reset --hard HEAD^`回退到上一个版本!不过,这是有条件的,就是你还没有把本身的当地版本库推送到远程。还记得Git是分布式版本控制体系吗?我们后面会讲到远程版本库,一旦你推送到远程版本库,你就真的惨了……
# 向ReadMe中新增一行代码hyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3This piece of code is like shit #新增代码# 提交hyb@139-159-150-152:~/gitcode$ git add ReadMehyb@139-159-150-152:~/gitcode$ git commit -m"test quash" test quash1 file changed, 1 insertion(+)# 回退hyb@139-159-150-152:~/gitcode$ git reset --hard HEAD^HEAD is now at 144a3f8 add filehyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
hello version1hello version2hello version3 10. 删除文件
https://i-blog.csdnimg.cn/img_convert/2efa8b8587bb99ae4d0be83fc2321e21.png
git 中,删除也是一个修改操作,我们实战一下,如果要删除file5文件,怎么搞呢?如果你这样做故了:
hyb@139-159-150-152:~/gitcode$ ls
file1 file2 file3 file4 file5 ReadMe
hyb@139-159-150-152:~/gitcode$ rm file5 但这样直接删除是没有用的,反而徒增烦恼,git status命令会立刻告诉你哪些文件被删除了:
hyb@139-159-150-152:~/gitcode$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: file5
no changes added to commit (use "git add" and/or "git commit -a") 此时,工作区和版本库就不同等了,要删文件,目前除了要删工作区的文件,还要扫除版本库的文件。
一般走到这里,有两种可能:
[*]确实要从版本库中删除该文件
[*]不小心删错了
对第二种环境,很显着误删,需要使用git来举行规复,很简朴,我们刚学过(删除也是修改):
hyb@139-159-150-152:~/gitcode$ git checkout -- file5
hyb@139-159-150-152:~/gitcode$ ls
file1 file2 file3 file4 file5 ReadMe 对于第一种环境,很显着是没有删完,我们只删除了工作区的文件。这时就需要使用git rm将文件从暂存区和工作区中删除,并且commit:
hyb@139-159-150-152:~/gitcode$ git rm file5
rm 'file5'
hyb@139-159-150-152:~/gitcode$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: file5
hyb@139-159-150-152:~/gitcode$ git commit -m"deleted file5"
deleted file5
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 file5
hyb@139-159-150-152:~/gitcode$ git status
On branch master
nothing to commit, working tree clean 现在,文件就从版本库中被删除了。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]