开发者

Does Git have a workflow for "restarting" your project?

I've been using Git for a bit now and I really like it but one thing I'm haven't been able to figure out. I run into this scenario almost every time I start a project where I don't know what I'm getting myself into (in this case Node.js):

I get pretty far in a project, committing along like a good Git citizen. Then I royally mess up... or realize I cannot possibly salvage my current code. I want to start fresh, but because I'm anal, I don't want to lose my git commits. Is there any way to "transfer" git commits to this new project?

EDIT: Thanks for the quick responses - I forgot to add. This new project would be in a new base folder thats outside the git repo开发者_如何学C. Would I just copy the git repo to that directory then? Deleting the files then committing won't work for me because I take working snippets from the previous project as I transition into the new project.

Thanks! Matt Mueller


@matt, you can create a new branch off the current master (or any branch), and then do a

git reset --hard HEAD~n

where n is the number of commits back you want to go. but be careful, make sure the new branch has the commits you want before you reset your master, as the hard reset blows them away (I think).

You can also go back to a specific commit via its sha1 hash, the syntax should be similar.


Well, it depends on exactly how you want to get back to a good state.

If you just want to write off some previous commits (units of work) mixed up with good ones then use git rebase -i HEAD~10... increase "10" to be far enough back and remove the commits that are junk.

If you want to revert a single file to an older version, do git checkout HEAD~10 -- Node.js to pull that into the current workspace and then commit that, saying you're reverting all the changes since... you get the idea.

If you just want to remove a bunch of recent commits, you can git reset --hard HEAD~10 or whatever to revert back to that old version.


You already have the base of your code in the initial commit.

One way to do this is to add a tag to the tip of your current branch and then use git-reset --hard to back your project out to the very beginning and then continue from there.

I prefer to use tags in this case because as long as you have a tag the commits will be rooted and won't be garbage collected. Also, if you use branches a lot, then you aren't always distracted by the dead branch as tags don't show up in branch lists.


You can copy over the old git repo, rename the old branches (to avoid future confusion) and then use either git cherry-pick [rev] or git checkout [rev] -- [filename] depending on whether you want to pick specific commits or obtain files as they were in a specific state. As a third possibility, you can git rebase --interactive to eliminate broken commits and rearrange the others to your needs.

I can also recommend using the workflow described in A successful Git branching model, i.e. (among other suggestions) use throw-away branches for experimental features and don't merge them back until you know they are ok:

Does Git have a workflow for "restarting" your project?


(source: nvie.com)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜