Git分支操作常见使用场景 git新建分支
ztj100 2024-12-23 14:52 18 浏览 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")
相关推荐
- Sublime Text 4 稳定版 Build 4113 发布
-
IT之家7月18日消息知名编辑器SublimeText4近日发布了Build4113版本,是SublimeText4的第二个稳定版。IT之家了解到,SublimeTe...
- 【小白课程】openKylin便签贴的设计与实现
-
openKylin便签贴作为侧边栏的一个小插件,提供便捷的文本记录和灵活的页面展示。openKylin便签贴分为两个部分:便签列表...
- 壹啦罐罐 Android 手机里的 Xposed 都装了啥
-
这是少数派推出的系列专题,叫做「我的手机里都装了啥」。这个系列将邀请到不同的玩家,从他们各自的角度介绍手机中最爱的或是日常使用最频繁的App。文章将以「每周一篇」的频率更新,内容范围会包括iOS、...
- 电气自动化专业词汇中英文对照表(电气自动化专业英语单词)
-
专业词汇中英文对照表...
- Python界面设计Tkinter模块的核心组件
-
我们使用一个模块,我们要熟悉这个模块的主要元件。如我们设计一个窗口,我们可以用Tk()来完成创建;一些交互元素,按钮、标签、编辑框用到控件;怎么去布局你的界面,我们可以用到pack()、grid()...
- 以色列发现“死海古卷”新残片(死海古卷是真的吗)
-
编译|陈家琦据艺术新闻网(artnews.com)报道,3月16日,以色列考古学家发现了死海古卷(DeadSeaScrolls)新残片。新出土的羊皮纸残片中包括以希腊文书写的《十二先知书》段落,这...
- 鸿蒙Next仓颉语言开发实战教程:订单列表
-
大家上午好,最近不断有友友反馈仓颉语言和ArkTs很像,所以要注意不要混淆。今天要分享的是仓颉语言开发商城应用的订单列表页。首先来分析一下这个页面,它分为三大部分,分别是导航栏、订单类型和订单列表部分...
- 哪些模块可以用在 Xposed for Lollipop 上?Xposed 模块兼容性解答
-
虽然已经有了XposedforLollipop的安装教程,但由于其还处在alpha阶段,一些Xposed模块能不能依赖其正常工作还未可知。为了解决大家对于模块兼容性的疑惑,笔者尽可能多...
- 利用 Fluid 自制 Mac 版 Overcast 应用
-
我喜爱收听播客,健身、上/下班途中,工作中,甚至是忙着做家务时。大多数情况下我会用MarcoArment开发的Overcast(Freemium)在iPhone上收听,这是我目前最喜爱的Po...
- 浅色Al云食堂APP代码(三)(手机云食堂)
-
以下是进一步优化完善后的浅色AI云食堂APP完整代码,新增了数据可视化、用户反馈、智能推荐等功能,并优化了代码结构和性能。项目结构...
- 实战PyQt5: 121-使用QImage实现一个看图应用
-
QImage简介QImage类提供了独立于硬件的图像表示形式,该图像表示形式可以直接访问像素数据,并且可以用作绘制设备。QImage是QPaintDevice子类,因此可以使用QPainter直接在图...
- 滚动条隐藏及美化(滚动条隐藏但是可以滚动)
-
1、滚动条隐藏背景/场景:在移动端,滑动的时候,会显示默认滚动条,如图1://隐藏代码:/*隐藏滚轮*/.ul-scrool-box::-webkit-scrollbar,.ul-scrool...
- 浅色AI云食堂APP完整代码(二)(ai 食堂)
-
以下是整合后的浅色AI云食堂APP完整代码,包含后端核心功能、前端界面以及优化增强功能。项目采用Django框架开发,支持库存管理、订单处理、财务管理等核心功能,并包含库存预警、数据导出、权限管理等增...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 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)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- 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)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)