使用 TortoiseGit 在某个提交后删除所有内容
使用TortoiseGit(我试图避免命令行使用)如何删除从某个提交添加的所有提交,并“回到过去”(示例用法:继续做某事,我想我不喜欢它的地方)正要去,并决定“回去”,不管中间的一切)。
简而言之,我希望“返回” 5 个提交,然后删除它们。
Using TortoiseGit (I'm trying to avoid command line usage) how does one delete all commits that accended from a certain commit, and "get back in the past" (example usage: continued doing something, figured I didn't like where it was going, and decided to go "back" disregarding all in between).
In short I wish to go 5 commits "back", and delete them afterwards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
转到 TortoiseGit ->显示日志。选择您要保留的最近提交,上面的所有内容都将被删除。然后右键单击它并选择“将“master”重置为此”并选择“硬”。但要小心,因为一旦执行此操作,之后的所有提交都将永远丢失,除非它们存储在另一个分支中的某个地方。
Go to TortoiseGit -> Show log. Select the most recent commit you want to keep, everything above will be erased. Then right click on it and select "Reset "master" to this" and choose Hard. Be careful though because once you do this all the commits after it will be forever lost unless they are stored off in another branch somewhere.
我从来没有使用过 Tortoise 任何东西(除非你算上电子乐组),但我会给你这些信息,以防你找不到使用 GUI 的方法,或者你最终决定使用 CLI。
正如 @Tuxified 提到的,您可以使用 git reset --hard来完成此操作。您需要指定一个提交,这可以通过任何一种令人生畏的可能方式来完成;最常见的形式有
HEAD~4
和deadbeef42
,前者指定当前分支头之前的 4 个提交,后者指定 SHA1 以 0xdeadbeef42 开头的提交。如果您运行的是 Linux 或 OSX,则可以通过“指定修订版本”下的man git-rev-parse
获取有关提交说明符的完整详细信息。您还可以重命名当前分支 (,然后将其签出。因此,这里的最终效果与第一个选项相同,只是您仍然将旧的更改保存在分支
gitbranch -m new_branch_name
),然后创建一个新分支,其头部位于您要恢复到的位置。优点是,如果您最终想要使用您在那里所做的全部或部分内容,您仍然可以轻松访问它;如果你输错了提交,你就可以避免一个大灾难时刻。另外,git 分支是超轻量级的,所以唯一真正的缺点是会列出另一个分支。要创建新的 master 分支,您可以使用 gitbranch masternew_branch_name
中。如果您愿意,您甚至可以稍后在新提交到master
后将这些更改合并回来。请注意,这两种技术都被视为“重写历史记录”,如果您与其他人共享您的存储库,则会导致问题。如果你很聪明,并推送到云城或另一台电脑上的备份存储库,那么你必须先修复该端的问题,然后才能再次推送。
这可能比您现在需要的信息多得多,但如果您最终经常使用 git,您可能会在某个时候想要学习这些东西。抱歉我不知道如何使用Tortoise...
I've never used Tortoise anything (unless you count the electronica group), but I'll give you this info in case you don't find a way to do it with the GUI or you end up deciding to go CLI after all.
As @Tuxified mentions, you can accomplish this with
git reset --hard <COMMIT>
. You need to specify a commit, which can be done in any of an intimidating panoply of possible ways; the most common have forms likeHEAD~4
, which specifies a commit 4 commits before the head of the current branch, anddeadbeef42
, which specifies a commit whose SHA1 starts with 0xdeadbeef42. If you're running linux or OSX, you can get the full details on commit specifiers viaman git-rev-parse
, under "SPECIFYING REVISIONS".You can also rename the current branch (
git branch -m new_branch_name
) and then create a new branch with its head at the place you want to revert to. The advantage is that if you end up wanting to use all or some of the stuff you did there, you can still readily access it; and if you typo the commit, you're saved from a big holycrap moment. Plus git branches are super lightweight so the only real downside is that there will be another branch listed. To make the new master branch you would usegit branch master <COMMIT>
, and then you check it out. So the net effect here is the same as the first option, except that you still have your old changes saved in the branchnew_branch_name
. You can even merge those changes back in later, after new commits tomaster
, if you feel like it.Note that either of these techniques are considered "rewriting history" and will cause issues if you're sharing your repo with others. If you're being smart and pushing to a backup repo in cloud city or another pc, you will have to fix things on that end before you can push to it again.
This is probably way more info than you need right now, but if you end up using git a lot you'll probably want to learn this stuff at some point. Sorry I don't know how to use Tortoise...