2024年循序渐进学Git(可复习)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xsyoMgPb-1662881503516)(C:\Users\hasee\Pictures\Git\查看状态.png)]

新增文件操作

vim hello.txt
#i 进入编辑模式
hello world
#wq保存并退出

再次查看本地库状态(检测到未追踪的文件)

在这里插入图片描述

④添加暂缓区

基本语法:

  • git add 文件名称

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

$ 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 "日志信息" 文件名

在这里插入图片描述

#没有文件需要提交
hasee@hasee MINGW64 /f/Git-space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean

下一段实操案例:

#1.修改文件
vim hello.txt
hello world 1111 #追加内容

#2.查看状态(检测到工作区有文件被修改)
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

#3.将修改的文件再次添加到暂缓区
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.

#4.查看状态(修改的文件已被提交到暂缓区
git status
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
 modified: hello.txt

2.2 版本相关操作

基本语法:

  • git reflog 查看版本信息
  • git log 查看版本详细信
  • git reset --hard 版本号 切换版本
#实操
git reflog #查看历史版本记录
3edd753 HEAD@{1}: commit: seconed time #3edd753 唯一版本标识号 
a6e4928 (HEAD -> master) HEAD@{2}: commit (initial): This is my first commit file

#2.切换版本号
git reset --hard a6e4928
HEAD is now at a6e4928 This is my first commit file #HEAD指向a6e4928,提交信息--;

#3.再次查看版本信息 已移动至a6e4928该版本
git reflog
a6e4928 (HEAD -> master) HEAD@{0}: reset: moving to a6e4928 #重点看这
3edd753 HEAD@{1}: commit: seconed time
a6e4928 (HEAD -> master) HEAD@{2}: commit (initial): This is my first commit file

#4.可以查看该版本的文件有何不同
cat hello.txt #1.0版本

Git切换版本,底层其实就是移动head指针,简单描述: head --> master --> first

三、分支

3.1 什么是分支?

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

3.2 分支的好处?

同时并行推进多个功能开发,提高开发效率。 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。

3.3分支的操作

命令名称作用
git branch 分支名创建分支
git branch -v查看分支
git checkout 分支名切换分支
git merge 分支名把指定的分支合并到当前分支上

案例实操

1) 查看分支

基本命令:

  • git branch -v

在这里插入图片描述

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

2) 创建分支

基本命令:

  • git branch 分支名
    在这里插入图片描述
hasee@hasee MINGW64 /f/Git-space/git-demo (master)
$ git branch demoBranch #创建分支

hasee@hasee MINGW64 /f/Git-space/git-demo (master)
$ git branch -v #查看分支
  demoBranch a6e4928 This is my first commit file #刚创建的分支 并将主分支master的内容复制了一份
* master     a6e4928 This is my first commit file

3) 修改分支

#在master分支下操作
vim hello.txt
#2.提交暂缓区
git add hello.txt
#3.提交本地库
git commit -m "GG" hello.txt
#4.查看分支
git branch -v
#运行结果
  demoBranch a6e4928 This is my first commit file #没有改变
* master     4a8df47 GG #当前master分支已更新为最新一次提交的版本
#查看master分支的文件
cat hello.txt

3.4分支的合并

基本语法:

  • git merge 分支名称

实操:在master分支上合并demoBranch

在这里插入图片描述

hasee@hasee MINGW64 /f/Git-space/git-demo (demoBranch)
$ git checkout master  #切换分支
Switched to branch 'master'

hasee@hasee MINGW64 /f/Git-space/git-demo (master)
$ cat hello.txt #首先查看master分支下的文件内容
hello world
hello 0914

hasee@hasee MINGW64 /f/Git-space/git-demo (master)
$ git merge demoBranch  #在Master分支下进行分支合并
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt #此时产生合并冲突
Automatic merge failed; fix conflicts and then commit the result.

hasee@hasee MINGW64 /f/Git-space/git-demo (master|MERGING)
$ cat hello.txt
hello world
<<<<<<< HEAD
hello 0914
=======
hello I'am demoBranch
>>>>>>> demoBranch

冲突产生:形式上分支后跟MERGING,其原因是合并分支时,两个分支在同一个文件的同一个位置有两个完全不相同的修改,也就是内容,Git无法决定。必须人为确定

首先,进行查看状态,可以看到文件有两处修改

hasee@hasee MINGW64 /f/Git-space/git-demo (master|MERGING)
$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   hello.txt

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

解决冲突问题:编辑有冲突的符号,删除特殊符号,决定要修改的内容。

特殊符号:<<<<<<< HEAD 当前分支的代码 ======= 合并过来的代码 >>>>>>> demoBranch

hello world
hello 0914
hello I'am demoBranch

#2.添加暂缓区


![img](https://img-blog.csdnimg.cn/img_convert/749224afdef5c630f7d695d79f9e3147.png)
![img](https://img-blog.csdnimg.cn/img_convert/c6092f70bb5ded6f6545bde633cec5c7.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**


[外链图片转存中...(img-ZyFFDUn5-1714665133600)]
[外链图片转存中...(img-kNJr8SKP-1714665133601)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值