不同的方法来删除本地的Git变化 [英] Various ways to remove local Git changes

查看:156
本文介绍了不同的方法来删除本地的Git变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是克隆一个Git仓库,并签出一个分支。我的工作就可以了,然后再决定删除我所有的局部变化,因为我想原件。

总之,我不得不做以下两个命令来删除我的本地更改

  git的结帐。Git的清洁-f

我的问题是,

(1)这是摆脱局部变化正确的方法,否则请让我知道正确的做法。

(2)我们什么时候使用的git的复位 - 硬为我能够即使没有此命令来重置

感谢

*解决方案:主要编辑(S):03/26:*
取代许多使用Git专业术语模糊条款
 [追踪/不露痕迹/上演/不分级]

有可能是只有三类文件,当我们进行本地更改:


  

1型履带上演文件


  
  

2型不分级跟踪的文件


  
  

类型3.不分级未跟踪文件a.k.a未跟踪文件



  • 上演 - 那些被转移到临时区域/添加到索引

  • 履带 - 修改后的文件

  • 未跟踪 - 新文件。始终不分级。如果上演,这意味着他们被跟踪。

什么每个命令执行:


  1. git的结帐 - 删除不分级跟踪的文件ONLY [类型2]


  2. git的清洁-f - 删除不分级未跟踪文件ONLY [类型3]


  3. 的git的复位 - 硬 - 删除上演跟踪和不分级跟踪的文件ONLY [1型,2型]


  4. git的藏匿-u - 删除所有的变化[1型,2型,3型]


结论:

很明显,我们可以使用

 (1)的`git的清洁-f`组合和'混帐重置--hard`

 (2)`git的藏匿-u`

以实现期望的结果。

请注意:藏起来,因为这个词的意思是。存储(东西)安全偷偷在指定的地方这总是可以使用 git的藏匿流行 retreived。
因此,上述两个方案之间进行选择是开发人员的电话。

感谢您克里斯托夫和弗雷德里克Schøning。

编辑:3月27日

我认为这是值得把'的提防的'音符 git的清洁-f

git的清洁-f

有没有回头路。使用 -n - 干润来preVIEW你会做伤害。

如果您也想删除目录,运行 git的清洁-f -d

如果你只是想删除忽略的文件,运行 git的清洁-f -X

如果您要删除忽略以及非忽略的文件,运行 git的清洁-f -x

参考:更多关于 git的清洁:<一href=\"http://stackoverflow.com/questions/61212/removing-untracked-files-from-your-git-working-copy?rq=1\">How从我目前的Git分支删除本地(未跟踪)文件?

编辑:15年5月20日

丢弃在此分支的所有本地提交的[删除本地提交]

为了抛弃这个分支中的所有本地提交,使当地分支机构相同
到这个分支的上游,只需运行的git的复位 - 硬@ {蓝}

参考:<一href=\"http://sethrobertson.github.io/GitFixUm/fixup.html\">http://sethrobertson.github.io/GitFixUm/fixup.html

还是的git的复位 - 硬起源/主 [如果地方分公司是]

注意:2015年6月12日
这是不会其他的副本SO质疑被标记为重复。这个问题解决如何删除本地GIT变化[删除添加的文件,删除添加到现有的文件等以及各种方法的变化;凡在其他SO线程只解决如何删除本地提交。如果添加了文件,并且要单独删除,那么其他SO线程没有关于它的讨论。因此这是不另一个的重复]

编辑:15年6月23日

如何恢复已承诺推到远程存储库?

  $ git的恢复ab12cd15

修改:2015年9月1日

删除previous从本地分支,提交远程分支

案例:您刚才COMMITED更改您的当地分支机构,并立即被推到远程分支,
恍然大悟,哦,不!我不需要这种变化。现在做什么?

的git的复位 - 硬HEAD〜1 [删除从当地分公司提交]

混帐推起源HEAD --force [两个命令必须执行。从远程分支删除]

请告诉我分支?它目前已签出的分支。

修改2015年9月8日 - 删除本地的git的合并

我在分支合并与新工作支行 2阶段

  $ git的状态
#分支主$ git的合并阶段2$ git的状态
#分支主
#分支是超前的原籍/主'的8提交。

问:如何摆脱这种合并?
试图的git的复位 - 硬 git的清洁-d -f
两人都没有工作。

唯一合作任何以下的人的:

  $ git的复位 - 硬起源/主

  $ git的复位 - 硬头〜8

$ git的复位 - 硬9a88396f51e2a068bb7 [SHA提交code - 这是一个一切都是你的合并的提交之前发生present]


解决方案

这一切都取决于你试图撤消/还原到底是什么。通过阅读<一开始就href=\"https://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-a-git-managed-project-to-$p$pvious-state\">the在后宇部的链接。但是,试图回答:

硬重置

 的git的复位 - 硬[HEAD]

完全删除所有演出和不分级的变化来跟踪文件。

我发现自己经常使用硬复位,当我像只是撤消一切就像如果我做了从远程完全重新克隆。在你的情况,你只希望你的回购质朴,这是可行的。

清除

  git的清洁[-f]

删除未跟踪文件。

有关删除临时文件,但要演出和不分级的变化已跟踪文件。大多数时候,我可能会最终使一个无视规则,而不是反复清洗 - 例如为斌/ OBJ文件夹在C#项目,你通常会想要从回购中排除,以节省空间,或者类似的东西。

-f(强制)选项也将删除的文件,这是不被跟踪的的也被忽视了,虽然混帐忽略规则。在上述情况下,与无视规则永远不会跟踪的bin / OBJ文件夹,即使这些文件夹被忽视的Git,使用强制选项会从文件系统中删除。我偶尔看到这一点,例如使用当脚本部署,并要清洗部署,压缩和解或任何在你的code。

清洁的Git不会碰的文件​​,那些已经被跟踪。

结帐点

  git的结帐。

我却从未读您的文章见过这个符号。我有一个很难为此找到文档(也许有人可以帮忙),但是从周围有点玩,它看起来像它的意思是:

撤消所有的变化在我的工作树。

即。撤消跟踪文件撤回变化。它显然没有触及阶段性变化和叶单未跟踪文件。

积攒

一些答案​​提到藏起来。由于措辞暗示,可能会使用藏起来,当你在的东西(没准备好提交)中间,你不得不暂时切换分公司或以某种方式在你的code的另一个状态工作,后来返回你的凌乱的办公桌。我不认为这也适用于你的问题,但它绝对方便。

综上所述

一般来说,如果你有信心,你犯了,也许推到了一个偏僻的重要变化,如果你只是玩弄或类似,使用的git的复位 - 硬HEAD 其次 git的清洁-f 将最终净化你的code的状态,这将是,如果它只是被克隆和从分支签出。需要强调的是真正重要的是,复位也将删除上演,但提交的更改。 将擦拭尚未承诺的一切(除未跟踪的文件,在这种情况下,使用的清洁的)。

其他所有的命令在那里,以方便更复杂的情况,在需要的撤消东西粒度:)

我觉得,你的问题#1被覆盖,但最后,在#2得出结论:原因你从来没有发现需要使用的git的复位 - 硬是你从来没有上演什么。有了你上演了变化,无论是 git的结帐。也不 git的清洁-f 会恢复的。

希望这涵盖了。

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.

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.

(2) when do we use git reset --hard as i am able to reset even without this command

Thanks

*Solution : Major Edit(s): 03/26 : * Replaced many of vague terms with git specific terminology [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

  • 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.

What each commands do:

  1. git checkout . - Removes Unstaged Tracked files ONLY [Type 2]

  2. git clean -f - Removes Unstaged UnTracked files ONLY [Type 3]

  3. git reset --hard - Removes Staged Tracked and UnStaged Tracked files ONLY[Type 1, Type 2]

  4. git stash -u - Removes all changes [Type 1, Type 2, Type 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 retreived using git stash pop. So choosing between the above two options is developer's call.

Thank you Christoph and Frederik Schøning.

Edit: 03/27

I thought it's worth putting the 'beware' note to git clean -f

git clean -f

There is no going back. Use -n or --dry-run to preview the damage you'll do.

If you want to also remove directories, run git clean -f -d

If you just want to remove ignored files, run git clean -f -X

If you want to remove ignored as well as non-ignored files, run git clean -f -x

reference : more on git clean : How do I remove local (untracked) files from my current Git branch?

Edit: 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}

Reference: http://sethrobertson.github.io/GitFixUm/fixup.html

or do git reset --hard origin/master [if local branch is master]

Note: 06/12/2015 This is not a 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]

Edit: 06/23/15

How to revert a commit already pushed to a remote repository?

$ git revert ab12cd15

Edit: 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 push origin HEAD --force [both the commands must be executed. For deleting from remote branch]

Whats the branch ? Its the currently checked out branch.

Edit 09/08/2015 - Remove local git merge:

I am on master branch and merged master branch with a newly working branch 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.

The only thing that worked are 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]

解决方案

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.

The -f (force) option will also remove files, that are not tracked and are 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.

Git clean will not touch files, that are already being tracked.

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.

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).

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.

Hope this covers.

这篇关于不同的方法来删除本地的Git变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆