{"id":1837,"date":"2017-12-06T08:45:07","date_gmt":"2017-12-06T08:45:07","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=1837"},"modified":"2025-02-01T22:47:39","modified_gmt":"2025-02-01T22:47:39","slug":"how-to-clean-workspace-in-git","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-clean-workspace-in-git\/","title":{"rendered":"Git Commands Tutorials and Example: Git Clean &#8211; How to clean git workspace?"},"content":{"rendered":"<p><strong>How to clean workspace in git?<\/strong><br>\nTo reset a specific file to the last-committed state (to discard uncommitted changes in a specific file):<\/p>\n<p>git checkout thefiletoreset.txt<\/p>\n<p>This is mentioned in the git status output:<\/p>\n<p>(use &#8220;git checkout &#8212; &#8230;&#8221; to discard changes in working directory)<\/p>\n<p>To reset the entire repository to the last committed state:<\/p>\n<p>git reset &#8211;hard<\/p>\n<p>To remove untracked files, I usually just delete all files in the working copy (but not the .git\/ folder!), then do git reset &#8211;hard which leaves it with only committed files.<\/p>\n<p>A better way is to use git clean:<\/p>\n<p>git clean -d -x -f<\/p>\n<p>will remove untracked files, including directories (-d) and files ignored by git (-x). Replace the -f argument with -n to perform a dry-run or -i for interactive mode and it will tell you what will be removed.<br>\nyou delete local files from your current branch?<\/p>\n<p>git clean -f<\/p>\n<p>But beware&#8230; there&#8217;s no going back. Use -n or &#8211;dry-run to preview the damage you&#8217;ll do.<\/p>\n<p>If you want to also remove directories, run git clean -f -d<\/p>\n<p>If you just want to remove ignored files, run git clean -f -X<\/p>\n<p>If you want to remove ignored as well as non-ignored files, run git clean -f -x<\/p>\n<p>Note the case difference on the X for the two latter commands.<br>\nRegards,<br>\nRajesh Kumar<br>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_78145\"><iframe loading=\"lazy\"  id=\"_ytid_93478\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_78145\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>How to clean workspace in git? To reset a specific file to the last-committed state (to discard uncommitted changes in a specific file): git checkout thefiletoreset.txt This is mentioned in&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[38],"tags":[160],"class_list":["post-1837","post","type-post","status-publish","format-standard","hentry","category-git","tag-git"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/1837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=1837"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/1837\/revisions"}],"predecessor-version":[{"id":25735,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/1837\/revisions\/25735"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=1837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=1837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=1837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}