百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

Git分支操作常见使用场景_git设置本地分支和远程分支关联

wptr33 2025-10-02 09:04 32 浏览

一、删除不需要的分支。

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")

相关推荐

oracle数据导入导出_oracle数据导入导出工具

关于oracle的数据导入导出,这个功能的使用场景,一般是换服务环境,把原先的oracle数据导入到另外一台oracle数据库,或者导出备份使用。只不过oracle的导入导出命令不好记忆,稍稍有点复杂...

继续学习Python中的while true/break语句

上次讲到if语句的用法,大家在微信公众号问了小编很多问题,那么小编在这几种解决一下,1.else和elif是子模块,不能单独使用2.一个if语句中可以包括很多个elif语句,但结尾只能有一个else解...

python continue和break的区别_python中break语句和continue语句的区别

python中循环语句经常会使用continue和break,那么这2者的区别是?continue是跳出本次循环,进行下一次循环;break是跳出整个循环;例如:...

简单学Python——关键字6——break和continue

Python退出循环,有break语句和continue语句两种实现方式。break语句和continue语句的区别:break语句作用是终止循环。continue语句作用是跳出本轮循环,继续下一次循...

2-1,0基础学Python之 break退出循环、 continue继续循环 多重循

用for循环或者while循环时,如果要在循环体内直接退出循环,可以使用break语句。比如计算1至100的整数和,我们用while来实现:sum=0x=1whileTrue...

Python 中 break 和 continue 傻傻分不清

大家好啊,我是大田。今天分享一下break和continue在代码中的执行效果是什么,进一步区分出二者的区别。一、continue例1:当小明3岁时不打印年龄,其余年龄正常循环打印。可以看...

python中的流程控制语句:continue、break 和 return使用方法

Python中,continue、break和return是控制流程的关键语句,用于在循环或函数中提前退出或跳过某些操作。它们的用途和区别如下:1.continue(跳过当前循环的剩余部分,进...

L017:continue和break - 教程文案

continue和break在Python中,continue和break是用于控制循环(如for和while)执行流程的关键字,它们的作用如下:1.continue:跳过当前迭代,...

作为前端开发者,你都经历过怎样的面试?

已经裸辞1个月了,最近开始投简历找工作,遇到各种各样的面试,今天分享一下。其实在职的时候也做过面试官,面试官时,感觉自己问的问题很难区分候选人的能力,最好的办法就是看看候选人的github上的代码仓库...

面试被问 const 是否不可变?这样回答才显功底

作为前端开发者,我在学习ES6特性时,总被const的"善变"搞得一头雾水——为什么用const声明的数组还能push元素?为什么基本类型赋值就会报错?直到翻遍MDN文档、对着内存图反...

2023金九银十必看前端面试题!2w字精品!

导文2023金九银十必看前端面试题!金九银十黄金期来了想要跳槽的小伙伴快来看啊CSS1.请解释CSS的盒模型是什么,并描述其组成部分。答案:CSS的盒模型是用于布局和定位元素的概念。它由内容区域...

前端面试总结_前端面试题整理

记得当时大二的时候,看到实验室的学长学姐忙于各种春招,有些收获了大厂offer,有些还在苦苦面试,其实那时候的心里还蛮忐忑的,不知道自己大三的时候会是什么样的一个水平,所以从19年的寒假放完,大二下学...

由浅入深,66条JavaScript面试知识点(七)

作者:JakeZhang转发链接:https://juejin.im/post/5ef8377f6fb9a07e693a6061目录由浅入深,66条JavaScript面试知识点(一)由浅入深,66...

2024前端面试真题之—VUE篇_前端面试题vue2020及答案

添加图片注释,不超过140字(可选)1.vue的生命周期有哪些及每个生命周期做了什么?beforeCreate是newVue()之后触发的第一个钩子,在当前阶段data、methods、com...

今年最常见的前端面试题,你会做几道?

在面试或招聘前端开发人员时,期望、现实和需求之间总是存在着巨大差距。面试其实是一个交流想法的地方,挑战人们的思考方式,并客观地分析给定的问题。可以通过面试了解人们如何做出决策,了解一个人对技术和解决问...