JHHK

欢迎来到我的个人网站
行者常至 为者常成

6、Git独自使用

目录

修改commit的message

一、修改最近commit的message

1、指令

git commit --amend

2、演示

#我们先来看下最近commit的message
localhost:gitLearning LC$ git log -n 1
commit e37b35d92e17026a15ed06a65bff3eaf5ee7fda6 (HEAD -> master)
Author: LC <LC@lc.com>
Date:   Tue Jan 12 16:18:10 2021 +0800

    Background change green


#我们使用如下命令来修改message
localhost:gitLearning LC$ git commit --amend

#当执行完上述命令后,会进入vim的编辑模式,修改信息后保存退出就会打印下面的信息
[master 90e928b] Background change from orange to  green
Date: Tue Jan 12 16:18:10 2021 +0800
1 file changed, 1 insertion(+), 1 deletion(-)


#我们再来看下最近commit的message
localhost:gitLearning LC$ git log -n 1
commit 90e928b86fc1f3e5e90485b7c376fbb707320a11 (HEAD -> master)
Author: LC <LC@lc.com>
Date:   Tue Jan 12 16:18:10 2021 +0800

    Background change from orange to  green
localhost:gitLearning LC$ 

注意commit的hash值已经发生改变

二、修改老旧commit的message

1、指令

#交互式变基
git rebase -i 

2、演示

localhost:gitLearning LC$ git log --oneline
90e928b (HEAD -> master) Background change from orange to  green
f1df605 Add styles
fe9b9f9 Add index.html
8119c2d Add readme
f7eed8c Initial

#我们使用下面的命令
localhost:gitLearning LC$ git rebase -i fe9b9f9

#当执行此名领后我们会进入vim模式
#对要修改的message使用reword命令,:wq退出后会进入另一个vim界面
#在该界面修改message消息,:wq退出后会打印下面信息

[detached HEAD 068de45] Add a tyles
 Date: Tue Jan 12 16:16:06 2021 +0800
 1 file changed, 50 insertions(+)
 create mode 100644 styles/style.css
Successfully rebased and updated refs/heads/master.

#从打印信息我们看到
#使用了分离头指针
#经历了两个阶段rebase 和 update 

现在是在独自使用Git的情况下,也就是说这是我们自己的分支并未共享出去,所以可以使用rebase命令

再次查看下commit的message

localhost:gitLearning LC$ git log --oneline 
b056781 (HEAD -> master) Background change from orange to  green
068de45 Add a tyles
fe9b9f9 Add index.html
8119c2d Add readme
f7eed8c Initial
localhost:gitLearning LC$ 

整理commit

一、整理连续的几个commit

1、指令

#交互式变基
git rebase -i 

2、演示

#如何把下面8119c2d ~ b056781的几次提交整理到一次提交中
localhost:gitLearning LC$ git log --graph --oneline
* b056781 (HEAD -> master) Background change from orange to  green
* 068de45 Add a tyles
* fe9b9f9 Add index.html
* 8119c2d Add readme
* f7eed8c Initial

#通过以下命令
localhost:gitLearning LC$ git rebase -i f7eed8c9

#命令执行后会进入vim模式        
#使用pick命令
pick 8119c2d Add readme
#下面三个使用s命令,合并到previous commit
s fe9b9f9 Add index.html
s 068de45 Add a tyles
s b056781 Background change from orange to  green

# Rebase f7eed8c..b056781 onto b056781 (4 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.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out


#对以上编辑保存后,会进入另一个vim,编辑保存后退出
# This is a combination of 4 commits.
Create a complete web page
# This is the 1st commit message:

Add readme

# This is the commit message #2:

Add index.html

# This is the commit message #3:

Add a tyles

# This is the commit message #4:

Background change from orange to  green

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Jan 12 15:54:48 2021 +0800
#
# interactive rebase in progress; onto f7eed8c
# Last commands done (4 commands done):
#    squash 068de45 Add a tyles
#    squash b056781 Background change from orange to  green
# No commands remaining.
# You are currently rebasing branch 'master' on 'f7eed8c'.
#
# Changes to be committed:
#       new file:   images/git-logo.png
#       new file:   index.html
#       new file:   js/script.js
#       new file:   readme
#       new file:   styles/style.css
#
~                                                                                                                                           

我们看下commit的message发生了什么变化

localhost:gitLearning LC$ git log 
commit 1ea5491b18a6ec9a79aa886a482327d7f5ccddf9 (HEAD -> master)
Author: LC <LC@lc.com>
Date:   Tue Jan 12 15:54:48 2021 +0800

    Create a complete web page
    
    Add readme
    
    Add index.html
    
    Add a tyles
    
    Background change from orange to  green

commit f7eed8c992c0db3b4ab33e2fb159f6c77322415e
Author: LC <LC@lc.com>
Date:   Tue Jan 12 15:52:17 2021 +0800

    Initial
localhost:gitLearning LC$ 
localhost:gitLearning LC$ git log --oneline
1ea5491 (HEAD -> master) Create a complete web page
f7eed8c Initial
localhost:gitLearning LC$ 

二、整理间隔的几个commit

1、指令

#交互式变基
git rebase -i 

2、演示

#我们先看下整理前的commit记录
localhost:gitLearning LC$ git log --oneline
db0dd43 (HEAD -> master) modify readme file
1ea5491 Create a complete web page
f7eed8c Initial

#使用rebase命令
#手动把f7eed8c添加上
pick f7eed8c
#把这条记录移动到f7eed8下面,改为s
s db0dd43 modify readme file
pick 1ea5491 Create a complete web page


#保存退出,如果弹出vim模式,与合并连续commit一样,修改message后保存退出即可
#如果没有弹出vim,执行git status 根据提示进行下步操作,试试下面的指令
git add .
git rebase --continue

#弹出vim,编辑内容后保存退出
Initia and modify readme

# This is a combination of 2 commits.
# This is the 1st commit message:
Initial

# This is the commit message #2:
all modify readme file
modify readme file
modify readme file2
modify readme file3

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Jan 12 15:52:17 2021 +0800
#
# interactive rebase in progress; onto f7eed8c
# Last commands done (2 commands done):
#    pick f7eed8c
#    squash 931037a all modify readme file
# Next command to do (1 remaining command):
#    pick 1ea5491 Create a complete web page
# You are currently rebasing branch 'master' on 'f7eed8c'.
#
# Changes to be committed:
#       new file:   readme
#

行者常至,为者常成!





R
Valine - A simple comment system based on Leancloud.