Git 基础(2)| 如何变动已经修改上一次提交?
目录1、撤销操作
在任何一个阶段,你都有可能想要撤消某些操作。有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。 此时,可以运行带有 --amend
选项的提交命令来重新提交:
--amend 作用:
[~/02_git_space/git-learn]$ git status *[master]
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: a.txt
new file: b.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
[~/02_git_space/git-learn]$
但是你现在误以为
c.txt
也已经纳入了版本管理
所以你直接运行如下命令:
[~/02_git_space/git-learn]$ git commit -am "fix:提交所有txt文件" *[master]
[master b192e13] fix:提交所有txt文件
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
[~/02_git_space/git-learn]$
此时就需要--amend
来帮你解决这个问题啦~,步骤如下:
[~/02_git_space/git-learn]$ git add c.txt *[master]
[~/02_git_space/git-learn]$ git status *[master]
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: c.txt
[~/02_git_space/git-learn]$ git commit --amend
fix:提交所有txt文件
(补录c.txt,到本次提交中) # 该内容为之后的补录描述
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sun Feb 27 20:28:51 2022 +0800
#
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# new file: a.txt
# new file: b.txt
# new file: c.txt
#
-- INSERT --
编辑后保存会覆盖原来的提交信息并显示如下:
[~/02_git_space/git-learn]$ git commit --amend *[master]
[master fb44c5f] fix:提交所有txt文件 (补录c.txt,到本次提交中)
Date: Sun Feb 27 20:28:51 2022 +0800
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
create mode 100644 c.txt
[~/02_git_space/git-learn]$
commit fb44c5f908e831b55845a7d53f3296cb6f4c12ee (HEAD -> master)
Author: lishanbiao <1336503209@qq.com>
Date: Sun Feb 27 20:28:51 2022 +0800
fix:提交所有txt文件
(补录c.txt,到本次提交中)
a.txt | 0
b.txt | 0
c.txt | 0
3 files changed, 0 insertions(+), 0 deletions(-)
commit 570e628d2c85c18ebc6f645fcad696d598992f6a (origin/master, origin/HEAD)
Author: lishanbiao <lishanbiao111@sina.com>
Date: Sat Feb 26 20:56:34 2022 +0800
这说明,已经将
c.txt
补录到了上一次的提交中,并改变了上一次提交的描述
至此为你的马虎提交矫正成功!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。