玖叶教程网

前端编程开发入门

Git分支操作常见使用场景

一、删除不需要的分支。

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: 卢大湿 <[email protected]>
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: 卢大湿 <[email protected]>
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")

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言