练习实践-git工具-id:2-git 仓库部分的基础命令

参考来源:
csdn技能树-git
https://fishc.com.cn/forum-334-1.html fishc论坛扩展阅读-git实用教程

git分支的实现原理理解–三棵树-工作、缓存、仓库

在这里插入图片描述

1.创建一个新文件license,修改readme.txt文件内容,之后查看git状态信息

F:\tmp\learning-git>echo "This is a license." >LICENSE

F:\tmp\learning-git>echo "Git has a mutable index called stage." >>readme.txt

F:\tmp\learning-git>dir
 驱动器 F 中的卷是 tmp
 卷的序列号是 0800-B859
 F:\tmp\learning-git 的目录
24/08/28  上午 12:47    <DIR>          .
24/08/28  上午 12:47    <DIR>          ..
24/08/28  上午 12:47                23 LICENSE
24/08/28  上午 12:48               125 readme.txt
               2 个文件            148 字节
               2 个目录 29,917,978,624 可用字节
F:\tmp\learning-git>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.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        LICENSE

no changes added to commit (use "git add" and/or "git commit -a")

F:\tmp\learning-git>

2.添加文件,查询git状态,此时文件就放在了git版本库的暂存区;

F:\tmp\learning-git>git add readme.txt LICENSE

F:\tmp\learning-git>git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   LICENSE
        modified:   readme.txt

F:\tmp\learning-git>

3.将暂存区内容提交,然后查询git状态,此时工作区是干净的(working tree clean)

F:\tmp\learning-git>git commit -m "understand how stage works"
[master 03dee38] understand how stage works
 2 files changed, 2 insertions(+)
 create mode 100644 LICENSE

F:\tmp\learning-git>git status
On branch master
nothing to commit, working tree clean
F:\tmp\learning-git>

撤销回退和删除的基本操作

撤消修改(git commit --amend,后悔药)

有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。 此时,可以运行带有 --amend 选项的提交命令来重新提交:
git commit --amend
示例:

$ git commit -m 'initial commit'
$ git add forgotten_file  #遗漏的文件添加上
$ git commit --amend  #再次提交
F:\tmp\learning-git>echo "forgotten_file " >forgotten_file #另一个待提交的文件

F:\tmp\learning-git>type readme.txt
"switch type"

F:\tmp\learning-git>echo "add a line." >>readme.txt  #修改文件内容

F:\tmp\learning-git>git add readme.txt  #只添加了一个修改文件,遗漏了forgotten_file

F:\tmp\learning-git>git commit -m "initial commit"  #进行了两步提交操作,添加了提交备注
[dev 14b63f3] initial commit
 1 file changed, 1 insertion(+)

F:\tmp\learning-git>git add forgotten_file  #补上遗漏的文件

F:\tmp\learning-git>git commit --amend  #再次提交
[dev 7559bf1] initial commit
 Date: Tue Sep 3 23:05:07 2024 +0800
 2 files changed, 2 insertions(+)
 create mode 100644 forgotten_file

F:\tmp\learning-git>

最终你只会有一个提交——第二次提交将代替第一次提交的结果。
当你在修补最后的提交时,并不是通过用改进后的提交 原位替换 掉旧有提交的方式来修复的, 理解这一点非常重要。从效果上来说,就像是旧有的提交从未存在过一样,它并不会出现在仓库的历史中。

修补提交最明显的价值是可以稍微改进你最后的提交,而不会让“啊,忘了添加一个文件”或者 “小修补,修正笔误”这种提交信息弄乱你的仓库历史

F:\tmp\learning-git>git log --graph --pretty=oneline --abbrev-commit  
* 7559bf1 (HEAD -> dev) initial commit
* af37860 add merge
* 2e4c9e1 add merge
* db8995a branch test
* 1452a8f understand how stage works
* 02d503a add distributed
* b75599d wrote a readme file

删除文件–两个场景(确认删除/误操作,已后悔)

在 Git 中,删除也是一个修改操作,我们先添加一个新文件test.txt到 Git 并且提交

F:\tmp\learning-git>echo "This is a test_file." >test.txt   #创建一个test.txt文件
F:\tmp\learning-git>dir
 驱动器 F 中的卷是 tmp
 卷的序列号是 0800-B859
 F:\tmp\learning-git 的目录

24/09/02  下午 11:16    <DIR>          .
24/09/02  下午 11:16    <DIR>          ..
24/08/28  上午 12:47                23 LICENSE
24/08/28  上午 12:48               125 readme.txt
24/09/02  下午 11:16                25 test.txt
               3 个文件            173 字节
               2 个目录 30,000,906,240 可用字节

F:\tmp\learning-git>git add test.txt   #提交到git缓存区

F:\tmp\learning-git>git status  #查看提交状态
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt

一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:

F:\tmp\learning-git>del test.txt

F:\tmp\learning-git>dir
 驱动器 F 中的卷是 tmp
 卷的序列号是 0800-B859

 F:\tmp\learning-git 的目录

24/09/02  下午 11:22    <DIR>          .
24/09/02  下午 11:22    <DIR>          ..
24/08/28  上午 12:47                23 LICENSE
24/08/28  上午 12:48               125 readme.txt
               2 个文件            148 字节
               2 个目录 30,000,906,240 可用字节

这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:

F:\tmp\learning-git>git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt

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:    test.txt

现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit:

F:\tmp\learning-git>git rm test.txt
rm 'test.txt'

F:\tmp\learning-git>git status
On branch master
nothing to commit, working tree clean

另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:

分支基本操作–创建、切换、合并、删除

通过checkout命令加参数创建一个新分支dev

F:\tmp\learning-git>git checkout -b dev
Switched to a new branch 'dev'

更新readme.txt文件内容并提交

F:\tmp\learning-git>dir
 驱动器 F 中的卷是 tmp
 卷的序列号是 0800-B859

 F:\tmp\learning-git 的目录

24/09/02  下午 11:31    <DIR>          .
24/09/02  下午 11:31    <DIR>          ..
24/08/28  上午 12:47                23 LICENSE
24/08/28  上午 12:48               125 readme.txt
               2 个文件            148 字节
               2 个目录 30,000,906,240 可用字节

F:\tmp\learning-git>echo "Creating a new branch is quick." >>readme.txt

F:\tmp\learning-git>git add readme.txt

F:\tmp\learning-git>git commit -m "branch test"
[dev db8995a] branch test
 1 file changed, 1 insertion(+)

切换到主节点master上,发现刚才提交的内容未显示,符合预期

F:\tmp\learning-git>git checkout master
Switched to branch 'master'

F:\tmp\learning-git>type readme.txt
"Git is a version control system."
"\nGit is free software."
"add new line."
"Git has a mutable index called stage."

F:\tmp\learning-git>

将dev分支提交内容合并到master中的操作,然后查看文件内容已同步更新,注意到此次合并的方式为快速合并Fast-forward,

F:\tmp\learning-git>git merge dev
Updating 1452a8f..db8995a
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

F:\tmp\learning-git>type readme.txt
"Git is a version control system."
"\nGit is free software."
"add new line."
"Git has a mutable index called stage."
"Creating a new branch is quick."

F:\tmp\learning-git>

删除分支dev,并且查看分支情况确认删除效果,*号为当前位置

F:\tmp\learning-git>git branch -d dev   
Deleted branch dev (was db8995a).

F:\tmp\learning-git>git branch
* master

F:\tmp\learning-git>

切换分支的另一种方式switch

通过git switch 创建分支,并且切换,更容易理解操作意图

F:\tmp\learning-git>git switch -c dev
Switched to a new branch 'dev'

F:\tmp\learning-git>git branch
* dev
  master

F:\tmp\learning-git>git switch master
Switched to branch 'master'

F:\tmp\learning-git>git branch
  dev
* master

F:\tmp\learning-git>

分支管理策略–合并方式

创建一个dev分支,并在分支下通过两步提交一个内容,作为测试基础

F:\tmp\learning-git>git switch -c dev
Switched to a new branch 'dev'

F:\tmp\learning-git>echo "switch type" >>readme.txt

F:\tmp\learning-git>git add readme.txt

F:\tmp\learning-git>git commit -m "add merge"
[dev af37860] add merge
 1 file changed, 1 insertion(+), 6 deletions(-)

F:\tmp\learning-git>git switch master
Switched to branch 'master'

通过–no-ff的方式合并,并且查看对应日志内容(过滤输出)

F:\tmp\learning-git>git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'ort' strategy.
 readme.txt | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

F:\tmp\learning-git>git log --graph --pretty=oneline --abbrev-commit
*   765e52f (HEAD -> master) merge with no-ff
|\
| * af37860 (dev) add merge
|/
* 2e4c9e1 add merge
* db8995a branch test
* 1452a8f understand how stage works
* 02d503a add distributed
* b75599d wrote a readme file

F:\tmp\learning-git>

分支管理的几个原则

首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;

其次,干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时,再把dev分支合并到master上,并在master分支发布1.0版本;

你和你的小伙伴们每个人都在dev分支上干活,每个人都有自己的分支,时不时地往dev分支上合并就可以了。

所以,团队合作的分支看起来就像这样:
在这里插入图片描述

日常开发中的分支

常设分支就主分支(master)和开发分支(develop)两个即可,另外的功能分支(feature)、预发布分支(release)和维护分支(hotfix)属于临时分支,用完之后应该及时删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值