删除本地 Git 更改的各种方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22620393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Various ways to remove local Git changes
提问by spiderman
I just cloned a git repository and checked out a branch. I worked on it, and then decided to remove all my local changes, as I wanted the original copy.
我刚刚克隆了一个 git 存储库并签出了一个分支。我进行了研究,然后决定删除所有本地更改,因为我想要原始副本。
In short, I had to do the following two commands to remove my local changes
简而言之,我必须执行以下两个命令才能删除我的本地更改
git checkout .
git clean -f
My question is,
我的问题是,
(1) Is this the correct approach in getting rid of local changes, or else please let me know the correct approach.
(1)这是摆脱局部变化的正确方法,否则请让我知道正确的方法。
(2) when do we use git reset --hard
as i am able to reset even without this command
(2) 我们什么时候使用,git reset --hard
因为即使没有这个命令我也可以重置
Thanks
谢谢
*Solution : Major Edit(s): 03/26 : *Replaced many of vague terms with git specific terminology [tracked/untracked/staged/unstaged]
*解决方案:主要编辑:03/26:*用 git 特定术语替换了许多模糊的术语 [tracked/untracked/staged/unstaged]
There could be only three categories of files when we make local changes:
当我们进行本地更改时,可能只有三类文件:
Type 1. Staged Tracked files
Type 2. Unstaged Tracked files
Type 3. Unstaged UnTracked files a.k.a UnTracked files
类型 1. 暂存跟踪文件
类型 2. Unstaged Tracked 文件
类型 3. Unstaged UnTracked 文件又名 UnTracked 文件
- Staged - Those that are moved to staging area/ Added to index
- Tracked - modified files
- UnTracked - new files. Always unstaged. If staged, that means they are tracked.
- 暂存 - 那些被移动到暂存区/添加到索引
- 跟踪 - 修改过的文件
- UnTracked - 新文件。始终未上演。如果上演,这意味着他们被跟踪。
What each commands do:
每个命令的作用:
git checkout .
- Removes Unstaged Tracked files ONLY [Type 2]git clean -f
- Removes Unstaged UnTracked files ONLY [Type 3]git reset --hard
- Removes Staged Tracked and UnStaged Tracked files ONLY[Type 1, Type 2]git stash -u
- Removes all changes [Type 1, Type 2, Type 3]
git checkout .
- 仅删除未暂存的跟踪文件 [类型 2]git clean -f
- 仅删除 Unstaged UnTracked 文件 [Type 3]git reset --hard
- 仅删除暂存跟踪和未暂存跟踪文件[类型 1,类型 2]git stash -u
- 删除所有更改 [类型 1、类型 2、类型 3]
Conclusion:
结论:
It's clear that we can use either
很明显,我们可以使用
(1) combination of `git clean -f` and `git reset --hard`
OR
或者
(2) `git stash -u`
to achieve the desired result.
以达到预期的效果。
Note: Stashing, as the word means 'Store (something) safely and secretly in a specified place.' This can always be retrieved using git stash pop
.
So choosing between the above two options is developer's call.
注意:藏匿,因为这个词的意思是“将(某物)安全秘密地存放在指定的地方”。这始终可以使用 检索git stash pop
。因此,在上述两个选项之间进行选择是开发人员的要求。
Thank you Christoph and Frederik Sch?ning.
谢谢 Christoph 和 Frederik Sch?ning。
Edit: 03/27
编辑:03/27
I thought it's worth putting the 'beware' note to git clean -f
我认为值得把“当心”注释放在git clean -f
git clean -f
git clean -f
There is no going back. Use -n
or --dry-run
to preview the damage you'll do.
没有回头路了。使用-n
或--dry-run
预览您将造成的伤害。
If you want to also remove directories, run git clean -f -d
如果您还想删除目录,请运行 git clean -f -d
If you just want to remove ignored files, run git clean -f -X
如果您只想删除被忽略的文件,请运行 git clean -f -X
If you want to remove ignored as well as non-ignored files, run git clean -f -x
如果要删除忽略和未忽略的文件,请运行 git clean -f -x
reference : more on git clean
: How to remove local (untracked) files from the current Git working tree?
参考:更多关于git clean
:如何从当前 Git 工作树中删除本地(未跟踪)文件?
Edit: 05/20/15
编辑:05/20/15
Discarding all local commits on this branch[Removing local commits]
丢弃此分支上的所有本地提交[删除本地提交]
In order to discard all local commits on this branch, to make the local branch identical
to the "upstream" of this branch, simply run git reset --hard @{u}
为了丢弃此分支上的所有本地提交,使本地分支与此分支的“上游”相同,只需运行 git reset --hard @{u}
Reference: http://sethrobertson.github.io/GitFixUm/fixup.html
参考:http: //sethrobertson.github.io/GitFixUm/fixup.html
or do git reset --hard origin/master
[if local branch is master
]
或做git reset --hard origin/master
[如果本地分支是master
]
Note: 06/12/2015This is nota duplicate of the other SO question that's marked as duplicate. This question address how to remove local GIT changes [remove a file added, remove changes added to existing file etc and the various approaches; Where in the other SO thread only address how to remove local commit. If you added a file, and you want to remove that alone, then the other SO thread doesn't discuss about it. Hence this is not a duplicate of the other one]
注意:06/12/2015这不是标记为重复的其他 SO 问题的重复。这个问题解决了如何删除本地 GIT 更改[删除添加的文件,删除添加到现有文件的更改等以及各种方法;在其他 SO 线程中的何处仅解决如何删除本地提交。如果您添加了一个文件,并且您想单独删除它,那么另一个 SO 线程不会讨论它。因此,这不是另一个的重复]
Edit: 06/23/15
编辑:06/23/15
How to revert a commit already pushed to a remote repository?
如何恢复已经推送到远程存储库的提交?
$ git revert ab12cd15
Edit: 09/01/2015
编辑:09/01/2015
Delete a previous commit from local branch and remote branch
从本地分支和远程分支删除先前的提交
Case: You just commited a change to your local branch and immediately pushed to the remote branch, Suddenly realized , Oh no! I dont need this change. Now do what?
案例:你刚刚向本地分支提交了一个更改,并立即推送到远程分支,突然意识到,哦不!我不需要这个改变。现在做什么?
git reset --hard HEAD~1
[for deleting that commit from local branch]
git reset --hard HEAD~1
[用于从本地分支删除该提交]
git push origin HEAD --force
[both the commands must be executed. For deleting from remote branch]
git push origin HEAD --force
[必须执行这两个命令。从远程分支删除]
Whats the branch ? Its the currently checked out branch.
分支是什么?它是当前签出的分支。
Edit 09/08/2015- Remove local git merge:
编辑 09/08/2015- 删除本地 git merge:
I am on master
branch and merged master
branch with a newly working branch phase2
我在master
分支上并master
与一个新工作的分支合并phase2
$ git status
# On branch master
$ git merge phase2
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 8 commits.
Q: How to get rid of this merge?
Tried git reset --hard
and git clean -d -f
Both didn't work.
问:如何摆脱这种合并?尝试过git reset --hard
,git clean -d -f
两者都不起作用。
The only thing that workedare any of the below ones:
唯一有效的是以下任何一项:
$ git reset --hard origin/master
or
或者
$ git reset --hard HEAD~8
or
或者
$ git reset --hard 9a88396f51e2a068bb7
[sha commit code - this is the one that was present before all your merge commits happened]
$ git reset --hard 9a88396f51e2a068bb7
[sha 提交代码 - 这是所有合并提交发生之前存在的代码]
采纳答案by Frederik Struck-Sch?ning
It all depends on exactly what you are trying to undo/revert. Start out by reading the post in Ube's link. But to attempt an answer:
这完全取决于您尝试撤消/还原的内容。首先阅读宇部链接中的帖子。但是要尝试回答:
Hard reset
硬重置
git reset --hard [HEAD]
completely remove all staged and unstaged changes to tracked files.
完全删除对跟踪文件的所有暂存和未暂存更改。
I find myself often using hard resetting, when I'm like "just undo everything like if I had done a complete re-clone from the remote". In your case, where you just want your repo pristine, this would work.
我发现自己经常使用硬重置,当我想“就像从遥控器上完成完全重新克隆一样撤消所有内容”时。在您的情况下,您只希望您的回购保持原始状态,这将起作用。
Clean
干净的
git clean [-f]
Remove files that are not tracked.
删除未跟踪的文件。
For removing temporary files, but keep staged and unstaged changes to already tracked files. Most times, I would probably end up making an ignore-rule instead of repeatedly cleaning - e.g. for the bin/obj folders in a C# project, which you would usually want to exclude from your repo to save space, or something like that.
用于删除临时文件,但保留对已跟踪文件的暂存和未暂存更改。大多数情况下,我可能最终会制定一个忽略规则而不是反复清理 - 例如,对于 C# 项目中的 bin/obj 文件夹,您通常希望将其从您的存储库中排除以节省空间,或类似的东西。
The -f (force) option will also remove files, that are not tracked andare also being ignored by git though ignore-rule. In the case above, with an ignore-rule to never track the bin/obj folders, even though these folders are being ignored by git, using the force-option will remove them from your file system. I've sporadically seen a use for this, e.g. when scripting deployment, and you want to clean your code before deploying, zipping or whatever.
-f (force) 选项也将删除文件,这些文件没有被跟踪并且也被 git 通过 ignore-rule 忽略。在上述情况下,使用忽略规则从不跟踪 bin/obj 文件夹,即使这些文件夹被 git 忽略,使用 force-option 也会将它们从文件系统中删除。我偶尔会看到它的用途,例如在编写脚本部署时,并且您想在部署、压缩或其他任何之前清理您的代码。
Git clean will not touch files, that are already being tracked.
Git clean 不会触及已经被跟踪的文件。
Checkout "dot"
结帐“点”
git checkout .
I had actually never seen this notation before reading your post. I'm having a hard time finding documentation for this (maybe someone can help), but from playing around a bit, it looks like it means:
在阅读您的帖子之前,我实际上从未见过这种符号。我很难找到这方面的文档(也许有人可以提供帮助),但是稍微玩了一下,它看起来意味着:
"undo all changes in my working tree".
“撤消我的工作树中的所有更改”。
I.e. undo unstaged changes in tracked files. It apparently doesn't touch staged changes and leaves untracked files alone.
即撤消跟踪文件中未暂存的更改。它显然不会触及分阶段的更改,而是单独留下未跟踪的文件。
Stashing
藏匿
Some answers mention stashing. As the wording implies, you would probably use stashing when you are in the middle of something (not ready for a commit), and you have to temporarily switch branches or somehow work on another state of your code, later to return to your "messy desk". I don't see this applies to your question, but it's definitely handy.
一些答案提到了藏匿。正如措辞所暗示的那样,当您处于某些事情的中间(尚未准备好提交)时,您可能会使用 stash,并且您必须暂时切换分支或以某种方式处理代码的另一种状态,稍后返回到您的“混乱”状态桌子”。我认为这不适用于您的问题,但这绝对很方便。
To sum up
总结
Generally, if you are confident you have committed and maybe pushed to a remote important changes, if you are just playing around or the like, using git reset --hard HEAD
followed by git clean -f
will definitively cleanse your code to the state, it would be in, had it just been cloned and checked out from a branch. It's really important to emphasize, that the resetting will also remove staged, but uncommitted changes. It will wipe everything that has not been committed(except untracked files, in which case, use clean).
通常,如果您确信自己已提交并可能推送到远程重要更改,如果您只是在玩弄或类似,使用git reset --hard HEAD
后跟git clean -f
将明确地将您的代码清理到状态,如果它刚刚被克隆,它将处于状态并从分行结帐。需要强调的是,重置也将删除暂存但未提交的更改,这一点非常重要。它将擦除所有尚未提交的内容(未跟踪的文件除外,在这种情况下,请使用clean)。
All the other commands are there to facilitate more complex scenarios, where a granularity of "undoing stuff" is needed :)
所有其他命令都用于促进更复杂的场景,其中需要“撤消内容”的粒度:)
I feel, your question #1 is covered, but lastly, to conclude on #2: the reason you never found the need to use git reset --hard
was that you had never staged anything. Had you staged a change, neither git checkout .
nor git clean -f
would have reverted that.
我觉得,你的问题 #1 已经涵盖了,但最后,总结一下 #2:你从来没有发现需要使用的原因是你从来git reset --hard
没有上演过任何东西。如果您进行了更改,则既git checkout .
不会也git clean -f
不会还原。
Hope this covers.
希望这涵盖。
回答by spiderman
Reason for adding an answer at this moment:
此时添加答案的原因:
So far I was adding the conclusion and ‘answers' to my initial question itself, making the question very lengthy, hence moving to separate answer.
到目前为止,我正在将结论和“答案”添加到我最初的问题本身,使问题变得非常冗长,因此转向单独的答案。
I have also added more frequently used git commandsthat helps me on git, to help someone else too.
我还添加了更常用的 git 命令来帮助我使用 git,也可以帮助其他人。
Basically to clean all local commits
$ git reset --hard
and
$ git clean -d -f
基本上是为了清理所有本地提交
$ git reset --hard
和
$ git clean -d -f
Firststep before you do any commits is to configure your username and email that appears along with your commit.
第一步骤中,您做任何承诺之前是配置您的用户名和电子邮件出现与你一起提交。
#Sets the name you want attached to your commit transactions
#设置您要附加到提交事务的名称
$ git config --global user.name "[name]"
#Sets the email you want atached to your commit transactions
#设置您想要附加到提交事务的电子邮件
$ git config --global user.email "[email address]"
#List the global config
#列出全局配置
$ git config --list
#List the remote URL
#列出远程地址
$ git remote show origin
#check status
#检查状态
git status
#List all local and remote branches
#列出所有本地和远程分支
git branch -a
#create a new local branch and start working on this branch
#创建一个新的本地分支并开始在这个分支上工作
git checkout -b "branchname"
or, it can be done as a two step process
或者,它可以作为两步过程完成
create branch: git branch branchname
work on this branch: git checkout branchname
创建分支:git branch branchname
在这个分支上工作:git checkout branchname
#commit local changes[two step process:- Add the file to the index, that means adding to the staging area. Then commit the files that are present in this staging area]
#commit local changes[两步过程:-将文件添加到索引,即添加到暂存区。然后提交此暂存区中存在的文件]
git add <path to file>
git commit -m "commit message"
#checkout some other local branch
#checkout 一些其他本地分支
git checkout "local branch name"
#remove all changes in local branch[Suppose you made some changes in local branch like adding new file or modifying existing file, or making a local commit, but no longer need that]
git clean -d -f
and git reset --hard
[clean all local changes made to the local branch except if local commit]
#删除本地分支中的所有更改[假设您在本地分支中进行了一些更改,例如添加新文件或修改现有文件,或进行本地提交,但不再需要]
git clean -d -f
和git reset --hard
[清除对本地分支所做的所有本地更改,除非本地提交]
git stash -u
also removes all changes
git stash -u
也删除所有更改
Note:It's clear that we can use either
(1) combination of git clean –d –f
and git reset --hard
OR
(2) git stash -u
to achieve the desired result.
注意:很明显,我们可以使用 (1)git clean –d –f
和git reset --hard
OR (2) 的组合git stash -u
来达到预期的结果。
Note 1: Stashing, as the word means 'Store (something) safely and secretly in a specified place.' This can always be retreived using git stash pop. So choosing between the above two options is developer's call.
注1:藏匿,因为该词的意思是“将(某物)安全秘密地存放在指定的地方”。这总是可以使用 git stash pop 检索。因此,在上述两个选项之间进行选择是开发人员的要求。
Note 2: git reset --hard
will delete working directory changes. Be sure to stash any local changes you want to keep before running this command.
注2:git reset --hard
将删除工作目录更改。在运行此命令之前,请务必存储您想要保留的任何本地更改。
# Switch to the master branchand make sure you are up to date.
# 切换到 master 分支并确保你是最新的。
git checkout master
git fetch
[this may be necessary (depending on your git config) to receive updates on origin/master ]
git fetch
[这可能是必要的(取决于您的 git 配置)以接收有关 origin/master 的更新]
git pull
# Merge the feature branch into the master branch.
# 将功能分支合并到主分支。
git merge feature_branch
# Reset the master branch to origin's state.
# 将 master 分支重置为 origin 的状态。
git reset origin/master
#Accidentally deleted a file from local , how to retrieve it back?Do a git status
to get the complete filepath of the deleted resource
#不小心从本地删除了一个文件,如何找回?执行 agit status
获取已删除资源的完整文件路径
git checkout branchname <file path name>
that's it!
就是这样!
#Merge master branch with someotherbranch
#将主分支与其他分支合并
git checkout master
git merge someotherbranchname
#rename local branch
#重命名本地分支
git branch -m old-branch-name new-branch-name
#delete local branch
#删除本地分支
git branch -D branch-name
#delete remote branch
#删除远程分支
git push origin --delete branchname
or
或者
git push origin :branch-name
#revert a commit already pushed to a remote repository
#revert 已经推送到远程仓库的提交
git revert hgytyz4567
#branch from a previous commit using GIT
#branch 来自使用 GIT 的先前提交
git branch branchname <sha1-of-commit>
#Change commit message of the most recent commit that's already been pushed to remote
#更改已经推送到远程的最近提交的提交消息
git commit --amend -m "new commit message"
git push --force origin <branch-name>
# Discarding all local commits on this branch[Removing local commits]
# 丢弃此分支上的所有本地提交[移除本地提交]
In order to discard all local commits on this branch, to make the local branch identical to the "upstream" of this branch, simply run
为了丢弃此分支上的所有本地提交,使本地分支与此分支的“上游”相同,只需运行
git reset --hard @{u}
Reference: http://sethrobertson.github.io/GitFixUm/fixup.htmlor do git reset --hard origin/master
[if local branch is master]
参考:http: //sethrobertson.github.io/GitFixUm/fixup.html或者做git reset --hard origin/master
[如果本地分支是master]
# Revert a commit already pushed to a remote repository?
# 恢复已经推送到远程仓库的提交?
$ git revert ab12cd15
#Delete a previous commit from local branch and remote branch
#从本地分支和远程分支删除之前的提交
Use-Case:You just commited a change to your local branch and immediately pushed to the remote branch, Suddenly realized , Oh no! I dont need this change. Now do what?
用例:你刚刚向本地分支提交了一个更改,并立即推送到远程分支,突然意识到,哦不!我不需要这个改变。现在做什么?
git reset --hard HEAD~1
[for deleting that commit from local branch. 1 denotes the ONE commit you made]
git reset --hard HEAD~1
[用于从本地分支删除该提交。1 表示您所做的 ONE 提交]
git push origin HEAD --force
[both the commands must be executed. For deleting from remote branch]. Currently checked out branch will be referred as the branch where you are making this operation.
git push origin HEAD --force
[必须执行这两个命令。用于从远程分支中删除]。当前签出的分支将被称为您进行此操作的分支。
#Delete some of recent commits from local and remote repo and preserve to the commit that you want. ( a kind of reverting commits from local and remote)
#从本地和远程存储库中删除一些最近的提交并保留到您想要的提交。(一种从本地和远程恢复提交)
Let's assume you have 3 commits that you've pushed to remote branch named 'develop
'
假设您有 3 个提交已推送到名为“ develop
”的远程分支
commitid-1 done at 9am
commitid-2 done at 10am
commitid-3 done at 11am. // latest commit. HEAD is current here.
To revert to old commit( to change the state of branch)
恢复到旧提交(更改分支状态)
git log --oneline --decorate --graph
// to see all your commitids
git log --oneline --decorate --graph
// 查看你所有的 commitids
git clean -d -f
// clean any local changes
git clean -d -f
// 清除所有本地更改
git reset --hard commitid-1
// locally reverting to this commitid
git reset --hard commitid-1
// 本地恢复到这个 commitid
git push -u origin +develop
// push this state to remote. + to do force push
git push -u origin +develop
// 将此状态推送到远程。+ 做强制推送
# Remove local git merge:Case: I am on master branch and merged master branch with a newly working branch phase2
# 删除本地 git merge:Case: I am on master 分支并合并了 master 分支与一个新的工作分支 phase2
$ git status
On branch master
在分支主
$ git merge phase2
$ git status
$ git merge phase2
$ git status
On branch master
在分支主
Your branch is ahead of 'origin/master' by 8commits.
您的分支领先于 'origin/master' 8 次提交。
Q: How to get rid of this local git merge?Tried git reset --hard
and git clean -d -f
Both didn't work.
The only thing that workedare any of the below ones:
问:如何摆脱这个本地 git 合并?尝试过git reset --hard
,git clean -d -f
两者都不起作用。唯一有效的是以下任何一项:
$ git reset --hard origin/master
$ git reset --hard origin/master
or
或者
$ git reset --hard HEAD~8
$ git reset --hard HEAD~8
or
或者
$ git reset --hard 9a88396f51e2a068bb7
[sha commit code - this is the one that was present before all your merge commits happened]
$ git reset --hard 9a88396f51e2a068bb7
[sha 提交代码 - 这是所有合并提交发生之前存在的代码]
#create gitignore file
#创建gitignore文件
touch .gitignore
// create the file in mac or unix users
touch .gitignore
// 在 mac 或 unix 用户中创建文件
sample .gitignore contents:
示例 .gitignore 内容:
.project
*.py
.settings
Reference link to GIT cheat sheet: https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf
GIT 备忘单参考链接:https: //services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf
回答by Christoph
As with everything in git there are multiple ways of doing it. The two commands you used are one way of doing it. Another thing you could have done is simply stash them with git stash -u
. The -u
makes sure that newly added files (untracked) are also included.
与 git 中的所有内容一样,有多种方法可以做到。您使用的两个命令是一种方法。您可以做的另一件事就是将它们与git stash -u
. 在-u
确保新增加的文件(未经跟踪)也包括在内。
The handy thing about git stash -u
is that
方便的git stash -u
是
- it is probably the simplest (only?) single command to accomplish your goal
- if you change your mind afterwards you get allyour work back with
git stash pop
(it's like deleting an email in gmail where you can just undo if you change your mind afterwards)
- 这可能是实现目标的最简单(唯一?)单个命令
- 如果您事后改变主意,您将恢复所有工作
git stash pop
(就像在 gmail 中删除电子邮件一样,如果您事后改变主意,您可以撤消)
As of your other question git reset --hard
won't remove the untracked files so you would still need the git clean -f
. But a git stash -u
might be the most convenient.
至于您的其他问题,git reset --hard
不会删除未跟踪的文件,因此您仍然需要git clean -f
. 但 agit stash -u
可能是最方便的。
回答by Actung
1. When you don't want to keep your local changes at all.
1. 当您根本不想保留本地更改时。
git reset --hard
This command will completely remove all the local changes from your local repository. This is the best way to avoid conflicts during pull command, only if you don't want to keep your local changes at all.
此命令将从您的本地存储库中完全删除所有本地更改。这是在 pull 命令期间避免冲突的最佳方法,仅当您根本不想保留本地更改时。
2. When you want to keep your local changes
2. 当您想保留本地更改时
If you want to pull the new changes from remote and want to ignore the local changes during this pull then,
如果您想从远程拉取新更改并希望在此拉取期间忽略本地更改,那么,
git stash
It will stash all the local changes, now you can pull the remote changes,
它将隐藏所有本地更改,现在您可以拉取远程更改,
git pull
Now, you can bring back your local changes by,
现在,您可以通过以下方式恢复本地更改,
git stash pop
回答by Tadas Stasiulionis
Use:
用:
git checkout -- <file>
To discard the changes in the working directory.
放弃工作目录中的更改。
回答by Emmanuel Mahuni
I think git has one thing that isn't clearly documented. I think it was actually neglected.
我认为 git 有一件事没有明确记录。我认为它实际上被忽视了。
git checkout .
git checkout .
Man, you saved my day. I always have things I want to try using the modified code. But the things sometimes end up messing the modified code, add new untracked files etc. So what I want to do is, stage what I want, do the messy stuff, then cleanup quickly and commit if I'm happy.
伙计,你救了我的一天。我总是想尝试使用修改后的代码。但事情有时最终会弄乱修改后的代码,添加新的未跟踪文件等。所以我想要做的是,展示我想要的东西,做一些凌乱的事情,然后快速清理并在我满意时提交。
There's git clean -fd
works well for untracked files.
有git clean -fd
很好的未跟踪文件的工作。
Then git reset
simply removes staged, but git checkout
is kinda too cumbersome. Specifying file one by one or using directories isn't always ideal. Sometimes the changed files I want to get rid of are within directories I want to keep. I wished for this one command that just removes unstaged changes and here you're. Thanks.
然后git reset
简单地删除staged,但git checkout
有点太麻烦了。一一指定文件或使用目录并不总是理想的。有时,我想删除的已更改文件位于我想保留的目录中。我希望这个命令只删除未暂存的更改,您就在这里。谢谢。
But I think they should just have git checkout
without any options, remove all unstaged changes and not touch the the staged. It's kinda modular and intuitive. More like what git reset
does. git clean
should also do the same.
但我认为他们应该git checkout
没有任何选择,删除所有未上演的更改,而不是触及上演的。它有点模块化和直观。更像是什么git reset
。git clean
也应该这样做。
回答by Tatarin
For discard alli like to stash and drop that stash, it's the fastest way to discard all, especially if you work between multiple repos.
对于丢弃所有我喜欢藏匿和丢弃该藏匿的物品,这是丢弃所有物品的最快方法,尤其是当您在多个存储库之间工作时。
This will stash all changes in {0}
key and instantly drop it from {0}
这将隐藏{0}
密钥中的所有更改并立即将其从{0}
git stash && git stash drop
git stash && git stash drop
回答by bpedroso
The best way is checking out the changes.
最好的方法是检查更改。
Changing the file pom.xml in a project named project-name you can do it:
在名为 project-name 的项目中更改文件 pom.xml 您可以这样做:
git status
# modified: project-name/pom.xml
git checkout project-name/pom.xml
git checkout master
# Checking out files: 100% (491/491), done.
# Branch master set up to track remote branch master from origin.
# Switched to a new branch 'master'
回答by Rizo
First of all check is your important change saved or not by:
首先检查您的重要更改是否保存:
$ git status
$ git 状态
than try
比尝试
$ git reset --hard
it will reset your branch to default
$ git reset --hard
它会将您的分支重置为默认值
but if you need just undo:
但如果您只需要撤消:
$ edit (1) $ git add frotz.c filfre.c $ mailx (2) $ git reset
(3) $ git pull git://info.example.com/ nitfol
$ edit (1) $ git add frotz.c filfre.c $ mailx (2) $ git reset
(3) $ git pull git://info.example.com/ nitfol
Read more >> https://git-scm.com/docs/git-reset