简体   繁体   English

如何在IntelliJ IDEA中将文件添加到已推送的提交中

[英]How to add files to already pushed commit in IntelliJ IDEA

I have commited and pushed some changes and forgot to add 2 files to that commit. 我已经提交并推送了一些更改,却忘记为该提交添加2个文件。 Since that commit, I have commited a number of commits, so there are many commits between the commit I want to change/add the file to and the HEAD. 自从提交以来,我已经提交了许多提交,因此在我要更改/添加文件的提交和HEAD之间有很多提交。 I need something like interactive rebasing and fixup, just in IntelliJ IDEA. 仅在IntelliJ IDEA中,我需要诸如交互式基础和修正之类的东西。 So my question is, how can I add these 2 files to that commit in IntelliJ IDEA? 所以我的问题是,如何将这两个文件添加到IntelliJ IDEA中的提交中?

First of all you should notice that changing the history of a published branch might be dangerous. 首先,您应该注意更改已发布分支的历史记录可能很危险。 If anyone else is using the same branch this could lead to a confusing history with duplicate commits. 如果其他人正在使用同一分支,则可能导致重复提交的历史令人困惑。

However if you would otherwise do that using the CLI you might also use IntelliJ to do so if you prefer the GUI way. 但是,如果您否则会使用CLI来执行此操作,那么如果您更喜欢GUI方式,也可以使用IntelliJ来执行此操作。

Amending the previous commit 修改上一次提交

If you need to add these two files to the last commit, you can just amend your changes to that commit. 如果需要将这两个文件添加到最后一个提交,则只需修改对该提交的更改即可。 To do so start committing like you would do to create a new commit. 为此,请像创建新提交一样开始进行提交。 In the commit window check the box next to "Amend commit". 在提交窗口中,选中“修改提交”旁边的框。 IntelliJ will then automatically fill the field "Commit Message" with the message of the last commit. 然后,IntelliJ将自动使用上次提交的消息填充“提交消息”字段。 After committing the changes there will be only one commit containing the changes of the previous commit and your recent changes. 提交更改后,将只有一个提交包含先前提交的更改和您最近的更改。

Rebasing 变基

You can even do interactive rebasing using the IntelliJ GUI to change the history. 您甚至可以使用IntelliJ GUI进行交互式变基,以更改历史记录。 The option "Rebase..." can be found in the menu "VCS" in the submenu "Git". 可以在子菜单“ Git”的“ VCS”菜单中找到“ Rebase ...”选项。 In the opening dialog you can select "Interactive" to do interactive rebasing. 在打开的对话框中,您可以选择“交互”进行交互基础。 You also need to select a branch or commit to rebase onto. 您还需要选择一个分支或提交作为基础。 This might be the branch your branch is based onto or the last commit in your history you don't want to change. 这可能是您的分支所基于的分支,或者是您不想更改的历史记录中的最后一个提交。 The opening dialog gives you the same options as the interactive rebase of the CLI. 打开的对话框为您提供与CLI的交互式基础相同的选项。 See the documentation example for Interactive rebase for the available options. 有关可用选项,请参见Interactive rebase的文档示例。 To add changes to a previous commit you might want to have a look at the section "Squashing multiple commits into one". 要将更改添加到先前的提交中,您可能需要查看“将多个提交压缩为一个”一节。

Pushing your changes 推动您的改变

To push these changes you first need to configure IntelliJ to allow force push. 要推送这些更改,您首先需要配置IntelliJ以允许强制推送。 This option can be found in the preferences under "Version Control" > "Git", where you can check "Allow force push". 该选项可以在“版本控制”>“ Git”下的首选项中找到,您可以在其中选中“允许强制推送”。 After doing so hovering over the "Push" button in the bottom right of the Push dialog opens a dropdown with an entry "Force push". 这样做之后,将鼠标悬停在“推”对话框右下方的“推”按钮上,将打开一个下拉列表,其中包含“强制推”条目。 You need to use that to push your changed commit to the remote repository. 您需要使用它来将更改的提交推送到远程存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM