开发者

git revert multiple files of bulk commit

I'm in the process of getting acquainted with GIT, just by ditching svn and continuing with GIT. Now I've made my hands dirty:
I created a branch and changed a bunch of files with an emacs macro, and committed them all with commit -a. I did do some other changes to some other files as well. Before merging everything to the master I tested a few things and that emacs macros lacked a few essential details. So now the greater part of the bulk commit, should be undone but not all. And the the commits after the bulk commit are also solid. What is the easiest way to proceed to restore the greater part of the bulk commit? Is there a cherry pick utility? I'm currently using command line git and emacs vcs (git-el). I've bought a 开发者_JAVA百科book and browsed a few sites but so far I haven't found a satisfying answer yet.

regards,

Jeroen.


git revert -n <hash of bulk commit> will try to reverse the changes in the bulk commit in your working tree. Once it's done, you can run git reset HEAD -- <paths to keep changes in> possibly with --patch (for interactively selecting changes to keep). Then you commit that like any other commit.

Is your repository private/not-pushed? If so, you have one other option, though I don't advise it for something like this. git rebase -i <hash of commit before bulk>. Edit the first line (should have the first line of bulk commit) to start with e or edit.

At that point, you can use git reset <hash of commit before bulk> -- <paths to revert> followed by git commit --amend to fix your bulk commit to exclude changes in some files. Then git rebase --continue will rewrite all of your remaining history on top of the adjusted commit; you may have to resolve conflicts and continue again if they touch the reverted bits of the files. After that, your history will read like you never made some of the bad changes at all; for a private project that may be a good thing, but just be aware that after a rebase it becomes pretty hard to get back to any of the intermediate states that you're rewriting from. You can do it with git reflog for awhile, but eventually the old versions of the commits will expire and be pruned from the repository.

In either case, I'd advise using git diff --name-only and some editing to put together a text file listing paths whose changes you want/don't want to undo, and then using a shell construct like $(< badfiles) in the places where I talk about using lists of paths on the command line to pull that file in. One per line or space-separated will work fine for that kind of thing. Oh, and if you have spaces in your paths, you'll need to put quotes around the paths in the file for it to work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜