git 如何丢弃修改后的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8273823/
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
How can I discard modified files?
提问by David Degea
This is a Linux 2.6 kernel repository. I git clone it to my local host.
这是一个 Linux 2.6 内核存储库。我将它克隆到我的本地主机。
After that. I didn't make any change. But when I "git status". I found 13 modified files. I want to discard them, but I can't.
在那之后。我没有做任何改变。但是当我“git status”时。我发现了 13 个修改过的文件。我想丢弃它们,但我不能。
> luke@Macbook-Pro~/Documents/workspace/linuxkernel/linux-2.6$ git
> status
> # On branch 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: include/linux/netfilter/xt_connmark.h
> # modified: include/linux/netfilter/xt_dscp.h
> # modified: include/linux/netfilter/xt_mark.h
> # modified: include/linux/netfilter/xt_rateest.h
> # modified: include/linux/netfilter/xt_tcpmss.h
> # modified: include/linux/netfilter_ipv4/ipt_ecn.h
> # modified: include/linux/netfilter_ipv4/ipt_ttl.h
> # modified: include/linux/netfilter_ipv6/ip6t_hl.h
> # modified: net/ipv4/netfilter/ipt_ecn.c
> # modified: net/netfilter/xt_dscp.c
> # modified: net/netfilter/xt_hl.c
> # modified: net/netfilter/xt_rateest.c
> # modified: net/netfilter/xt_tcpmss.c
> #
> # Untracked files:
> # (use "git add <file>..." to include in what will be committed)
> #
> # samples/hidraw/ no changes added to commit (use "git add" and/or "git commit -a")
> luke@Macbook-Pro~/Documents/workspace/linuxkernel/linux-2.6$ git
> checkout -- include/linux/netfilter/xt_connmark. h
> luke@Macbook-Pro~/Documents/workspace/linuxkernel/linux-2.6$ git
> status
> # On branch 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: include/linux/netfilter/xt_CONNMARK.h
> # modified: include/linux/netfilter/xt_dscp.h
> # modified: include/linux/netfilter/xt_mark.h
> # modified: include/linux/netfilter/xt_rateest.h
> # modified: include/linux/netfilter/xt_tcpmss.h
> # modified: include/linux/netfilter_ipv4/ipt_ecn.h
> # modified: include/linux/netfilter_ipv4/ipt_ttl.h
> # modified: include/linux/netfilter_ipv6/ip6t_hl.h
> # modified: net/ipv4/netfilter/ipt_ecn.c
> # modified: net/netfilter/xt_dscp.c
> # modified: net/netfilter/xt_hl.c
> # modified: net/netfilter/xt_rateest.c
> # modified: net/netfilter/xt_tcpmss.c
> #
> # Untracked files:
> # (use "git add <file>..." to include in what will be committed)
> #
> # samples/hidraw/ no changes added to commit (use "git add" and/or "git commit -a")
> luke@Macbook-Pro~/Documents/workspace/linuxkernel/linux-2.6$ git
> checkout -- include/linux/netfilter/xt_dscp.h
> luke@Macbook-Pro~/Documents/workspace/linuxkernel/linux-2.6$ git
> status
> # On branch 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: include/linux/netfilter/xt_CONNMARK.h
> # modified: include/linux/netfilter/xt_DSCP.h
> # modified: include/linux/netfilter/xt_mark.h
> # modified: include/linux/netfilter/xt_rateest.h
> # modified: include/linux/netfilter/xt_tcpmss.h
> # modified: include/linux/netfilter_ipv4/ipt_ecn.h
> # modified: include/linux/netfilter_ipv4/ipt_ttl.h
> # modified: include/linux/netfilter_ipv6/ip6t_hl.h
> # modified: net/ipv4/netfilter/ipt_ecn.c
> # modified: net/netfilter/xt_dscp.c
> # modified: net/netfilter/xt_hl.c
> # modified: net/netfilter/xt_rateest.c
> # modified: net/netfilter/xt_tcpmss.c
> #
> # Untracked files:
> # (use "git add <file>..." to include in what will be committed)
> #
> # samples/hidraw/ no changes added to commit (use "git add" and/or "git commit -a")
> luke@Macbook-Pro~/Documents/workspace/linuxkernel/linux-2.6$
>
edited:
编辑:
luke@Macbook-Pro~/Documents/workspace/linuxkernel/linux-2.6$ git config -l
user.name=99999
user.email=99999
alias.co=checkout
core.editor=mate -w
core.edit=mate -w
core.excludesfile=/Users/luke/.gitignore_global
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
edited: this is my diff file:
编辑:这是我的差异文件:
diff --git a/include/linux/netfilter/xt_CONNMARK.h b/include/linux/netfilter/xt_CONNMARK.h
index 2f2e48e..efc17a8 100644
--- a/include/linux/netfilter/xt_CONNMARK.h
+++ b/include/linux/netfilter/xt_CONNMARK.h
@@ -1,6 +1,31 @@
-#ifndef _XT_CONNMARK_H_target
-#define _XT_CONNMARK_H_target
+#ifndef _XT_CONNMARK_H
+#define _XT_CONNMARK_H
-#include <linux/netfilter/xt_connmark.h>
+#include <linux/types.h>
-#endif /*_XT_CONNMARK_H_target*/
+/* Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
+ * by Henrik Nordstrom <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+enum {
+ XT_CONNMARK_SET = 0,
+ XT_CONNMARK_SAVE,
+ XT_CONNMARK_RESTORE
+};
+
+struct xt_connmark_tginfo1 {
+ __u32 ctmark, ctmask, nfmask;
+ __u8 mode;
+};
+
+struct xt_connmark_mtinfo1 {
+ __u32 mark, mask;
+ __u8 invert;
+};
+
+#endif /*_XT_CONNMARK_H*/
diff --git a/include/linux/netfilter/xt_DSCP.h b/include/linux/netfilter/xt_DSCP.h
index 648e0b3..15f8932 100644
--- a/include/linux/netfilter/xt_DSCP.h
+++ b/include/linux/netfilter/xt_DSCP.h
@@ -1,26 +1,31 @@
-/* x_tables module for setting the IPv4/IPv6 DSCP field
+/* x_tables module for matching the IPv4/IPv6 DSCP field
*
* (C) 2002 Harald Welte <[email protected]>
- * based on ipt_FTOS.c (C) 2000 by Matthew G. Marsh <[email protected]>
* This software is distributed under GNU GPL v2, 1991
*
* See RFC2474 for a description of the DSCP field within the IP Header.
*
- * xt_DSCP.h,v 1.7 2002/03/14 12:03:13 laforge Exp
+ * xt_dscp.h,v 1.3 2002/08/05 19:00:21 laforge Exp
*/
-#ifndef _XT_DSCP_TARGET_H
-#define _XT_DSCP_TARGET_H
-#include <linux/netfilter/xt_dscp.h>
+#ifndef _XT_DSCP_H
+#define _XT_DSCP_H
+
#include <linux/types.h>
-/* target info */
-struct xt_DSCP_info {
+#define XT_DSCP_MASK 0xfc /* 11111100 */
+#define XT_DSCP_SHIFT 2
+#define XT_DSCP_MAX 0x3f /* 00111111 */
+
+/* match info */
+struct xt_dscp_info {
__u8 dscp;
+ __u8 invert;
};
-struct xt_tos_target_info {
- __u8 tos_value;
+struct xt_tos_match_info {
__u8 tos_mask;
+ __u8 tos_value;
+ __u8 invert;
};
-#endif /* _XT_DSCP_TARGET_H */
+#endif /* _XT_DSCP_H */
diff --git a/include/linux/netfilter/xt_mark.h b/include/linux/netfilter/xt_mark.h
index ecadc40..41c456d 100644
--- a/include/linux/netfilter/xt_mark.h
+++ b/include/linux/netfilter/xt_mark.h
@@ -1,15 +1,6 @@
-#ifndef _XT_MARK_H
-#define _XT_MARK_H
+#ifndef _XT_MARK_H_target
+#define _XT_MARK_H_target
-#include <linux/types.h>
+#include <linux/netfilter/xt_mark.h>
-struct xt_mark_tginfo2 {
- __u32 mark, mask;
-};
-
-struct xt_mark_mtinfo1 {
- __u32 mark, mask;
- __u8 invert;
-};
-
-#endif /*_XT_MARK_H*/
+#endif /*_XT_MARK_H_target */
diff --git a/include/linux/netfilter/xt_rateest.h b/include/linux/netfilter/xt_rateest.h
index d40a619..6605e20 100644
--- a/include/linux/netfilter/xt_rateest.h
+++ b/include/linux/netfilter/xt_rateest.h
@@ -1,37 +1,15 @@
-#ifndef _XT_RATEEST_MATCH_H
-#define _XT_RATEEST_MATCH_H
+#ifndef _XT_RATEEST_TARGET_H
+#define _XT_RATEEST_TARGET_H
Due to Stack Overflow length limit. Only part of diff file can be copied here.
由于堆栈溢出长度限制。这里只能复制 diff 文件的一部分。
still need help
仍然需要帮助
回答by Igor Popov
A git reset --hard HEAD
should solve the problem.
Agit reset --hard HEAD
应该可以解决问题。
回答by Buddy
It happens because the linux kernel includes files with the same name (but different cases):
发生这种情况是因为 linux 内核包含具有相同名称的文件(但情况不同):
include/linux/netfilter/xt_connmark.h
include/linux/netfilter/xt_CONNMARK.h
回答by avichalp
You can use:
您可以使用:
git checkout .
To understand the difference between git checkout
and git reset
refer to the following question :
要了解两者之间的区别git checkout
并git reset
参考以下问题:
What's the difference between "git reset" and "git checkout"?
回答by Jayant Tripathi
By entering these commands in succession.
通过连续输入这些命令。
git stash
git stash drop
git stash
stores modified files on a stack for later retrieval.
git stash
将修改后的文件存储在堆栈上供以后检索。
git stash drop
discards them.
git stash drop
丢弃它们。
回答by Drew D
this problem was caused, for me, by a non-case-sensitive filesystem (e.g. on Mac OS). You can either create a case-sensitive disk image and mount it and do your git work on there, or use a different OS to do your development.
对我来说,这个问题是由不区分大小写的文件系统(例如在 Mac OS 上)引起的。您可以创建一个区分大小写的磁盘映像并挂载它并在那里进行 git 工作,或者使用不同的操作系统来进行开发。
回答by horoyoi o
modified: specific_filename
modified: specific_filename
git checkout specific_filename
结帐 specific_filename
It's safer than removing it all at once using git checkout .
它比使用一次全部删除更安全 git checkout .
回答by Victor Choy
The question is similar to Strange behaviour of Git: mysterious changes cannot be undone
该问题类似于Git 的奇怪行为:神秘的更改无法撤消
You need a case-insensitive filesystem. If your PC is Apple MAC like me, you can create Case-sensitive disk.
您需要一个不区分大小写的文件系统。如果您的 PC 和我一样是 Apple MAC,您可以创建区分大小写的磁盘。
hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 25g ~/android.dmg
hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage
# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg.sparseimage -mountpoint /Volumes/android; }
# unmount the android file image
function umountAndroid() { hdiutil detach /Volumes/android; }
回答by Adrian Cornish
You can remove untracked files with clean as well
您也可以使用 clean 删除未跟踪的文件
git clean -f
回答by prusswan
There's another possibility where the issue is filemode-related. In that case you may try git config core.filemode false
还有另一种可能,该问题与文件模式相关。在这种情况下,您可以尝试git config core.filemode false
More info: How do I make Git ignore file mode (chmod) changes?
更多信息:如何让 Git 忽略文件模式 (chmod) 更改?
Edit:
编辑:
Another possibility would be the line endings, try setting git config core.autocrlf true|false|input
and see if it makes any difference.
另一种可能性是行尾,尝试设置git config core.autocrlf true|false|input
并查看它是否有任何区别。
More info: Why should I use core.autocrlf=true in Git?
回答by Leo
If you would like to remove all the files and modifications that differ from your originbranch but would like to keep all the ignored files (ex. node_modules, .vscode, .idea etc..) the best choice is to combine these two commands:
如果您想删除与您的原始分支不同的所有文件和修改,但想保留所有被忽略的文件(例如 node_modules、.vscode、.idea 等),最好的选择是组合这两个命令:
git reset --hard && git clean -f
This will reset all modified files and will clean all the unstaged ones.
这将重置所有修改过的文件并清除所有未暂存的文件。