Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

git - 如何修改指定的提交?(How to modify a specified commit?)

I usually submit a list of commits for review.

(我通常会提交一份提交清单供审核。)

If I have the following commits:

(如果我有以下提交:)

  1. HEAD
  2. Commit3
  3. Commit2
  4. Commit1

...I know that I can modify head commit with git commit --amend .

(...我知道我可以使用git commit --amend修改head commit。)

But how can I modify Commit1 , given that it is not the HEAD commit?

(但是,鉴于它不是HEAD提交,我该如何修改Commit1呢?)

  ask by Sam Liao translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use git rebase .

(您可以使用git rebase 。)

For example, if you want to modify commit bbc643cd , run

(例如,如果要修改commit bbc643cd ,请运行)

$ git rebase --interactive 'bbc643cd^'

Please note the caret ^ at the end of the command, because you need actually to rebase back to the commit before the one you wish to modify .

(请注意命令末尾的插入符^ ,因为实际上您需要要修改的提交之前基准变回提交 。)

In the default editor, modify pick to edit in the line mentioning 'bbc643cd'.

(在默认编辑器中,修改pick以在提到“ bbc643cd”的行中进行edit 。)

Save the file and exit: git will interpret and automatically execute the commands in the file.

(保存文件并退出:git将解释并自动执行文件中的命令。)

You will find yourself in the previous situation in which you just had created commit bbc643cd .

(您会发现自己处在之前创建commit bbc643cd的先前情况中。)

At this point, bbc643cd is your last commit and you can easily amend it : make your changes and then commit them with the command:

(此时, bbc643cd是您的最后一次提交,您可以轻松地对其进行修改 :进行更改,然后使用以下命令提交它们:)

$ git commit --all --amend --no-edit

After that, type:

(之后,输入:)

$ git rebase --continue

to return back to the previous HEAD commit.

(返回上一个HEAD提交。)

WARNING : Note that this will change the SHA-1 of that commit as well as all children -- in other words, this rewrites the history from that point forward.

(警告 :请注意,这将更改该提交以及所有子项的SHA-1-换句话说,这将从该点开始重写历史记录。)

You can break repos doing this if you push using the command git push --force

(如果使用命令git push --force推送,则可以中断存储库)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...