I'm modifying the content of a past commit called "commit2", with the following steps:
我正在使用以下步骤修改名为“Commit2”的过去提交的内容:
#1 checkout the commit we want to modify
git checkout ad4fe51
#2 amend the commit with new changes
git add . && git commit --amend --no-edit
#3 remove files from folder
git rm -rf .
#4 return
git checkout -
#5 rebase
git rebase --onto @{-1} ad4fe51
However, at step 5 it pushes back the things i have modified in the commit (#2).
然而,在步骤5,它推回我在提交中修改的内容(#2)。
If I call push origin, commit2 contains all modifications correctly, however,
如果我调用推送来源,Commit2将正确包含所有修改,但是,
"commit7" will contain { commit7.txt, newfile1.txt, newfile2.txt }
“Commit7”将包含{Commit7.txt,newfile1.txt,newfile2.txt}
How can I avoid this?
我怎么才能避免这种情况呢?
I recorded a gif with the steps: https://i.imgur.com/qc9fVeA.gif
我录了一段动漫舞步:https://i.imgur.com/qc9fVeA.gif
Obs: I'm avoiding using --interative to skip the editor as I'm automating this process.
OBS:我避免使用--interative来跳过编辑器,因为我正在自动化这个过程。
更多回答
You cannot modify a commit. Ever. Commits are immutable. Although it is quite common to create a new commit with the same (or very similar) tree, commit message, etc., you should understand that you CAN NOT modify a commit.
您不能修改提交。永远不会。提交是不变的。尽管使用相同的(或非常相似的)树、提交消息等创建新的提交是很常见的,但您应该理解,您不能修改提交。
优秀答案推荐
I think your life will be much easier if you start with git rebase
:
我认为,如果你从Git Rebase开始,你的生活会容易得多:
Identify the commit you want to modify.
Run:
git rebase -i COMMIT^
E.g., if you want to edit commit ad4fe51
, run:
git rebase -i ad4fe51^
In the pick list, change pick
to edit
for that commit and save the list.
Make the changes you want and git add
the files.
Run git rebase --continue
.
Update your commit message, if necessary, and save it.
You're all done!
你们都完蛋了!
If I put the following script in $HOME/bin/git-edit
(or somewhere else in my $PATH
)...
如果我将以下脚本放入$HOME/bin/git-EDIT(或我的$PATH中的其他位置)...
#!/bin/sh
CID=$(git rev-parse "$1")
GIT_SEQUENCE_EDITOR="sed -i '1 s/pick/edit/'" \
git rebase -i "${CID}^"
...then I can run git edit COMMIT
and immediately start making changes (this takes care of both steps 2 and 3 in the above list).
...然后我可以运行git编辑提交并立即开始进行更改(这会处理上面列表中的步骤2和步骤3)。
更多回答
I did that way to avoid the editor because im automating the process
我这样做是为了避开编辑,因为我自动化了这个过程
You'll note that the solution I show at the bottom of this answer avoids the editor (because we replace it with sed
).
您将注意到,我在此答案底部显示的解决方案避开了编辑器(因为我们将其替换为sed)。
我是一名优秀的程序员,十分优秀!