简体   繁体   中英

Modifying old commit in git

I have a habit of going back to my old commits in git and modify my working directory.For example I have a series of commits

A<-B<-C<-D<-E<-F

In commit BI added some text files.Now I want to add more text files in that commit and record it there.

I have heard that git rebase and git reset --soft should be used here But didn't understand how to use them in practice? Should I use the following command

git rebase HEAD^5

You can use

git rebase -i HEAD~5

this will open a window with a couple of lines that look something like

pick 362trhy4
pick ewq3w5y4
pick 34642wfg
pick 34265wey
pick 34652366

rebase blabla onto blabla

Note that the commit hashes are just made up. What you want to do is go on the line of the commit you want to edit, and change pick to edit . Then quit the editor (nano, vim, etc) and confirm.

It will 'bring you back' to the commit you want to edit. You are now free to make changes. Once you are done, git add or git rm your changed files, commit amend them (because you want to alter your commit)

git commit --amend -m "new commit message"

Note that this will change your commit hash. Then do

git rebase --continue`

this will make the rebase process continue. Because all the other commits are just pick s, git will try to reapply them onto your edited commit.

If no conflicts arise: great, you are now done.

If they do: solve the conflicts any way you like and mark the conflicts solved with git add or git rm . Then again git rebase --continue . Repeat this step until all conflicts are resolved. You should now be back at where you started, but with a changed history.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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