我试图撤销自上次提交以来的所有更改。在看完这篇文章后,我尝试了git reset -hard和git reset -hard HEAD。我回复头部现在在18c3773…但当我查看我的本地资源时,所有的文件都还在那里。我错过了什么?
当前回答
# Navigate to project root, `.` works too.
git restore *
git状态显示我有一些文件被更改,但我想摆脱这些并开始一个新的分支。直到今天,我一直在使用git重置方法,我喜欢用它来跳回其他特定的提交。
https://www.git-tower.com/learn/git/commands/git-restore/
其他回答
使用此选项可以在上次提交后删除不需要的更改。
git reset --hard HEAD
国家从一个承诺过渡到新的承诺
0. last commit,i.e. HEAD commit
1. Working tree changes, file/directory deletion,adding,modification.
2. The changes are staged in index
3. Staged changes are committed
状态转换的动作
0->1: manual file/directory operation
1->2: git add .
2->3: git commit -m "xxx"
检查差异
0->1: git diff
0->2: git diff --cached
0->1, and 0->2: git diff HEAD
last last commit->last commit: git diff HEAD^ HEAD
恢复到上次提交
2->1: git reset
1->0: git checkout . #only for tracked files/directories(actions include modifying/deleting tracked files/directories)
1->0: git clean -fdx #only for untracked files/directories(action includes adding new files/directories)
2->1, and 1->0: git reset --hard HEAD
相当于git的克隆,没有重新下载任何东西
git reset && git checkout . && git clean -fdx
Git中有三个选项可以帮助撤消本地更改。
要查看在你的工作目录中所做的更改,你应该运行git status:
git status
用git stash撤销更改 要丢弃所有本地更改,但也要保存它们以供以后使用,您可以运行git stash命令:
git stash
用git签出撤消更改 要永久丢弃对文件的本地更改,您可以运行:
git checkout -- <file>
用git重置撤销更改 要永久丢弃对所有文件的所有本地更改,您可以执行:
git reset --hard
来源:https://www.w3docs.com/snippets/git/how-to-discard-unstaged-changes.html
我只是偶然发现了一个github库,它可以很容易地撤销git中的一些东西。它叫做ugit
只需输入ugit,它就会为您提供一个选项列表,您可以选择这些选项来撤销git命令
This will unstage all files you might have staged with git add: git reset This will revert all local uncommitted changes (should be executed in repo root): git checkout . You can also revert uncommitted changes only to particular file or directory: git checkout [some_dir|file.txt] Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory): git reset --hard HEAD This will remove all local untracked files, so only git tracked files remain: git clean -fdx WARNING: -x will also remove all ignored files, including ones specified by .gitignore! You may want to use -n for preview of files to be deleted.
总结一下:执行下面的命令基本上相当于从原始源代码复制一个新的git(但它不会重新下载任何东西,所以更快):
git reset
git checkout .
git clean -fdx
典型的使用是在构建脚本中,当你必须确保你的树是绝对干净的——没有任何修改或本地创建的目标文件或构建工件,并且你想让它工作得非常快,而不是每次都重新克隆整个存储库。
推荐文章
- 如何从终端/命令行调用VS代码编辑器
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- 在Jar文件中运行类
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 如何在Windows命令提示符下运行.sh ?
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 如何从命令行通过mysql运行一个查询?
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖