各种方法删除本地Git更改

本文介绍了在Git中删除本地更改的各种方法,包括移除未分阶段的跟踪文件、未分阶段的未跟踪文件以及分阶段的跟踪文件。文章详细解释了每个命令的作用,如`git checkout`、`git reset`和`git clean`,并讨论了何时使用`git stash`。同时,还涵盖了如何撤销本地提交,以及如何在合并冲突时管理本地更改。
摘要由CSDN通过智能技术生成

本文翻译自:Various ways to remove local Git changes

I just cloned a git repository and checked out a branch. 我刚刚克隆了一个git存储库并检出了一个分支。 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. (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 类型1.分阶段跟踪文件

Type 2. Unstaged Tracked files 键入2.未分级跟踪文件

Type 3. Unstaged UnTracked files aka UnTracked files 键入3. Unstaged UnTracked文件,即UnTracked文件

  • Staged - Those that are moved to staging area/ Added to index 分阶段 - 那些被移动到临时区域/添加到索引
  • Tracked - modified files 跟踪 - 修改过的文件
  • UnTracked - new files. UnTracked - 新文件。 Always unstaged. 总是没有分期。 If staged, that means they are tracked. 如果上演,则意味着他们被跟踪。

What each commands do: 每个命令的作用:

  1. git checkout . - Removes Unstaged Tracked files ONLY [Type 2] - 仅删除未分级跟踪文件[类型2]

  2. git clean -f - Removes Unstaged UnTracked files ONLY [Type 3] git clean -f - 仅删除未分级的UnTracked文件[类型3]

  3. git reset --hard - Removes Staged Tracked and UnStaged Tracked files ONLY[Type 1, Type 2] git reset --hard - 仅删除分阶段跟踪和未分阶段跟踪文件[类型1,类型2]

  4. git stash -u - Removes all changes [Type 1, Type 2, Type 3] 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 . 这总是可以使用git stash pop检索。 So choosing between the above two options is developer's call. 因此,在上述两个选项之间进行选择是开发人员的号召。

Thank you Christoph and Frederik Schøning. 谢谢Christoph和FrederikSchø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

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/2015 This is not a duplicate of the other SO question that's marked as duplicate. 注意:06/12/2015不是另一个被标记为重复的SO问题的副本。 This question address how to remove local GIT changes [remove a file added, remove changes added to existing file etc and the various approaches; 此问题解决了如何删除本地GIT更改[删除添加的文件,删除添加到现有文件等的更改以及各种方法; Where in the other SO thread only address how to remove local commit. 其他SO线程中的位置仅解决了如何删除本地提交的问题。 If you added a file, and you want to remove that alone, then the other SO thread doesn't discuss about it. 如果你添加了一个文件,并且想要单独删除它,那么另一个SO线程就不会讨论它。 Hence this is not a duplicate of the other one] 因此,这不是另一个的重复]

Edit: 06/23/15 编辑:2015年6月23日

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. git push origin HEAD --force [必须执行这两个命令。 For deleting from remote branch] 从远程分支删除]

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 --hardgit clean -d -f两者都不起作用。

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] $ git reset --hard 9a88396f51e2a068bb7 [sha commit code - 这是在你的所有合并提交发生之前出现的代码]


#1楼

参考:https://stackoom.com/question/1Wub3/各种方法删除本地Git更改


#2楼

As with everything in git there are multiple ways of doing it. 与git中的所有内容一样,有多种方法可以实现。 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 . 你可以做的另一件事就是用git stash -u隐藏它们。 The -u makes sure that newly added files (untracked) are also included. -u确保还包括新添加的文件(未跟踪)。

The handy thing about git stash -u is that 关于git stash -u的方便之git stash -u

  1. it is probably the simplest (only?) single command to accomplish your goal 它可能是实现目标的最简单(唯一?)单一命令
  2. if you change your mind afterwards you get all your 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 . 至于你的另一个问题git reset --hard将不会删除未跟踪的文件,所以你仍然需要git clean -f But a git stash -u might be the most convenient. 但是git stash -u可能是最方便的。


#3楼

It all depends on exactly what you are trying to undo/revert. 这一切都取决于你想要撤消/恢复的确切内容。 Start out by reading the post in Ube's link . 首先阅读Ube链接中的帖子 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. 在你的情况下,你只需要你的repo pristine,这将工作。

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 - eg 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 and are also being ignored by git though ignore-rule. -f(强制)选项也将删除未被跟踪的文件, 并且 git通过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. 在上面的例子中,使用ignore-rule永远不会跟踪bin / obj文件夹,即使git忽略了这些文件夹,使用force-option也会将它们从文件系统中删除。 I've sporadically seen a use for this, eg 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. 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". “撤消我工作树中的所有更改”。

Ie 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. 一般来说,如果你确信你已经投入并且可能被推到了一个遥远的重要变化,如果你只是在玩游戏等,使用git reset --hard HEAD后跟git clean -f将最终清理你的代码到状态,如果它刚被克隆并从分支机构检出,它就会进入。 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 ). 它将擦除所有尚未提交的内容 (未跟踪文件除外,在这种情况下,请使用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. 我觉得,你的问题#1已被涵盖,但最后,在#2结束:你从未发现需要使用git reset --hard因为你从未上过任何东西。 Had you staged a change, neither git checkout . 如果你上演了一个改变,那么git checkout . nor git clean -f would have reverted that. 也不是git clean -f会还原那个。

Hope this covers. 希望这涵盖。


#4楼

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'

#5楼

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 commands that 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


First step 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 #List全局配置

$ git config --list

#List the remote URL #List远程URL

$ git remote show origin

#check status #检查状态

git status

#List all local and remote branches #List所有本地和远程分支

git branch -a

#create a new local branch and start working on this branch #create一个新的本地分支并开始在这个分支上工作

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 create branch: 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. #commit local changes [两步过程: - 将文件添加到索引,这意味着添加到临时区域。 Then commit the files that are present in this staging area] 然后提交此暂存区域中存在的文件]

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] #remove本地分支中的所有更改 [假设您在本地分支中进行了一些更改,如添加新文件或修改现有文件,或进行本地提交,但不再需要) git clean -d -fgit 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 –fgit reset --hard OR(2) git stash -u来实现所需的结果。

Note 1: Stashing, as the word means 'Store (something) safely and secretly in a specified place.' 注1:存储,因为这个词的意思是“在指定的地方安全地,秘密地存储(某物)”。 This can always be retreived using git stash pop. 这总是可以使用git stash pop进行检索。 So choosing between the above two options is developer's call. 因此,在上述两个选项之间进行选择是开发人员的号召。

Note 2: git reset --hard will delete working directory changes. 注2: git reset --hard将删除工作目录更改。 Be sure to stash any local changes you want to keep before running this command. 在运行此命令之前,请务必隐藏要保留的任何本地更改。

# Switch to the master branch and make sure you are up to date. #切换到主分支 ,确保您是最新的。

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? #Accidentally从本地删除了一个文件,如何检索回来? Do a git status to get the complete filepath of the deleted resource 执行git status以获取已删除资源的完整文件路径

git checkout branchname <file path name>

that's it! 而已!

#Merge master branch with someotherbranch #Merge master branch with someotherbranch

git checkout master
git merge someotherbranchname

#rename local branch #rename local branch

git branch -m old-branch-name new-branch-name

#delete local branch #delete local branch

git branch -D branch-name

#delete remote branch #delete远程分支

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 #Change提交已经被推送到远程的最近提交的消息

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.html or do git reset --hard origin/master [if local branch is master] 参考: httpgit reset --hard origin/master或者执行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 #Delete从本地分支和远程分支的先前提交

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. git reset --hard HEAD~1 [用于从本地分支删除该提交。 1 denotes the ONE commit you made] 1代表你做的ONE提交]

git push origin HEAD --force [both the commands must be executed. git push origin HEAD --force [必须执行这两个命令。 For deleting from remote branch]. 从远程分支删除]。 Currently checked out branch will be referred as the branch where you are making this operation. 当前签出的分支将被称为您进行此操作的分支。

#Delete some of recent commits from local and remote repo and preserve to the commit that you want. #Delete来自本地和远程repo的一些最近的提交,并保留到你想要的提交。 ( 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 //查看你所有的提交

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. git push -u origin +develop //将此状态推送到远程。 + to do force push +做推力

# Remove local git merge: Case: I am on master branch and merged master branch with a newly working branch phase2 #删除本地git merge: Case:我在master分支上,并且与一个新工作的分支phase2合并了master分支

$ 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 8 commits. 你的分支在8次提交之前领先于'origin / master'。

Q: How to get rid of this local git merge? 问: 如何摆脱这种本地git合并? Tried git reset --hard and git clean -d -f Both didn't work. 尝试git reset --hardgit clean -d -f两者都不起作用。 The only thing that worked are any of the below ones: 唯一有效的是以下任何一个:

$ 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 commit code - 这是在你的所有合并提交发生之前出现的代码]

#create gitignore file #create 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


#6楼

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值