Undo a git commit + git branch <branchname> -- with a twist
I am in the process of "documenting in hind-sight" the history of an application development, by moving existing snapshots of the project's directory tree (that were saved back then by plain & primitive folder copies, not git or any other version control) to git.
After struggling with learning git's new concepts and new terminology, this migration process seemed to have been going well when, all of a sudden I discovered: "Oops... my last git commit
+ git branch <branchname>
skipped one snapshot."
Since the order of committing the snapshots is important to me, I would like to completely undo the last git commit
+ git branch <branchname>
, as if it were never done.
That is, "fixing by modifying a commit" as defined at the bottom of the Undoing in Git chapter in the Git book.
Everything is still local (I didn't "publish" nor did I "push" anything yet), so I believe this is in line with the warnings about "rewriting history". :)
开发者_JS百科Could you please confirm or correct the following steps required per my understanding?
- Empty the working directory (without
deleting the
.git
subdirectory, of course) git checkout <folder-name>
(bringing working directory back to the state it were immediately after I committed the wrong folder and tagged it via git branch)git branch -D <branchname-of-mistaken-folder-commit>
git commit --amend
At this point I expect that git to no longer remember that the last branch and commit have ever been done, and the working directory contain the mistaken folder (which I will be deleting, replacing completely by the one I skipped). Is this correct?
To just delete the commit you just made, assuming you are on the master
branch, all you have to do is:
git branch -D <name of the branch>
to delete the branchgit reset --hard HEAD^
to reset the current branch to the previous commit and the working directory with it
After that, you can commit the forgotten directory normally and then the one you reset.
精彩评论