如何修改现有的,未完成的提交? [英] How to modify existing, unpushed commits?

查看:191
本文介绍了如何修改现有的,未完成的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交信息中写了错误的东西。或者,我忘记了包含一些文件。



如何更改提交消息/文件?

解决方案

修改最近的提交消息



  git commit --amend 

将打开您的编辑器,允许您更改最近提交的提交消息。另外,你可以直接在命令行中设置提交信息:

  git commit  - 修改-m新提交消息

...但是,这可以使多行提交消息或小更正更麻烦。



请确保在执行此操作前您没有任何工作副本更改,否则他们也会提交。 ( Unstaged 更改不会被提交。)



更改已经推送到远程分支的提交消息



如果您已经将提交提交到远程分支,那么您将



关于Interactive rebase的重要提示



当您使用 git rebase -i HEAD〜X 可以 more X 提交。 Git将收集最后一个 X 提交中的所有提交,并且如果在该范围之间的某处存在合并,您将看到所有提交,所以结果将会是X +。



好提示:



如果您必须为单个分支做更多的事情,在修改内容时面临冲突,设置 git rerere 并让git自动为您解决这些冲突。




文件




  • git-commit(1)手册页


  • git-rebase(1)手册页

    a>


  • git-push(1)手册页

  • I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.

    How can I change the commit message/files? The commit has not been pushed yet.

    解决方案

    Amending the most recent commit message

    git commit --amend
    

    will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

    git commit --amend -m "New commit message"
    

    …however, this can make multi-line commit messages or small corrections more cumbersome to enter.

    Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)

    Changing the message of a commit that you've already pushed to your remote branch

    If you've already pushed your commit up to your remote branch, then you'll need to force push the commit with:

    git push <remote> <branch> --force
    # Or
    git push <remote> <branch> -f
    

    Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.

    Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.


    Use interactive rebase

    Another option is to use interactive rebase.
    This allows you to edit any message you want to update even if it's not the latest message.

    In order to do a git squash, follow these steps:

    // X is the number of commits to the last commit you want to be able to edit
    git rebase -i HEAD~X
    

    Once you squash your commits - choose the e/r for editing the message

    Important note about Interactive rebase

    When you use the git rebase -i HEAD~X there can be more than X commits. Git will "collect" all the commits in the last X commits and if there was a merge somewhere in between that range you will see all the commits as well so the outcome will be X+.

    Good tip:

    If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere and let git resolve those conflicts automatically for you.


    Documentation

    这篇关于如何修改现有的,未完成的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆