如何修改尚未push的commit

2022-10-24  本文已影响0人  今年27

1.修改最后一条commit
git commit --amend修改最后一条commit记录
2.批量修改 commit记录
git rebase -i 1798320c4956051f2b2a819c93da8099f562cf9f
这个1798320c4956051f2b2a819c93da8099f562cf9f是某一条commit的sha-1,这条指令的意思就是修改从1798320c4956051f2b2a819c93da8099f562cf9f到现在的所有commit,这个指令会弹出一个vim弹窗

pick b85a51a 测试
pick ee110b7 sdfasd
pick c196127 hellow

# Rebase 1798320..c196127 onto 1798320 (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
# 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.
"~/Documents/swift/localwebcache/.git/rebase-merge/git-rebase-todo" 29L, 1196B

这里要注意,上面的顺序与git log的顺序是相反的,但在Source Tree界面中是一样的,可以将pick改成reword

reword b85a51a 测试
reword ee110b7 sdfasd

这样当存档离开之后,就会弹出vim编辑页面

测试

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Oct 20 13:29:19 2022 +0800
#
# interactive rebase in progress; onto 1798320
# Last command done (1 command done):
#    reword b85a51a 测试
# Next commands to do (2 remaining commands):
#    reword ee110b7 sdfasd
#    pick c196127 hellow
# You are currently editing a commit while rebasing branch 'master' on '1798320'.
#
# Changes to be committed:
#       new file:   index.html
#
~                                                                               
~                                                                               
~                                                                               
~                                                                               
"~/Documents/swift/localwebcache/.git/COMMIT_EDITMSG" 18L, 539B

测试改为修改测试

修改测试

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Oct 20 13:29:19 2022 +0800
#
# interactive rebase in progress; onto 1798320
# Last command done (1 command done):
#    reword b85a51a 测试
# Next commands to do (2 remaining commands):
#    reword ee110b7 sdfasd
#    pick c196127 hellow
# You are currently editing a commit while rebasing branch 'master' on '1798320'.
#
# Changes to be committed:
#       new file:   index.html
#
~                                                                               
~                                                                               
~                                                                               
~           

保存后会弹出另外一条弹窗

sdfasd

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Oct 20 13:43:47 2022 +0800
#
# interactive rebase in progress; onto 1798320
# Last commands done (2 commands done):
#    reword b85a51a 测试
#    reword ee110b7 sdfasd
# Next command to do (1 remaining command):
#    pick c196127 hellow
# You are currently editing a commit while rebasing branch 'master' on '1798320'.
#
# Changes to be committed:
#       new file:   index.js
#
~                                                                               
~                                                                               
~                                                                               
~                                                                               
"~/Documents/swift/localwebcache/.git/COMMIT_EDITMSG" 18L, 537B

同样的将sdfasd修改为修改sdfasd,保存
然后通过git log --oneline我们就已经发现commit已经修改了

appledeMacBook-Pro:localwebcache apple$ git log --oneline
c79f292 (HEAD -> master) hellow
63e72c2 修改sdfasd
7131ba2 修改测试
1798320 First

3.把多个commit合并为一个commit,使用squash命令

d7729d1 (HEAD -> master) 测试3
99369e5 测试2
da1d06f 测试1

同样的,我们需要先
git rebase -i da1d06f
比如我们需要将测试3合入测试2中

pick 99369e5 测试2
pick d7729d1 测试3

我们只需要将测试3的pick改为squash

pick 99369e5 测试2
squash d7729d1 测试3

保存,退出,会跳出一个vim编辑窗口

# This is the 1st commit message:

测试2

# This is the commit message #2:

测试3

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Oct 23 22:43:18 2022 +0800
#
# interactive rebase in progress; onto da1d06f
# Last commands done (2 commands done):
#    pick 99369e5 测试2
#    squash d7729d1 测试3
# No commands remaining.
# You are currently rebasing branch 'master' on 'da1d06f'.
#
# Changes to be committed:
#       modified:   index.js
#


改为

合并测试23

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Oct 23 22:43:18 2022 +0800
#
# interactive rebase in progress; onto da1d06f
# Last commands done (2 commands done):
#    pick 99369e5 测试2
#    squash d7729d1 测试3
# No commands remaining.
# You are currently rebasing branch 'master' on 'da1d06f'.
#
# Changes to be committed:
#       modified:   index.js
#

保存退出
就修改成功了
通过 git log --oneline
可以清晰的看到

appledeMacBook-Pro:localwebcache apple$ git log --oneline
35836c5 (HEAD -> master) 合并测试23
da1d06f 测试1
c7d73ff (origin/master, origin/HEAD) Merge branch 'master' of https://github.com/yangqingshan0402/localwebcache
7131ba2 修改测试
c196127 hellow
ee110b7 sdfasd
b85a51a 测试
1798320 First

4,拆分commit
如果某一条的commit提交的内容过多,可以考虑拆分commit
比如我们要把合并测试23拆成两个commit
同样可以通过git rebase -i da1d06f
pick 35836c5 合并测试23
前面的pick改为edit
edit 35836c5 合并测试23,保存后
界面变成

Stopped at 35836c5...  合并测试23
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

然后输入git reset HEAD^

git status
interactive rebase in progress; onto da1d06f
Last command done (1 command done):
   edit 35836c5 合并测试23
No commands remaining.
You are currently splitting a commit while rebasing branch 'master' on 'da1d06f'.
  (Once your working directory is clean, run "git rebase --continue")

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   index.js

no changes added to commit (use "git add" and/or "git commit -a")

可以通过git add -p提交一部分代码

diff --git a/index.js b/index.js
index fbe7db1..02c6de2 100644
--- a/index.js
+++ b/index.js
@@ -1 +1,3 @@
-sf
\ No newline at end of file
+sf
+sf
+算法、
\ No newline at end of file
(1/1) Stage this hunk [y,n,q,a,d,e,?]? 

选择e

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1 +1,3 @@
-sf
\ No newline at end of file
+sf
+sf
+算法、
\ No newline at end of file
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again.  If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.

将不需要的add+号那一行去掉,并保存

@@ -1 +1,3 @@
-sf
\ No newline at end of file
+sf
+算法、
\ No newline at end of file
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again.  If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.

此时调用git status

interactive rebase in progress; onto da1d06f
Last command done (1 command done):
   edit 35836c5 合并测试23
No commands remaining.
You are currently splitting a commit while rebasing branch 'master' on 'da1d06f'.
  (Once your working directory is clean, run "git rebase --continue")

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   index.js

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   index.js

我们调用git commit -m "测试2"
然后把剩下的调用
git add index.js
git commit -m "测试3"

此时我们再调用git log --oneline,就会看到commit已经拆开了

61ba281 (HEAD) 测试3
2da8820 测试2
da1d06f 测试1
上一篇 下一篇

猜你喜欢

热点阅读