如何恢复未提交的更改(包括文件和文件夹)?

发布于 2024-11-03 19:33:45 字数 51 浏览 4 评论 0原文

是否有 Git 命令可以恢复工作树和索引中所有未提交的更改,并删除新创建的文件和文件夹?

Is there a Git command to revert all uncommitted changes in a working tree and index and to also remove newly created files and folders?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

残月升风 2024-11-10 19:33:45

您可以运行这两个命令:

# Revert changes to modified files.
git reset --hard

# Remove all untracked files and directories.
# '-f' is force, '-d' is remove directories.
git clean -fd

You can run these two commands:

# Revert changes to modified files.
git reset --hard

# Remove all untracked files and directories.
# '-f' is force, '-d' is remove directories.
git clean -fd
巴黎盛开的樱花 2024-11-10 19:33:45

如果您只想恢复当前工作目录中的更改,请使用

git checkout -- .

在此之前,您可以列出将恢复的文件,而无需实际执行任何操作,只是为了检查会发生什么,使用:

git checkout --

If you want to revert the changes only in the current working directory, use

git checkout -- .

And before that, you can list the files that will be reverted without actually making any action, just to check what will happen, with:

git checkout --
葬シ愛 2024-11-10 19:33:45

使用“git checkout -- ...”放弃工作目录中的更改

git checkout -- app/views/posts/index.html.erb

git checkout -- *

删除 git status 中对未暂存文件所做的所有更改,例如

modified:    app/controllers/posts.rb
modified:    app/views/posts/index.html.erb

Use "git checkout -- ..." to discard changes in working directory

git checkout -- app/views/posts/index.html.erb

or

git checkout -- *

removes all changes made to unstaged files in git status eg

modified:    app/controllers/posts.rb
modified:    app/views/posts/index.html.erb
小姐丶请自重 2024-11-10 19:33:45

一种重要的方法是运行这两个命令:

  1. git stash 这会将您的更改移动到存储中,使您回到 HEAD 的状态
  2. git stash drop 这将删除在最后一个命令中创建的最新存储。

One non-trivial way is to run these two commands:

  1. git stash This will move your changes to the stash, bringing you back to the state of HEAD
  2. git stash drop This will delete the latest stash created in the last command.
素衣风尘叹 2024-11-10 19:33:45

Git 2.23 引入了 git Restore 命令来恢复工作树文件。

恢复当前目录中的所有文件:

git restore .

如果要恢复所有C源文件以匹配索引中的版本,可以这样做

git restore '*.c'

Git 2.23 introduced the git restore command to restore working tree files.

To restore all files in the current directory:

git restore .

If you want to restore all C source files to match the version in the index, you can do

git restore '*.c'
怪我鬧 2024-11-10 19:33:45
git clean -fd

没有帮助,新文件仍然存在。我完全删除了所有工作树,然后

git reset --hard

参见“https://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git/673420#673420” 添加 -x 选项进行清理的建议:

git clean -fdx

注意 -x 标志将删除 Git 忽略的所有文件,因此小心(参见我参考的答案中的讨论)。

git clean -fd

didn't help, and new files remained. I totally deleted all the working tree and then

git reset --hard

See "https://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git/673420#673420" for advice to add the -x option to clean:

git clean -fdx

Note -x flag will remove all files ignored by Git, so be careful (see the discussion in the answer I refer to).

随梦而飞# 2024-11-10 19:33:45

我认为你可以使用以下命令:git reset --hard

I think you can use the following command: git reset --hard

爱情眠于流年 2024-11-10 19:33:45

如果您有未提交的更改(仅在工作副本中)并且希望恢复到最新提交中的副本,请执行以下操作:

git checkout filename

If you have an uncommitted change (it’s only in your working copy) that you wish to revert to the copy in your latest commit, do the following:

git checkout filename
凹づ凸ル 2024-11-10 19:33:45

请注意,可能仍然有一些文件看起来不会消失 - 它们可能未经编辑,但由于 CRLF / LF 更改,Git 可能已将它们标记为正在编辑。查看您最近是否对 .gitattributes 进行了一些更改。

就我而言,我已将 CRLF 设置添加到 .gitattributes 文件中,因此所有文件都保留在“已修改文件”列表中。更改 .gitattributes 设置使它们消失。

Please note that there might still be files that won't seem to disappear - they might be unedited, but Git might have marked them as being edited because of CRLF / LF changes. See if you've made some changes in .gitattributes recently.

In my case, I've added CRLF settings into the .gitattributes file and all the files remained in the "modified files" list because of this. Changing the .gitattributes settings made them disappear.

梦情居士 2024-11-10 19:33:45

您只需使用以下 Git 命令即可恢复存储库中所做的所有未提交的更改:

git checkout .

示例:

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   application/controllers/Drivers.php
        modified:   application/views/drivers/add.php
        modified:   application/views/drivers/load_driver_info.php
        modified:   uploads/drivers/drivers.xlsx

no changes added to commit (use "git add" and/or "git commit -a")

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git checkout .

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean

You can just use following Git command which can revert back all the uncommitted changes made in your repository:

git checkout .

Example:

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   application/controllers/Drivers.php
        modified:   application/views/drivers/add.php
        modified:   application/views/drivers/load_driver_info.php
        modified:   uploads/drivers/drivers.xlsx

no changes added to commit (use "git add" and/or "git commit -a")

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git checkout .

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean
慢慢从新开始 2024-11-10 19:33:45

来自 Git 帮助:

 Changes to be committed:
      (use "git restore --staged <file>..." to unstage)

    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)

From Git help:

 Changes to be committed:
      (use "git restore --staged <file>..." to unstage)

    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
谈场末日恋爱 2024-11-10 19:33:45

我通常使用这种效果很好的方法:

mv fold/file /tmp
git checkout fold/file

I usually use this way that works well:

mv fold/file /tmp
git checkout fold/file
鲜肉鲜肉永远不皱 2024-11-10 19:33:45

安全而漫长的方法:

  1. git Branch todelete
  2. git checkout todelete
  3. git add .
  4. git commit -m "我做了一件坏事,抱歉”
  5. git checkoutdevelopment
  6. gitbranch -D todelete

A safe and long way:

  1. git branch todelete
  2. git checkout todelete
  3. git add .
  4. git commit -m "I did a bad thing, sorry"
  5. git checkout develop
  6. git branch -D todelete
过气美图社 2024-11-10 19:33:45

用途:

git reset HEAD filepath

例如:

git reset HEAD om211/src/META-INF/persistence.xml

Use:

git reset HEAD filepath

For example:

git reset HEAD om211/src/META-INF/persistence.xml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文