Git 使用 —— 本地环境使用

准备

  1. 创建一个仓库,并进入仓库

    git init test
    cd test
  2. master 分支下添加一个 readme 文件,并提交

    git add -A
    git commit -m 'Add a readme'
  3. 该分支下继续添加三次文件,分别提交,最后的记录如下:

  4. 创建一个名为 temp 的分支

    git checkout -b temp 51f0f45f9d1e # 后面的哈希值是某个 commit
  5. temp 分支下修改 readme 文件内容并提交

    vim readme
    git commit -am 'change readme content'
  6. 创建另外一个分支,查看所有的分支

    git checkout -b aaa
    git branch -v

删除不需要的分支

上面创建的 aaa 分支是多余的,我们删除它

  1. 使用命令 git branch -d aaa,这里会提示让我们使用大写 D 清除
  2. 这时再用 git branch -D aaa 进行删除
  • 报错 error: The branch 'XXX' is not fully merged,是指该分支不曾并入到其他任何分支,Git 会判断该分支是否和其他分支合并,没有合并意味着删除存在风险。
  • 报错 error: Cannot delete branch 'XXX' checked out at 'XXXXX',是指要删除的分支正在使用,切换到其他分支再进行删除。

使用 -d 进行删除时会先判断是否存在风险,使用 -D 是强制删除。

修改 message

修改最新 commit 的 message

git commit --amend

在新界面中修改 commit 内容。

Add a style.css (master)

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Feb 22 21:12:52 2020 +0800
#
# On branch master
# Changes to be committed:
#       new file:   style.css
#

修改后退出保存,这里涉及到 vim 的操作,请自行摸索。使用 log 命令可看到 message 改变了。

git commit --amend 命令其实应该是修改或替换上一次提交,不只是修改 message,它会创建一个新的 commit 来替换

修改其他 message

先查看当前的 message

# test (master) git log
commit 80c60c31eb8ada12c959e4f32fa40b327fec93ed (HEAD -> master)
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:12:52 2020 +0800

    Add a style.css (master)

commit 38329827ff4c40cccdf224b7c8e585080a8970cc
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:11:57 2020 +0800

    Add src folder + img

commit 1da564f1493246f7d6d7d5f2f542c7cc04908d70
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:11:03 2020 +0800

    Add a index.html

commit 51f0f45f9d1e13b9ee518d2d9b8a78f9d8f37bbc
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:05:18 2020 +0800

    Add a readme

接着修改第二个 commit 的 message,注意:这里是基于谁来修改,选择的是此 commit 的父亲,也就是最后一个 commit 的哈希值。

git rebase -i 1da564f1

在弹出编辑界面中进行配置。

r 3832982 Add src folder + img
pick 80c60c3 Add a style.css (master)

# Rebase 1da564f..80c60c3 onto 1da564f (2 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
# b, break = stop here (continue rebase later with 'git rebase --continue')
# 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.

在该界面中,下面有每种命令所对应的解释,p, pick 就是保留该 commit,r, reword 是保留该 commit,但是需要修改 commit 的 message,所以,需要在想要操作的 commit 前面加上对应的命令,这里我们需要修改,所以第一行使用了缩写命令 r

保存退出,会弹出另外一个编辑界面,在这里进行 message 的修改,在此保存即可。

Add src folder and a image.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Feb 22 21:11:57 2020 +0800
#
# interactive rebase in progress; onto 1da564f
# Last command done (1 command done):
#    r 3832982 Add src folder + img
# Next command to do (1 remaining command):
#    pick 80c60c3 Add a style.css (master)
# You are currently editing a commit while rebasing branch 'master' on '1da564f'.
#
# Changes to be committed:
#       new file:   src/Snipaste_2020-02-17_20-47-55.jpg

修改完成后再次查看,可看到已经修改成功,并且,从修改的那一次 commit 开始,之后的 commit 哈希值都改变了。

commit 72cafa2840638aed69dad7174e874865fec22d3c (HEAD -> master) # 哈希值变了
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:12:52 2020 +0800

    Add a style.css (master)

commit 31ae4fb9f67e28e98f0d12439c9668292d32d298 # 哈希值变了
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:11:57 2020 +0800

    Add src folder and a image.

commit 1da564f1493246f7d6d7d5f2f542c7cc04908d70 # 哈希值变了
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:11:03 2020 +0800

    Add a index.html

commit 51f0f45f9d1e13b9ee518d2d9b8a78f9d8f37bbc # 哈希值没变
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:05:18 2020 +0800

    Add a readme

rebase 意味着基于新 base 的 commit 来变更,messagecommit 的一个属性,修改后,commit 的哈希值自然就改变了,最后一次创建的 commit 是基于上一次的 commit,只要上一次的 commit 修改过,那么最近这一次的哈希值也会跟着改变。下面是其他命令的作用:

命令 说明
pick 保留 commit
reword 保留 commit,修改 message
edit 保留 commit,但是要停下来修改该提交(不仅仅修改注释)
squash 将该 commit 和前一个 commit 合并
fixup 将该 commit 和前一个 commit 合并,但不保留该提交的 message
exec 执行 shell 命令
drop 丢弃该 commit
label 用名字标记当前 HEAD
reset 将 HEAD 重置

合并 commit

连续合并

合并连续的 commit,同样使用 rebase 命令。

在这里,合并最近的两条 commit,也就是 Add a style.css (master)Add src folder and a image。和上面的道理一样,需要选择它们的父级,也就是 message 为 Add a index.html 的哈希值。

git rebase -i 1da564f149

进入到编辑界面,需要把最后的 commit 并入上一个 commit,应该使用 squash 命令。

pick 31ae4fb Add src folder and a image.
s 72cafa2 Add a style.css (master)

# Rebase 1da564f..72cafa2 onto 1da564f (2 commands)

这时,还会跳出一个编辑界面,在这里对此次合并进行说明

# This is a combination of 2 commits.
+ Create a commit	# 加号表示这条是新增的
# This is the 1st commit message:

Add src folder and a image.

# This is the commit message #2:

Add a style.css (master)

再次查看记录,原来的四条 commit 变为了三条。

间隔合并

commit 6b16c48797fe9ea05fb6bdbda1167e6ff840fb80 (HEAD -> master)
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:11:57 2020 +0800

    Add src folder and a image.

    Add a style.css (master)

commit 1da564f1493246f7d6d7d5f2f542c7cc04908d70
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:11:03 2020 +0800

    Add a index.html

commit 51f0f45f9d1e13b9ee518d2d9b8a78f9d8f37bbc
Author: staunchkai <staunchkai@outlook.com>
Date:   Sat Feb 22 21:05:18 2020 +0800

    Add a readme

把第一条和第三条进行合并,因为第一条 Add a readme 已经是祖宗了,所以使用命令 rebase 就选择它。

git rebase -i 51f0f45f9

进入编辑界面后如下

pick 1da564f Add a index.html
pick 6b16c48 Add src folder and a image.

但是这是不够的,需要把祖宗这一条写进去。

+ pick 51f0f45
+ s 6b16c48 Add src folder and a image.
- pick 1da564f Add a index.html
pick 1da564f Add a index.html
- pick 6b16c48 Add src folder and a image.
  • 把第一个 commit 保留下来,对应第一行
  • 要第三个和第一个合并,对应第二行,原来的删除

保存退出后,会提示如下:

interactive rebase in progress; onto 51f0f45
Last command done (1 command done):
   pick 51f0f45
Next commands to do (2 remaining commands):
   s 6b16c48 Add src folder and a image.
   pick 6b16c48 Add src folder and a image.
You are currently rebasing branch 'master' on '51f0f45'.

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git cherry-pick --skip'
Could not apply 51f0f45...

使用 git status 查看一下情况

interactive rebase in progress; onto 51f0f45
Last command done (1 command done):
   pick 51f0f45
Next commands to do (2 remaining commands):
   s 6b16c48 Add src folder and a image.
   pick 6b16c48 Add src folder and a image.
  (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'master' on '51f0f45'.
  (all conflicts fixed: run "git rebase --continue")

nothing to commit, working tree clean

根据提示,应该使用 git rebase --continue 命令,之后就会出现类似之前的编辑界面。

# This is a combination of 2 commits.
+ Add readme + srouce.
# This is the 1st commit message:
Add a readme
# This is the commit message #2:
Add src folder and a image.
Add a style.css (master)

Git 系列到此暂停。。。

如果觉得这篇文章不错,不妨

微信打赏 微信打赏
支付宝打赏 支付宝打赏
算法时间复杂度分析(二)
Git 使用 —— .git 目录