如何撤消Git中最近提交的提交?

[英]How do I undo the most recent commits in Git?


I accidentally committed the wrong files to Git, but I haven't pushed the commit to the server yet.

我不小心将错误的文件提交给Git,但我还没有将提交推送到服务器。

How can I undo those commits from the local repository?

如何从本地存储库中撤消这些提交?

76 个解决方案

#1


Undo a commit and redo

$ git commit -m "Something terribly misguided"             # (1)
$ git reset HEAD~                                          # (2)
<< edit files as necessary >>                              # (3)
$ git add ...                                              # (4)
$ git commit -c ORIG_HEAD                                  # (5)
  1. This is what you want to undo
  2. 这是您要撤消的内容

  3. This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status, and you'll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message1, you could use git reset --soft HEAD~ instead, which is like git reset HEAD~ (where HEAD~ is the same as HEAD~1) but leaves your existing changes staged.
  4. 这会使您的工作树(磁盘上文件的状态)保持不变但撤消提交并保留您未提交的更改(因此它们将显示为“git status中的”未提交的更改“,您需要在提交之前再次添加它们)。如果你只想在上一次提交中添加更多更改,或者更改提交message1,你可以使用git reset --soft HEAD~代替,这就像git reset HEAD~(其中HEAD~与HEAD~1相同)但是你现有的改变是暂时的。

  5. Make corrections to working tree files.
  6. 对工作树文件进行更正。

  7. git add anything that you want to include in your new commit.
  8. git添加您想要包含在新提交中的任何内容。

  9. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.
  10. 提交更改,重用旧的提交消息。重置将旧头复制到.git / ORIG_HEAD;使用-c ORIG_HEAD进行提交将打开一个编辑器,该编辑器最初包含来自旧提交的日志消息,并允许您对其进行编辑。如果您不需要编辑消息,则可以使用-C选项。

Beware however that if you have added any new changes to the index, using commit --amend will add them to your previous commit.

但请注意,如果您已向索引添加任何新更改,则使用commit --amend会将它们添加到您之前的提交中。

If the code is already pushed to your server and you have permissions to overwrite history (rebase) then:

如果代码已经推送到您的服务器并且您有权覆盖历史记录(rebase),那么:

git push origin master --force

You can also look at this answer:

你也可以看看这个答案:

How to move HEAD back to a previous location? (Detached head)

如何将HEAD移回以前的位置? (独立头)

The above answer will show you git reflog which is used to find out what is the SHA-1 which you wish to revert to. Once you found the point to which you wish to undo to use the sequence of commands as explained above.

上面的答案将向您展示git reflog,它用于找出您希望恢复的SHA-1。一旦找到要撤消的点,就可以使用上面解释的命令序列。


1 Note, however, that you don't need to reset to an earlier commit if you just made a mistake in your commit message. The easier option is to git reset (to upstage any changes you've made since) and then git commit --amend, which will open your default commit message editor pre-populated with the last commit message.

1但是,请注意,如果您在提交消息中犯了错误,则无需重置为先前的提交。更简单的选择是git reset(为了更新你之后所做的任何更改),然后是git commit --amend,这将打开预先填充了最后一次提交消息的默认提交消息编辑器。

#2


Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand.

如果你不知道如何工作,撤销提交有点可怕。但是如果你理解的话,这实际上非常容易。

Say you have this, where C is your HEAD and (F) is the state of your files.

假设你有这个,其中C是你的HEAD,(F)是你文件的状态。

   (F)
A-B-C
    ↑
  master

You want to nuke commit C and never see it again. You do this:

你想要提交C并且永远不会再看到它。你做这个:

git reset --hard HEAD~1

The result is:

结果是:

 (F)
A-B
  ↑
master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

现在B是HEAD。因为您使用了--hard,所以您的文件将重置为它们在提交B时的状态。

Ah, but suppose commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:

啊,但是假设提交C不是灾难,但只是有点偏。您希望撤消提交,但在进行更好的提交之前,请保留更改以进行一些编辑。从这里开始,用C作为你的HEAD:

   (F)
A-B-C
    ↑
  master

You can do this, leaving off the --hard:

你可以做到这一点,离开--hard:

git reset HEAD~1

In this case the result is:

在这种情况下,结果是:

   (F)
A-B-C
  ↑
master

In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven't lost a thing!

在这两种情况下,HEAD只是指向最新提交的指针。当你执行git reset HEAD~1时,你告诉Git将HEAD指针移回一次提交。但是(除非你使用--hard)你保留文件原样。所以现在git status显示你已经检查过C的变化。你没有丢失任何东西!

For the lightest touch, you can even undo your commit but leave your files and your index:

对于最轻微的触摸,您甚至可以撤消提交,但保留文件和索引:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

这不仅会使您的文件单独存在,甚至会使您的索引单独存在。当你执行git status时,你会看到索引中的文件和以前一样。实际上,在这个命令之后,你可以执行git commit,然后你将重做你刚刚提交的相同提交。

One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

还有一件事:假设你在第一个例子中销毁了一个提交,但后来发现你需要它呢?运气好,对吗?

Nope, there's still a way to get it back. Type git reflog and you'll see a list of (partial) commit shas (that is, hashes) that you've moved around in. Find the commit you destroyed, and do this:

不,还有办法让它回来。输入git reflog,你会看到你已经移动的(部分)提交shas(即哈希)列表。找到你销毁的提交,并执行以下操作:

git checkout -b someNewBranchName shaYouDestroyed

You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.

你现在已经复活了这个提交。提交实际上并没有在Git中被摧毁大约90天,所以你通常可以回去拯救一个你并不想要摆脱的东西。

#3


This took me a while to figure out, so maybe this will help someone...

我花了一段时间才弄明白,所以也许这会帮助别人......

There are two ways to "undo" your last commit, depending on whether or not you have already made your commit public (pushed to your remote repository):

根据您是否已将提交公开(推送到远程存储库),有两种方法可以“撤消”上次提交:

How to undo a local commit

Let's say I committed locally, but now want to remove that commit.

假设我在本地提交,但现在想删除该提交。

git log
    commit 101: bad commit    # latest commit, this would be called 'HEAD'
    commit 100: good commit   # second to last commit, this is the one we want

To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD:

要将所有内容恢复到上次提交之前的状态,我们需要在HEAD之前重置为提交:

git reset --soft HEAD^     # use --soft if you want to keep your changes
git reset --hard HEAD^     # use --hard if you don't care about keeping the changes you made

Now git log will show that our last commit has been removed.

现在git log将显示我们的上一次提交已被删除。

How to undo a public commit

If you have already made your commits public, you will want to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).

如果您已将提交公开,则需要创建一个新的提交,它将“还原”您在先前提交(当前HEAD)中所做的更改。

git revert HEAD

Your changes will now be reverted and ready for you to commit:

您的更改现在将被还原并准备好您提交:

git commit -m 'restoring the file I removed by accident'
git log
    commit 102: restoring the file I removed by accident
    commit 101: removing a file we don't need
    commit 100: adding a file that we need

For more info, check out Git Basics - Undoing Things

有关更多信息,请查看Git Basics - Undoing Things

#4


Add/remove files to get things the way you want:

添加/删除文件以获得您想要的方式:

git rm classdir
git add sourcedir

Then amend the commit:

然后修改提交:

git commit --amend

The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place.

之前的错误提交将被编辑以反映新的索引状态 - 换句话说,它就像你从来没有犯过错误。

Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.

请注意,只有在尚未推送的情况下才应执行此操作。如果你已推,那么你只需要正常提交修复。

#5


git rm yourfiles/*.class
git commit -a -m "deleted all class files in folder 'yourfiles'"

or

git reset --hard HEAD~1

Warning: The above command will permanently remove the modifications to the .java files (and any other files) that you wanted to commit.

警告:上述命令将永久删除您要提交的.java文件(以及任何其他文件)的修改。

The hard reset to HEAD-1 will set your working copy to the state of the commit before your wrong commit.

对HEAD-1进行硬重置会在错误提交之前将工作副本设置为提交状态。

#6


To change the last commit

Replace the files in the index:

替换索引中的文件:

git rm --cached *.class
git add *.java

Then, if it's a private branch, amend the commit:

然后,如果它是一个私有分支,修改提交:

git commit --amend

Or, if it's a shared branch, make a new commit:

或者,如果它是共享分支,则进行新的提交:

git commit -m 'Replace .class files with .java files'


(to change a previous commit, use the awesome interactive rebase)

(要更改先前的提交,请使用令人敬畏的交互式rebase)


ProTip™:   Add *.class to a gitignore to stop this happening again.

ProTip™:将* .class添加到gitignore以再次阻止这种情况发生。


To revert a commit

Amending a commit is the ideal solution if you need to change the last commit, but a more general solution is reset.

如果您需要更改上次提交,则修改提交是理想的解决方案,但会重置更通用的解决方案。

You can reset git to any commit with:

您可以将git重置为任何提交:

git reset @~N

Where N is the number of commits before HEAD, and @~ resets to the previous commit.

其中N是HEAD之前的提交数,@〜重置为上一次提交。

So, instead of amending the commit, you could use:

因此,您可以使用以下命令而不是修改提交:

git reset @~
git add *.java
git commit -m "Add .java files"

Check out git help reset, specifically the sections on --soft --mixed and --hard, for a better understanding of what this does.

查看git help reset,特别是关于--soft --mixed和--hard的部分,以便更好地理解它的作用。

Reflog

If you mess up, you can always use the reflog to find dropped commits:

如果你陷入困境,你总是可以使用reflog来查找丢弃的提交:

$ git reset @~
$ git reflog
c4f708b HEAD@{0}: reset: moving to @~
2c52489 HEAD@{1}: commit: added some .class files
$ git reset 2c52489
... and you're back where you started


#7


Use git revert <commit-id>

使用git revert

To get the commit ID, just use git log

要获取提交ID,只需使用git log

#8


If you are planning to undo a local commit entirely, whatever you change you did on the commit, and if you don't worry anything about that, just do the following command.

如果您计划完全撤消本地提交,无论您在提交时做了什么更改,如果您对此没有任何担心,只需执行以下命令即可。

git reset --hard HEAD^1

(This command will ignore your entire commit and your changes will be lost completely from your local working tree). If you want to undo your commit, but you want your changes in the staging area (before commit just like after git add) then do the following command.

(此命令将忽略您的整个提交,您的更改将完全从本地工作树中丢失)。如果要撤消提交,但希望在暂存区域中进行更改(在提交之前,就像在git add之后),请执行以下命令。

git reset --soft HEAD^1

Now your committed files come into the staging area. Suppose if you want to upstage the files, because you need to edit some wrong content, then do the following command

现在,您提交的文件将进入暂存区域。假设您想要更新文件,因为您需要编辑一些错误的内容,请执行以下命令

git reset HEAD

Now committed files to come from the staged area into the unstaged area. Now files are ready to edit, so whatever you change, you want to go edit and added it and make a fresh/new commit.

现在提交的文件从分段区域进入未分区区域。现在文件已经可以编辑了,所以无论你改变什么,你都要编辑并添加它并进行新的/新的提交。

More

#9


If you have Git Extras installed, you can run git undo to undo the latest commit. git undo 3 will undo the last 3 commits.

如果安装了Git Extras,则可以运行git undo来撤消最新的提交。 git undo 3将撤消最后3次提交。

#10


I wanted to undo the lastest 5 commits in our shared repository. I looked up the revision id that I wanted to rollback to. Then I typed in the following.

我想在我们的共享存储库中撤消最新的5次提交。我查找了想要回滚的修订版ID。然后我键入以下内容。

prompt> git reset --hard 5a7404742c85
HEAD is now at 5a74047 Added one more page to catalogue
prompt> git push origin master --force
Total 0 (delta 0), reused 0 (delta 0)
remote: bb/acl: neoneye is allowed. accepted payload.
To git@bitbucket.org:thecompany/prometheus.git
 + 09a6480...5a74047 master -> master (forced update)
prompt>

#11


I prefer to use git rebase -i for this job, because a nice list pops up where I can choose the commits to get rid of. It might not be as direct as some other answers here, but it just feels right.

我更喜欢使用git rebase -i来完成这项工作,因为会弹出一个很好的列表,我可以选择要删除的提交。它可能不像其他答案那样直接,但它感觉恰到好处。

Choose how many commits you want to list, then invoke like this (to enlist last three)

选择要列出的提交数量,然后像这样调用(以获取最后三个)

git rebase -i HEAD~3

Sample list

pick aa28ba7 Sanity check for RtmpSrv port
pick c26c541 RtmpSrv version option
pick 58d6909 Better URL decoding support

Then Git will remove commits for any line that you remove.

然后Git将删除您删除的任何行的提交。

#12


How to fix the previous local commit

Use git-gui (or similar) to perform a git commit --amend. From the GUI you can add or remove individual files from the commit. You can also modify the commit message.

使用git-gui(或类似的)来执行git commit --amend。从GUI中,您可以从提交中添加或删除单个文件。您还可以修改提交消息。

How to undo the previous local commit

Just reset your branch to the previous location (for example, using gitk or git rebase). Then reapply your changes from a saved copy. After garbage collection in your local repository, it will be like the unwanted commit never happened. To do all of that in a single command, use git reset HEAD~1.

只需将分支重置为上一个位置(例如,使用gitk或git rebase)。然后从保存的副本中重新应用更改。在本地存储库中进行垃圾收集之后,就像从未发生过意外提交一样。要在单个命令中执行所有操作,请使用git reset HEAD~1。

Word of warning: Careless use of git reset is a good way to get your working copy into a confusing state. I recommend that Git novices avoid this if they can.

警告:不小心使用git reset是将工作副本置于混乱状态的好方法。我建议Git新手尽可能避免这种情况。

How to undo a public commit

Perform a reverse cherry pick (git-revert) to undo the changes.

执行反向樱桃选择(git-revert)以撤消更改。

If you haven't yet pulled other changes onto your branch, you can simply do...

如果您还没有将其他更改添加到您的分支,您可以简单地做...

git revert --no-edit HEAD

Then push your updated branch to the shared repository.

然后将更新的分支推送到共享存储库。

The commit history will show both commits, separately.

提交历史记录将分别显示两个提交。


Advanced: Correction of the private branch in public repository

This can be dangerous -- be sure you have a local copy of the branch to repush.

Also note: You don't want to do this if someone else may be working on the branch.

另请注意:如果其他人可能正在分支机构工作,您不希望这样做。

git push --delete (branch_name) ## remove public version of branch

Clean up your branch locally then repush...

在当地清理您的分支,然后重新抢购......

git push origin (branch_name)

In the normal case, you probably needn't worry about your private-branch commit history being pristine. Just push a followup commit (see 'How to undo a public commit' above), and later, do a squash-merge to hide the history.

在正常情况下,您可能不必担心您的私有分支提交历史是原始的。只需推送一个后续提交(参见上面的'如何撤消公共提交'),然后进行压缩合并以隐藏历史记录。

#13


If you have committed junk but not pushed,

如果你犯了垃圾而没有推,

git reset --soft HEAD~1

HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash if you want to reset to. --soft option will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.

HEAD~1是head之前提交的简写。或者,如果要重置为,则可以参考哈希的SHA-1。 --soft选项将删除提交,但它将保留所有已更改的文件“要提交的更改”,因为git status会将其设置为。

If you want to get rid of any changes to tracked files in the working tree since the commit before head use "--hard" instead.

如果你想要去除工作树中跟踪文件的任何更改,因为在头部之前提交“--hard”之前。

OR

If you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

如果你已经推了并且有人拉了,这通常是我的情况,你不能使用git reset。但是你可以做一个git revert,

git revert HEAD

This will create a new commit that reverses everything introduced by the accidental commit.

这将创建一个新的提交,以反转意外提交引入的所有内容。

#14


If you want to permanently undo it and you have cloned some repository

如果要永久撤消它并且已经克隆了一些存储库

The commit id can be seen by

提交ID可以通过以下方式查看

git log 

Then you can do -

然后你可以做 -

git reset --hard <commit_id>

git push origin <branch_name> -f

#15


On SourceTree (GUI for GitHub), you may right-click the commit and do a 'Reverse Commit'. This should undo your changes.

在SourceTree(GitHub的GUI)上,您可以右键单击提交并执行“反向提交”。这应该撤消您的更改。

On the terminal:

在终端上:

You may alternatively use:

您也可以使用:

git revert

Or:

git reset --soft HEAD^ # Use --soft if you want to keep your changes.
git reset --hard HEAD^ # Use --hard if you don't care about keeping your changes.

#16


A single command:

单个命令:

git reset --soft 'HEAD^' 

It works great to undo the last local commit!

它可以很好地撤消上一次本地提交!

#17


Just reset it doing the command below using git:

只需使用git重置它,执行以下命令:

git reset --soft HEAD~1

Explain: what git reset does, it's basically reset to any commit you'd like to go back to, then if you combine it with --soft key, it will go back, but keep the changes in your file(s), so you get back to the stage which the file was just added, HEAD is the head of the branch and if you combine with ~1 (in this case you also use HEAD^), it will go back only one commit which what you want...

解释:git reset的作用是什么,它基本上会重置为你想要返回的任何提交,然后如果你将它与--soft键结合使用,它会返回,但保留你文件中的更改,所以你回到刚刚添加文件的阶段,HEAD是分支的头部,如果你与~1结合(在这种情况下你也使用HEAD ^),它将只返回你想要的一个提交。 ..

I create the steps in the image below in more details for you, including all steps that may happens in real situations and committing the code:

我将在下面的图片中为您创建更多详细信息,包括在实际情况下可能发生的所有步骤并提交代码:

How to undo the last commits in Git?

#18


How to undo the last Git commit?

如何撤消上次Git提交?

To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD.

要将所有内容恢复到上次提交之前的状态,我们需要在HEAD之前重置为提交。

  1. If you don't want to keep your changes that you made:

    如果您不想保留所做的更改:

    git reset --hard HEAD^
    
  2. If you want to keep your changes:

    如果您想保留更改:

    git reset --soft HEAD^
    

Now check your git log. It will show that our last commit has been removed.

现在检查你的git日志。它将显示我们的上一次提交已被删除。

#19


Use reflog to find a correct state

使用reflog查找正确的状态

git reflog

reflog before REFLOG BEFORE RESET

重置前的REFLOG

Select the correct reflog (f3cb6e2 in my case) and type

选择正确的reflog(在我的例子中为f3cb6e2)并输入

git reset --hard f3cb6e2

After that the repo HEAD will be reset to that HEADid reset effect LOG AFTER RESET

之后,repo HEAD将重置为HEADID LOGET RESET RESET

Finally the reflog looks like the picture below

最后,reflog如下图所示

reflog after REFLOG FINAL

#20


"Reset the working tree to the last commit"

“将工作树重置为上次提交”

git reset --hard HEAD^ 

"Clean unknown files from the working tree"

“从工作树中清除未知文件”

git clean    

see - Git Quick Reference

看 - Git快速参考

NOTE: This command will delete your previous commit, so use with caution! git reset --hard is safer –

注意:此命令将删除您之前的提交,因此请谨慎使用! git reset --hard更安全 -

#21


First run:

git reflog

It will show you all the possible actions you have performed on your repository, for example, commit, merge, pull, etc.

它将向您显示您在存储库中执行的所有可能操作,例如,提交,合并,拉取等。

Then do:

git reset --hard ActionIdFromRefLog

#22


Undo last commit:

git reset --soft HEAD^ or git reset --soft HEAD~

git reset --soft HEAD ^或git reset --soft HEAD~

This will undo the last commit.

这将撤消上次提交。

Here --soft means reset into staging.

这里--soft意味着重置为暂存。

HEAD~ or HEAD^ means to move to commit before HEAD.

HEAD~或HEAD ^表示在HEAD之前移动到提交。


Replace last commit to new commit:

git commit --amend -m "message"

It will replace the last commit with the new commit.

它将用新提交替换最后一次提交。

#23


Another way:

Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree I right-clicked on the and selected "Reset BRANCHNAME to this commit".

检查要还原的分支,然后将本地工作副本重新设置为您希望成为远程服务器上最新版本的提交(之后的所有内容将再次出现)。为此,在SourceTree中我右键单击并选择“将BRANCHNAME重置为此提交”。

Then navigate to your repository's local directory and run this command:

然后导航到存储库的本地目录并运行以下命令:

git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME

This will erase all commits after the current one in your local repository but only for that one branch.

这将删除本地存储库中当前提交之后的所有提交,但仅删除该一个分支。

#24


Type git log and find the last commit hash code and then enter:

输入git log并找到最后一次提交哈希码,然后输入:

git reset <the previous co>

#25


In my case I accidentally committed some files I did not want to. So I did the following and it worked:

在我的情况下,我不小心提交了一些我不想要的文件。所以我做了以下工作:

git reset --soft HEAD^
git rm --cached [files you do not need]
git add [files you need]
git commit -c ORIG_HEAD

Verify the results with gitk or git log --stat

使用gitk或git log --stat验证结果

#26


Use SourceTree (graphical tool for Git) to see your commits and tree. You can manually reset it directly by right clicking it.

使用SourceTree(Git的图形工具)查看您的提交和树。您可以通过右键单击直接手动重置它。

#27


There are two main scenarios

主要有两种情况

You haven't pushed the commit yet

你尚未推送提交

If the problem was extra files you commited (and you don't want those on repository), you can remove them using git rm and then commiting with --amend

如果问题是您提交的额外文件(并且您不希望存储库中存在这些文件),则可以使用git rm删除它们,然后使用--amend提交

git rm <pathToFile>

You can also remove entire directories with -r, or even combine with other Bash commands

您还可以使用-r删除整个目录,甚至可以与其他Bash命令结合使用

git rm -r <pathToDirectory>
git rm $(find -name '*.class')

After removing the files, you can commit, with --amend option

删除文件后,您可以使用--amend选项进行提交

git commit --amend -C HEAD # the -C option is to use the same commit message

This will rewrite your recent local commit removing the extra files, so, these files will never be sent on push and also will be removed from your local .git repository by GC.

这将重写您最近的本地提交删除额外的文件,因此,这些文件永远不会在推送时发送,也将由GC从您的本地.git存储库中删除。

You already pushed the commit

你已经推送了提交

You can apply the same solution of the other scenario and then doing git push with the -f option, but it is not recommended since it overwrites the remote history with a divergent change (it can mess your repository).

您可以应用其他方案的相同解决方案,然后使用-f选项执行git push,但不推荐使用它,因为它会以不同的更改覆盖远程历史记录(它可能会弄乱您的存储库)。

Instead, you have to do the commit without --amend (remember this about -amend`: That option rewrites the history on the last commit).

相反,你必须在没有--amend的情况下进行提交(请记住这个关于-amend`:该选项会重写最后一次提交的历史记录)。

#28


For a local commit

git reset --soft HEAD~1

or if you do not remember exactly in which commit it is, you might use

或者如果你不记得它究竟在哪个提交中,你可以使用

git rm --cached <file>

For a pushed commit

The proper way of removing files from the repository history is using git filter-branch. That is,

从存储库历史记录中删除文件的正确方法是使用git filter-branch。那是,

git filter-branch --index-filter 'git rm --cached <file>' HEAD

But I recomnend you use this command with care. Read more at git-filter-branch(1) Manual Page.

但我建议您小心使用此命令。更多信息,请访问git-filter-branch(1)手册页。

#29


Simple, run this in your command line:

很简单,在命令行中运行:

git reset --soft HEAD~ 

#30


To reset to the previous revision, permanently deleting all uncommitted changes:

要重置为先前的修订,请永久删除所有未提交的更改:

git reset --hard HEAD~1
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2018/10/19/53b44a78a01c4142efb174dbb7f34ef.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告