各種方法刪除本地Git更改

本文翻譯自: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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章