Git分支操作常见使用场景 git新建分支
ztj100 2024-12-23 14:52 12 浏览 0 评论
一、删除不需要的分支。
1,现在my-pro项目下,新建若干用于测试的分支。
git checkout master #将HEAD指向master分支
git branch testdel #在master分之下新建分支
git checkout testdel #将HEAD指向testdel分支
#在这里修改一下style.css,例如加一个背景色background:#d8d8d8
git commit css/style.css -m'just a test commit' #在testdel分之下执行一个commit操作
git branch testagain
?
git checkout master
git branch anothertest
gitk --all #使用gitk可视化查看当前分支状况
经过多次分支新建及commit操作之后,我们的分支变成了如下图的结构:
2,使用git branch -d 【分支名称】命令,删除某个指定分支。
Yooye-2:my-pro yooye$ git branch -av #查看当前分支状态
anothertest dc7ecf7 let index.html link the style file
change-border-color 413552e change the box border-color
* master dc7ecf7 let index.html link the style file
testagain e1e471a just a test
testdel e1e471a just a test
Yooye-2:my-pro yooye$ git checkout master #进入master分支
Switched to branch 'master'
Yooye-2:my-pro yooye$ git branch -d testagain #使用-d删除某个分支
error: The branch 'testagain' is not fully merged.
If you are sure you want to delete it, run 'git branch -D testagain'. #提示我们使用-D进行删除
Yooye-2:my-pro yooye$ git branch -D testagain #使用-D再次尝试删除某个分支
Deleted branch testagain (was e1e471a). #删除成功
Yooye-2:my-pro yooye$ gitk --all #查看删除后的可视化分支状态
删除testagain分之后的状态如下:
3,当我们使用git checkout在testdel与master分支间切换的时候,项目中的style.css等代码文件,会随之切换。如果删除testdel分支,则在该分支下提交的所有commit操作记录将全被移除。所以我们在删除分支的之前一定要做版本确认。
二、修改最近一次commit的描述内容
使用 git commit --amend命令,修改最近一次提交的commit的描述性文字。操作流程如下:
Yooye-2:my-pro yooye$ git checkout testdel #【1】进入testdel分支
Switched to branch 'testdel'
?
Yooye-2:my-pro yooye$ git log -1 #【2】查看最近一次的提交记录,注意这里是git log -[数字1]
commit e1e471a5945ab83673bc56055e3455e9fbec8e61 (HEAD -> testdel)
Author: 卢大湿 <dashi@vip.com>
Date: Fri Mar 1 10:26:49 2019 +0800
just a test # 【2-1】这是修改之前的描述文字
Yooye-2:my-pro yooye$ git commit --amend # 【3】执行修改命令,进入修改状态,详细操作看下面第【4】步
?
Yooye-2:my-pro yooye$ git log -1 #【5】查看确定是否修改成功
commit 1062fa43b85ad7111738d4eaee56436279768757 (HEAD -> testdel)
Author: 卢大湿 <dashi@vip.com>
Date: Fri Mar 1 10:26:49 2019 +0800
just a test and i had change this words 【5-1】修改后的描述文字
Yooye-2:my-pro yooye$
【4】执行git commit --amend修改命令,修改的方式如下图:
三、修改旧的commit描述信息
使用 git rebase -i [被修改的父级commit的唯一id] 命令进入变基操作。
【注意】:上面使用的是父级commit进入变基操作,进去操作的是父级下面的commit。
1,因为rebase状态下可以支持多种操作,这里我们要将需要修改的commit前面默认pick操作方式改为reword(也就是修改的意思)。然后按照第二步中一样的方式保存退出,进入下一个操作界面。
2,这个操作界面,可以将原本commit的描述内容进行修改,如下图第一行所示。然后继续保存退出。
3,使用git log --graph 查看修改后的结果,如下图:
四、合并多个连续的commit动作
根据上一步git log --graph的结果可以看出,其实上面有三个commit都是为了给让index.html以正常的表现展示出来,所以我们可以将除了readme操作之外的其他三个commit进行合并。
1,使用git rebase -i [最开始创建readme的commit唯一id]命令,进入操作界面。将这三个commit合并至其中某一个,然后保存退出进入下一步。
pick 789f15c add a index.html file #【1】表示将其他commit合并至这个commit
squash e7bf7b0 add aaaaaaa style file #【2】合并
squash 4216faf let index.html link the style file #【3】合并
?
# Rebase 1bb42bc..4216faf onto 1bb42bc (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
#
# Note that empty commits are commented out
~
:wq!
2,填写此次合并的描述文字,并保留原commit的描述文字,方便后续查看。
# This is the 1st commit messages:
?
I am trying to make a combination, Yeah! #【1】此次合并操作的描述文字
?
add a index.html file # 【2-1】保留的原有commit描述文字
# This is the commit message #2:
?
add aaaaaaa style file # 【2-2】保留的原有commit描述文字
?
# This is the commit message #3:
?
let index.html link the style file # 【2-3】保留的原有commit描述文字
?
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Feb 27 11:42:12 2019 +0800
#
# interactive rebase in progress; onto 1bb42bc
# Last commands done (3 commands done):
# squash e7bf7b0 add aaaaaaa style file
# squash 4216faf let index.html link the style file
# No commands remaining.
# You are currently rebasing branch 'anothertest' on '1bb42bc'.
#
# Changes to be committed:
# new file: css/style.css
# new file: index.html
#
# Untracked files:
# js/
#
:wq!
3,使用git log --graph 查看合并后的版本状态。
五、合并不连续commit
1,基于上一步,为了演示我们将其他测试分支全部移除,只保留原本的master分支,及与index.html、style.css、readme.md相关的三个commit。
之后我们修改my-pro下的readme.md文件内容,并执行一次commit操作。然后使用git log --graph命令,查看当前的commit状态列表。
2,因为最前面跟最后面的commit都是关于readme的提交,所以我们需要将其合并。在执行git rebase -i [最下面的祖先commit]之前,记得复制改祖先commit的id(如:1bb42bc),以备使用。
进入操作窗口后,按照下面代码所示进行操作保存退出。
pick 1bb42bc8 #【1】这一句是我们自己新增的,就是将另一个操作readme的commit操作合并过来
squash 702dcbf I am the real readme.md file # 【2】这一行本来在最下面,我们移动到这一样,并修改pick至squash
pick 2b2e247 I am trying to make a combination, Yeah!
?
# Rebase 1bb42bc..702dcbf onto 1bb42bc (2 commands)
#
# Commands:这里本来有很多command提示,被删除了
# These lines can be re-ordered; they are executed from top to bottom.
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
:wq!
3,如果保存退出后提醒分支不连续,我们可以使用git rebase --continue继续执行下一步操作。
六、比较暂存区与HEAD所含文件的差异
1,修改my-pro仓库中index.html的内容。
2,执行一次git add 操作。
3,使用 git diff -cached 查看暂存区与HEAD所含文件差异。
Yooye-2:my-pro yooye$ git diff --cached #【1】执行对比命令
diff --git a/index.html b/index.html
index 084901f..ec530c2 100644
--- a/index.html
+++ b/index.html
@@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/style.css">
- <title>Document</title> #【2-1】未改动之前HEAD指向的旧内容
+ <title>Learn Git</title> #【2-2】改动后通过git add新增到暂存区的内容
</head>
<body>
<div class="box"></div>
七、比较工作区与暂存区所含文件差异
1,基于上一步操作,我们再调整一下style.css文件(如:添加个背景),然后先不执行git add操作,这个时候,我们刚才编辑的style.css文件的改变就属于工作区。
2,执行git diff命令,查看工作区与之前提交的暂存区文件区别。
Yooye-2:my-pro yooye$ git diff
diff --git a/css/style.css b/css/style.css
index 20ce14c..488853c 100644
--- a/css/style.css
+++ b/css/style.css
@@ -2,4 +2,5 @@
width: 100px;
height: 100px;
border: 1px solid red;
+ background: pink;
}
\ No newline at end of file
Yooye-2:my-pro yooye$
3,如果再提交至暂存区前,修改了多个文件,然而我们只想查看某个文件的变动,例如readme.md,可以使用如下命令:
git diff -- readme.md
八、将暂存区中的内容恢复成与HEAD一样
使用场景,我们在第六步,将index.html修改后,通过git add提交到了暂存区,如果这个时候我们反悔了,就可以使用 git reset HEAD 命令,将其恢复到与HEAD一样。测试流程如下:
Yooye-2:my-pro yooye$ git status # 【1】在恢复之前,先查看commit状态
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
?
modified: index.html #【2-1】已经在暂存区
?
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: css/style.css #【2-2】未在暂存区
modified: readme.md
?
Yooye-2:my-pro yooye$ git reset HEAD #【3】恢复暂存区内容
Unstaged changes after reset:
M css/style.css
M index.html
M readme.md
Yooye-2:my-pro yooye$ 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: css/style.css #【4】未在暂存区
modified: index.html
modified: readme.md
?
no changes added to commit (use "git add" and/or "git commit -a")
相关推荐
- Vue 技术栈(全家桶)(vue technology)
-
Vue技术栈(全家桶)尚硅谷前端研究院第1章:Vue核心Vue简介官网英文官网:https://vuejs.org/中文官网:https://cn.vuejs.org/...
- vue 基础- nextTick 的使用场景(vue的nexttick这个方法有什么用)
-
前言《vue基础》系列是再次回炉vue记的笔记,除了官网那部分知识点外,还会加入自己的一些理解。(里面会有部分和官网相同的文案,有经验的同学择感兴趣的阅读)在开发时,是不是遇到过这样的场景,响应...
- vue3 组件初始化流程(vue组件初始化顺序)
-
学习完成响应式系统后,咋们来看看vue3组件的初始化流程既然是看vue组件的初始化流程,咋们先来创建基本的代码,跑跑流程(在app.vue中写入以下内容,来跑流程)...
- vue3优雅的设置element-plus的table自动滚动到底部
-
场景我是需要在table最后添加一行数据,然后把滚动条滚动到最后。查网上的解决方案都是读取html结构,暴力的去获取,虽能解决问题,但是不喜欢这种打补丁的解决方案,我想着官方应该有相关的定义,于是就去...
- Vue3为什么推荐使用ref而不是reactive
-
为什么推荐使用ref而不是reactivereactive本身具有很大局限性导致使用过程需要额外注意,如果忽视这些问题将对开发造成不小的麻烦;ref更像是vue2时代optionapi的data的替...
- 9、echarts 在 vue 中怎么引用?(必会)
-
首先我们初始化一个vue项目,执行vueinitwebpackechart,接着我们进入初始化的项目下。安装echarts,npminstallecharts-S//或...
- 无所不能,将 Vue 渲染到嵌入式液晶屏
-
该文章转载自公众号@前端时刻,https://mp.weixin.qq.com/s/WDHW36zhfNFVFVv4jO2vrA前言...
- vue-element-admin 增删改查(五)(vue-element-admin怎么用)
-
此篇幅比较长,涉及到的小知识点也比较多,一定要耐心看完,记住学东西没有耐心可不行!!!一、添加和修改注:添加和编辑用到了同一个组件,也就是此篇文章你能学会如何封装组件及引用组件;第二能学会async和...
- 最全的 Vue 面试题+详解答案(vue面试题知识点大全)
-
前言本文整理了...
- 基于 vue3.0 桌面端朋友圈/登录验证+60s倒计时
-
今天给大家分享的是Vue3聊天实例中的朋友圈的实现及登录验证和倒计时操作。先上效果图这个是最新开发的vue3.x网页端聊天项目中的朋友圈模块。用到了ElementPlus...
- 不来看看这些 VUE 的生命周期钩子函数?| 原力计划
-
作者|huangfuyk责编|王晓曼出品|CSDN博客VUE的生命周期钩子函数:就是指在一个组件从创建到销毁的过程自动执行的函数,包含组件的变化。可以分为:创建、挂载、更新、销毁四个模块...
- Vue3.5正式上线,父传子props用法更丝滑简洁
-
前言Vue3.5在2024-09-03正式上线,目前在Vue官网显最新版本已经是Vue3.5,其中主要包含了几个小改动,我留意到日常最常用的改动就是props了,肯定是用Vue3的人必用的,所以针对性...
- Vue 3 生命周期完整指南(vue生命周期及使用)
-
Vue2和Vue3中的生命周期钩子的工作方式非常相似,我们仍然可以访问相同的钩子,也希望将它们能用于相同的场景。...
- 救命!这 10 个 Vue3 技巧藏太深了!性能翻倍 + 摸鱼神器全揭秘
-
前端打工人集合!是不是经常遇到这些崩溃瞬间:Vue3项目越写越卡,组件通信像走迷宫,复杂逻辑写得脑壳疼?别慌!作为在一线摸爬滚打多年的老前端,今天直接甩出10个超实用的Vue3实战技巧,手把...
- 怎么在 vue 中使用 form 清除校验状态?
-
在Vue中使用表单验证时,经常需要清除表单的校验状态。下面我将介绍一些方法来清除表单的校验状态。1.使用this.$refs...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- Vue 技术栈(全家桶)(vue technology)
- vue 基础- nextTick 的使用场景(vue的nexttick这个方法有什么用)
- vue3 组件初始化流程(vue组件初始化顺序)
- vue3优雅的设置element-plus的table自动滚动到底部
- Vue3为什么推荐使用ref而不是reactive
- 9、echarts 在 vue 中怎么引用?(必会)
- 无所不能,将 Vue 渲染到嵌入式液晶屏
- vue-element-admin 增删改查(五)(vue-element-admin怎么用)
- 最全的 Vue 面试题+详解答案(vue面试题知识点大全)
- 基于 vue3.0 桌面端朋友圈/登录验证+60s倒计时
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- node卸载 (33)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- exceptionininitializererror (33)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)