RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 559711
Accepted
sys_dev
sys_dev
Asked:2020-08-26 18:06:15 +0000 UTC2020-08-26 18:06:15 +0000 UTC 2020-08-26 18:06:15 +0000 UTC

在 GIT 中是否可以将另一个文件添加到上次本地提交?

  • 772

有时我会在提交时忘记指定其他文件。有没有办法将文件添加到最后一次提交?

在 Mercurial 中,我这样做:

  1. hg qinit
  2. 将提交转换为补丁
  3. 我更新补丁
  4. 将补丁转换为提交

也许 GIT 也有办法?

git
  • 4 4 个回答
  • 10 Views

4 个回答

  • Voted
  1. Best Answer
    Volen
    2020-08-26T18:08:44Z2020-08-26T18:08:44Z

    是的,该选项--amend用于此。

    使用示例

    1. 让我们看看提交历史。

      $ git log
      
      commit 5c4a8e76f951eb7ee157f4136257f6666fddf1d1
      Author: John Doe <johndoe@gmail.com>
      Date:   Sun Aug 28 14:40:30 2016 +0300
      
          Changed 1.txt
      
      commit 7f2ad8c26ad032800c049d0d6122c43410a5cbbc
      Author: John Doe <johndoe@gmail.com>
      Date:   Sun Aug 28 14:39:30 2016 +0300
      
          Initial commit
      
    2. 假设有一个文件需要添加到之前的提交中。

      $ git status
      
      On branch master
      Untracked files:
         (use "git add <file>..." to include in what will be committed)    
              test.txt
      
    3. 使用 将此文件添加到索引中git add。

      $ git add --all
      
      $ git status
      
      On branch master
      Changes to be committed:
        (use "git reset HEAD <file>..." to unstage)
              new file:   test.txt
      
    4. 然后,您可以--amend使用git commit. 您还可以通过添加更改提交消息-m 'Commit message'。要使提交消息保持不变,只需传递一个空字符串而不是-m ''.

      $ git commit -m 'Added test' --amend
      
      [master aad3e76] Added test
       Date: Sun Aug 28 14:40:30 2016 +0300
       2 files changed, 2 insertions(+), 1 deletion(-)
       create mode 100644 test.txt
      
    5. 让我们检查提交历史。

      $ git log --stat
      commit aad3e7653cd76c4afa1a9272bd421493b4e3055c
      Author: John Doe <johndoe@gmail.com>
      Date:   Sun Aug 28 14:40:30 2016 +0300
      
           Added test
      
       1.txt    | 3 ++-
       test.txt | 0
       2 files changed, 2 insertions(+), 1 deletion(-)
      
      commit 7f2ad8c26ad032800c049d0d6122c43410a5cbbc
      Author: John Doe <johndoe@gmail.com>
      Date:   Sun Aug 28 14:39:30 2016 +0300
      
           Initial commit
      
       1.txt | 1 +
       1 file changed, 1 insertion(+)
      

    改变历史的其他方法

    git rebase诸如和之类的命令可用于更改提交历史记录git filter-branch,但它们用于更复杂的情况。例如,如果你想更改一个在多次提交之前进行的提交,你可以使用交互式rebase- git rebase -i。

    您可以在此处阅读有关在 git 中更改历史记录的更多信息。

    PS使用此技术时要小心git commit --amend,因为它实质上会创建一个新提交,其中包括您正在修改的提交的更改 + 添加的更改。如果您已经将最新提交推送到远程存储库,请不要编辑它。

    • 36
  2. A K
    2020-08-26T18:41:25Z2020-08-26T18:41:25Z

    重要提示:这假设您的最后一次提交尚未推送,因为在团队工作中不建议通过 push -f 对存储库进行更改。这也是后记@Volen。

    有两种方法,一种是@Volen在上面已经介绍过,所以不再重复,第二种是使用rebase。应该做什么。

    有一个要在其中添加文件的提交 A。

    您创建添加此文件的提交 B。

    然后你发出一个命令git rebase -i HEAD~2并在提交 B 中用 squash 替换 pick (你可以只替换字母 s)

    那些。最初它会向您显示类似的内容:

    [ak@hostname testfolder]$ git rebase -i HEAD~2
    pick 7921448 Commit A
    pick 8a84e59 Commit B
    
    # Rebase ce97832..8a84e59 onto ce97832
    #
    # Commands:
    #  p, pick = use commit
    #  r, reword = use commit, but edit the commit message
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #  f, fixup = like "squash", but discard this commit's log message
    #  x, exec = run command (the rest of the line) using shell
    #
    # 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
    ~
    ~
    ~
    "~/testfolder/.git/rebase-merge/git-rebase-todo" 20L, 650C
    

    您需要像这样将提交 B 合并到 A 中:

    pick 7921448 Commit A
    s 8a84e59 Commit B
    

    保存后,将出现一个窗口,提示您选择新的合并提交的名称:

    Rebasing (2/2)
    # This is a combination of 2 commits.
    # The first commit's message is:
    
    Commit A
    
    # This is the 2nd commit message:
    
    Commit B
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # HEAD detached at 7921448
    # You are currently editing a commit while rebasing branch 'master' on 'ce97832'.
    #
    # Changes to be committed:
    #   (use "git reset HEAD^1 <file>..." to unstage)
    #
    #       new file:   file2
    #       new file:   file3
    #
    ~
    ~
    ".git/COMMIT_EDITMSG" 20L, 494C
    

    您可能认为 amend 方法更简单。确实如此。但是,基于 rebase,您可以如此广泛地修改提交 - 拆分、合并、重新排列 - 其他方法无法在功能方面进行比较。

    因此,我喜欢通过简单的操作来展示 rebase 的可能性,而你的操作非常简单。

    PS 我举了一个来自 git 命令行的例子,当然,图形客户端也可以变基——例如,在 SourceTree 中,您在 A 之前的上一个提交上单击右键,然后在上下文菜单中选择 Rebase children interactively 项。

    • 20
  3. Nakilon
    2020-09-02T02:47:09Z2020-09-02T02:47:09Z

    我会很简短:

    git add file.txt
    git commit --amend
    

    如果在此之前的最后一次提交已经在服务器后面push或服务器上,那么下一次提交push将不得不--force重写。

    • 5
  4. jekaby
    2020-09-23T17:11:29Z2020-09-23T17:11:29Z

    如果服务器还没有被推送,那么你可以撤销最后一次提交git reset --soft HEAD^,添加丢失的文件git add,再次重复提交。

    git reset --soft HEAD^ # 撤消上次提交
    git 添加文件名
    git status # 查看绿色文件
    git commit -m '提交所有文件'
    

    更简单地说:

    修改将做同样的事情(将更改附加到最新的提交):

    git commit --amend
    
    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5