简体   繁体   English

如何为上次未按下的提交添加新更改?

[英]How can I add new changes to the last not-pushed commit?

  • I pulled the last version from the online repository git pull origin master 我从在线存储库中提取了最新版本git pull origin master
  • Then made some changes in the code-base and added them git add . 然后在代码库中进行了一些更改,并添加了git add .
  • Then created a new commit git commit -m "bug x fixed" 然后创建一个新的提交git commit -m "bug x fixed"
  • Then made some new changes in the code base and I want to merge them to the last commit (which isn't pushed to the online repository yet) . 然后在代码库中进行了一些新更改,我想将它们合并到最后一次提交(尚未提交到在线存储库中)

Should I add them again? 我应该再添加一次吗? Should I remove the last commit and make a new one? 我应该删除上一次提交并重新提交吗? or what? 要不然是啥?

What you are looking for is: 您正在寻找的是:

git add your_changes
git commit --amend

The amend option will take the last commit and replace it by a new one with the previous changes and the new staged changes (new files, rename, delete...). amend选项将采用最后一次提交,并用先前的更改和新的阶段更改(新文件,重命名,删除...)替换为新提交。

It should open the configurated editor with the message of the previous commit, you can replace it with a new message if you like by changing it. 它应使用上次提交的消息打开已配置的编辑器,如果您愿意,可以通过更改将其替换为新的消息。

Another option is to just keep committing as many times as you want since this is local. 另一种选择是仅保留所需次数,因为这是本地的。 Then at the end, when you are ready, you can "squash" all your local commits into a single commit that is pushed up to the remote. 然后,最后,当您准备就绪时,可以将所有本地提交“压缩”到单个提交中,然后将其推送到远程。 There are a few ways to do this, but here is a link with a nice example (possibly one of the more flexible/easy ways): see top answer here 有几种方法可以做到这一点,但是这里有一个很好的例子的链接(可能是更灵活/简便的方法之一): 请参见此处的最佳答案

The advantage of this over things like "amend" is that you can keep all your commits and history locally so you have a better changes of going back if things go wrong or doing smaller diff etc... and all the hassle of going back and amending things later can be postponed to one time only all at the end when you are good and ready.... well, I prefer it that way anyway :) 相对于“修改”之类的东西,此方法的优势在于,您可以将所有提交和历史记录保留在本地,因此,如果事情出错或进行较小的比较等操作,则可以有更好的更改,例如可以返回,并且可以轻松地进行返回和修改。当您准备好并准备好之后,以后再进行修改只能推迟到最后一次。...好吧,无论如何,我还是更喜欢这样做:)

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

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