本文翻译自:How do I clear my local working directory in Git? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

  • How to remove local (untracked) files from the current Git working tree 35 answers 如何从当前Git工作树 35中 删除本地(未跟踪)文件

How can I clear my working directory in Git? 如何在Git中清除我的工作目录?


#1楼

参考:https://stackoom.com/question/2pBP/如何清除Git中的本地工作目录-重复


#2楼

All the answers so far retain local commits. 到目前为止,所有答案都保留了本地提交。 If you're really serious, you can discard all local commits and all local edits by doing: 如果您真的很认真,可以通过以下方式丢弃所有本地提交和所有本地编辑:

git reset --hard origin/branchname

For example: 例如:

git reset --hard origin/master

This makes your local repository exactly match the state of the origin (other than untracked files). 这使您的本地存储库与原始状态(未跟踪文件除外) 完全匹配

If you accidentally did this after just reading the command, and not what it does :), use git reflog to find your old commits. 如果您在阅读完命令之后意外地执行了此操作,而不是它的操作:),请使用git reflog查找旧的提交。


#3楼

You could create a commit which contains an empty working copy. 您可以创建一个包含空工作副本的提交。

This is a generally safe, non-destructive approach because it does not involve the use of any brute-force reset mechanisms. 这是一种通常安全,非破坏性的方法,因为它不涉及使用任何强力复位机制。 First you hide all managed content with git checkout empty , then you are free to manually review and remove whatever unmanaged content remains. 首先,您将所有托管内容隐藏为git checkout empty ,然后您可以自由查看并删除任何未管理的内容。

## create a stand-alone, tagged, empty commit
true | git mktree | xargs git commit-tree | xargs git tag empty## clear the working copy
git checkout empty

Your working copy should now be clear of any managed content. 您的工作副本现在应该清除任何托管内容。 All that remains are unmanaged files and the .git folder itself. 剩下的就是非托管文件和.git文件夹本身。

To re-populate your working copy... 要重新填充您的工作副本......

git checkout master ## or whatever branch you will be using

If you're a forward thinking individual, you might start your repository off on the right foot by basing everything on an initial empty commit... 如果你是一个具有前瞻思维的人,你可以通过将所有内容都放在最初的空提交中来启动你的知识库。

git init
git commit --allow-empty --allow-empty-message -m ""
git tag empty
...

There are various uses for a tagged empty worktree. 标记的空工作树有各种用途。 My favorite at the moment is to depopulate the root under a set of git worktree subfolders. 我最喜欢的是在一组git worktree子文件夹下减少root。


#4楼

To reset a specific file as git status suggests: 要将特定文件重置为git status建议:

git checkout <filename>

To reset a folder 要重置文件夹

git checkout <foldername>/*

#5楼

To switch to another branch, discarding all uncommitted changes (eg resulting from Git's strange handling of line endings): 要切换到另一个分支,丢弃所有未提交的更改(例如,由Git奇怪处理行结尾):

git checkout -f <branchname>

I had a working copy with hundreds of changed files (but empty git diff --ignore-space-at-eol ) which I couldn't get rid off with any of the commands I read here, and git checkout <branchname> won't work, either - unless given the -f (or --force ) option. 我有一个包含数百个已更改文件的工作副本(但是空的git diff --ignore-space-at-eol ),我无法用我在这里读到的任何命令摆脱它, git checkout <branchname> won' --force工作 - 除非给出-f (或--force )选项。


#6楼

Use: 使用:

git clean -df

It's not well advertised, but git clean is really handy. 它没有得到很好的宣传,但git clean非常方便。 Git Ready has a nice introduction to git clean . Git Readygit clean有一个很好的介绍 。

如何清除Git中的本地工作目录? [重复]相关推荐

  1. 如何丢弃 Git 中的本地提交

    [问题标题]:如何丢弃 Git 中的本地提交?(How to discard local commits in Git?) [发布时间]:2011-04-22 09:46:21 [问题描述]: 我一直 ...

  2. 代码版本控制工具Concurrent Versions System(CVS)的三种用配置库更新本地工作目录文件的方法

    (1)选中文件右键->team->update (2)选中文件右键->replace with->latest from HEAD (3)选中文件右键->team-> ...

  3. svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录

    svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录 参考文章: (1)svn更新路径,解决办法详细步骤,ec ...

  4. 丢弃Git中的本地提交

    由于一些糟糕的挑选,我的本地Git存储库目前在原点之前提交了五个提交,并且状态不佳. 我想摆脱所有这些提交并重新开始. 显然,删除我的工作目录并重新克隆会这样做,但是再次从GitHub下载所有内容似乎 ...

  5. Git 中删除本地分支和删除远程分支是两回事儿

    目录 本地操作 1. 创建本地分支 2. 删除本地分支 远端操作 1. 创建远程分支 2. 删除远程分支 本地操作 1. 创建本地分支 在本地仓库中创建本地分支,Git 命令如下: git branc ...

  6. Java中实现cd命令:运行中更改当前工作目录

    2019独角兽企业重金招聘Python工程师标准>>> 因为一些原因,想在Java中模拟部分Shell命令的功能,比如cd.pwd.ls等. 实现pwd很简单,返回System.ge ...

  7. 在python中获取当前工作目录可以通过_python-获取当前工作路径

    1.sys.argv[0] import sys print sys.argv[0]#获得的是当前执行脚本的位置(若在命令行执行的该命令,则为空) 运行结果(在python脚本中执行的结果): F:/ ...

  8. 研发部 svn git中的的项目管理目录

    项目目录结构 序号 目录名称 目录说明 1 01_Documents 项目文档目录 2 01_Management 项目管理文档 3 01_会议记录 项目组会议的会议纪要 4 01_周会 5 02_反 ...

  9. 在Git中,HEAD,工作树和索引之间有什么区别?

    有人能告诉我在Git中HEAD,工作树和索引之间的区别吗? 据我所知,它们都是不同分支的名称. 我的假设是否正确? 编辑 我找到了这个 单个git存储库可以跟踪任意数量的分支,但是您的工作树只与其中一 ...

最新文章

  1. Android之解决AppBarLayout 下面一道阴影
  2. MFC空间几何变换之图像平移、镜像、旋转、缩放
  3. 程序防止SqlServer使用SqlServer Profiler跟踪
  4. datacount+php,dataCount.php
  5. 牛,碉堡了:GitHub 加速神器
  6. Python-文件操作
  7. PhalApi2.x笔记
  8. 软件测试面试该如何谈薪
  9. TB交易开拓者入门教程
  10. 智能访客机要注意这些陷阱
  11. Android8.0 NotificationChannel修改铃声和振动的坑
  12. Σd|nφ(d)=n的证明
  13. 科普:Java 后端开发常用的 10 种第三方服务
  14. Citric I 模拟赛心得
  15. Disentangled Graph Collaborative Filtering
  16. c语言静态变量与局部变量,C语言 全局变量、静态全局变量、局部变量、静态局部变量...
  17. 十年前的AlexNet,今天的NeurIPS 2022时间检验奖
  18. Codeblocks加入头文件
  19. 详解 利用NPOI向Excel指定位置中加入图片(支持.xlsx和.xls)
  20. Android开发之播放音频

热门文章

  1. 高处看Surface,WIndow,View,SurfaceView
  2. Unknown host 'services.gradle.org' 解决方法
  3. Android 数据库 ANR的例子
  4. 浅析@Deprecated,调用方法时出现横线划掉样式
  5. wireshark网络分析就这么简单_【读书笔记】2wireshark网络分析就这么简单——不同子网如何发送消息。...
  6. Android之帧动画与补间动画的使用
  7. MacApp开发Error Domain=NSURLErrorDomain Code=-1003 A server with the specified hostname could not be
  8. (0033) iOS 开发之Block 的基础用法及注意事项2
  9. (iOS-框架封装)AFN3.x 网络请求封装
  10. webpack加载器打包样式表中的图片和字体