首页 文章

各种方法删除本地Git更改

提问于
浏览
586

我刚刚克隆了一个git存储库并检出了一个分支 . 我做了它,然后决定删除我所有的本地更改,因为我想要原始副本 .

简而言之,我必须执行以下两个命令来删除我的本地更改

git checkout .

git clean -f

My question is,

(1)这是摆脱当地变化的正确方法,或者请让我知道正确的方法 .

(2)我们什么时候使用 git reset --hard ,因为即使没有这个命令也可以重置

谢谢

***Solution : Major Edit(s): 03/26 : *** 用git特定术语替换了许多含糊不清的术语[tracked / untracked / staged / unstaged]

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

类型1.分阶段跟踪文件类型2.未分阶段跟踪文件类型3.未分级UnTracked文件a.k.a UnTracked文件

  • 已暂存 - 已移至暂存区/已添加到索引的那些

  • 已跟踪 - 已修改的文件

  • UnTracked - 新文件 . 总是没有分期 . 如果上演,这意味着他们被跟踪 .

What each commands do:

  • git checkout . - 仅删除未分级的跟踪文件[类型2]

  • git clean -f - 仅删除未分级的UnTracked文件[类型3]

  • git reset --hard - 仅删除分阶段跟踪和未分阶段跟踪文件[类型1,类型2]

  • git stash -u - 删除所有更改[类型1,类型2,类型3]

Conclusion:

很明显,我们可以使用其中之一

(1) combination of `git clean -f` and `git reset --hard`

要么

(2) `git stash -u`

达到预期的效果 .

注意:Stashing,因为单词表示'Store (something) safely and secretly in a specified place.'始终可以使用 git stash pop 检索 . 因此,在上述两个选项之间进行选择是开发人员的号召 .

谢谢Christoph和FrederikSchøning .

Edit: 03/27

我以为's worth putting the '要小心'注意 git clean -f

git clean -f

没有回头路 . 使用 -n--dry-run 预览您将要执行的伤害 .

如果还要删除目录,请运行 git clean -f -d

如果您只想删除被忽略的文件,请运行 git clean -f -X

如果要删除已忽略和未忽略的文件,请运行 git clean -f -x

参考:更多关于 git cleanHow to remove local (untracked) files from the current Git working tree?

Edit: 05/20/15

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

为了丢弃此分支上的所有本地提交,要使本地分支与此分支的"upstream"相同,只需运行 git reset --hard @{u}

参考:http://sethrobertson.github.io/GitFixUm/fixup.html

git reset --hard origin/master [如果当地分支是 master ]

Note: 06/12/2015 这是 not 与_477092讨论的其他SO问题的重复 . 因此,这不是另一个的重复]

Edit: 06/23/15

如何还原已经推送到远程存储库的提交?

$ git revert ab12cd15

Edit: 09/01/2015

从本地分支和远程分支删除先前的提交

案例:您刚刚对当地分公司进行了更改并立即推送到远程分支机构,突然意识到,哦不!我不需要这个改变 . 现在做什么?

git reset --hard HEAD~1 [用于从本地分支删除该提交]

git push origin HEAD --force [必须执行这两个命令 . 从远程分支删除]

分支是什么?它是当前签出的分支 .

Edit 09/08/2015 - 删除本地git merge

我在 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.

问:如何摆脱这种合并?尝试 git reset --hardgit clean -d -f 两个都不起作用 .

worked 唯一的东西是以下任何一个:

$ git reset --hard origin/master

要么

$ git reset --hard HEAD~8

要么

$ git reset --hard 9a88396f51e2a068bb7 [sha commit code - 这是在所有合并提交发生之前出现的代码]

9 回答

  • 4

    1.当您不想保留本地更改时 .

    git reset --hard
    

    此命令将从本地存储库中完全删除所有本地更改 . 这是在pull命令期间避免冲突的最佳方法,只有在您根本不想保留本地更改时才这样做 .

    2.当您想要保留本地更改时

    如果你想从远程拉出新的更改并想在此拉动期间忽略本地更改,

    git stash
    

    它将隐藏所有本地更改,现在您可以拉远程更改,

    git pull
    

    现在,您可以通过以下方式恢复您的本地更改:

    git stash pop
    
  • 2

    我认为git有一件事没有明确记录 . 我认为它实际上被忽视了 .

    git checkout .

    伙计,你救了我的一天 . 我总是想要尝试使用修改后的代码 . 但事情有时最终会弄乱修改后的代码,添加新的未跟踪文件等 . 所以我想要做的就是,按照我想要的方式,做一些杂乱的事情,然后快速清理并提交,如果我很高兴的话 .

    git clean -fd 适用于未跟踪文件 .

    那么 git reset 只是删除了暂存,但 git checkout 有点太麻烦了 . 逐个指定文件或使用目录isn 't always ideal. Sometimes the changed files I want to get rid of are within directories I want to keep. I wished for this one command that just removes unstaged changes and here you' re . 谢谢 .

    但我认为他们应该只有 git checkout 没有任何选项,删除所有未分级的更改,而不是触摸上演 . 它有点模块化和直观 . 更像 git reset 的作用 . git clean 也应该这样做 .

  • 2

    最好的方法是检查更改 .

    在名为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'
    
  • 20

    这一切都取决于正是你想要撤消/恢复的内容 . 首先阅读the post in Ube's link . 但要尝试回答:

    Hard reset

    git reset --hard [HEAD]
    

    完全删除所跟踪文件的所有暂存和非暂停更改 .

    我发现自己经常使用硬重置,当我喜欢“只是撤消一切,就像我从遥控器完成了一次完全重新克隆” . 在你的情况下,你只需要你的repo pristine,这将工作 .

    Clean

    git clean [-f]
    

    删除未跟踪的文件 .

    用于删除临时文件,但保留已跟踪文件的暂存和非暂存更改 . 大多数情况下,我可能最终会制定一个忽略规则而不是反复清理 - 例如对于C#项目中的bin / obj文件夹,您通常希望从repo中排除这些文件夹以节省空间,或类似的东西 .

    -f(强制)选项也将删除未被跟踪的文件,并且git通过ignore-rule也会被忽略 . 在上面的例子中,使用ignore-rule永远不会跟踪bin / obj文件夹,即使git忽略了这些文件夹,使用force-option也会将它们从文件系统中删除 . 我偶尔会看到一个用途,例如脚本部署时,您希望在部署,压缩或其他任何内容之前清理代码 .

    Git clean不会触及已经被跟踪的文件 .

    Checkout "dot"

    git checkout .
    

    在阅读你的帖子之前,我实际上从未见过这种表示法 . 我很难找到这方面的文档(也许有人可以提供帮助),但是从玩了一下,看起来它意味着:

    "undo all changes in my working tree" .

    即撤消跟踪文件中的未分级更改 . 它显然没有触及分阶段的变化,只留下未跟踪的文件 .

    Stashing

    一些答案提到了藏匿 . 正如措辞所暗示的那样,你可能会在你处于中间状态时使用存储(没有准备好提交),并且你必须暂时切换分支或以某种方式处理代码的另一个状态,以后再回到你的“杂乱”台” . 我不认为这适用于你的问题,但它绝对方便 .

    To sum up

    一般来说,如果您确信自己已经投入并且可能被推送到远程重要更改,如果您只是玩游戏等,使用 git reset --hard HEAD 后跟 git clean -f 将最终清理您的代码到状态,它会在,只是它只是被克隆并从分支机构检出 . 重要的是要强调,重置还将删除分阶段但未提交的更改 . It will wipe everything that has not been committed (未跟踪的文件除外,在这种情况下,请使用clean) .

    所有其他命令都是为了促进更复杂的场景,其中需要“撤消内容”的粒度:)

    我觉得,你的问题#1已被涵盖,但最后,在#2结束:你从未发现需要使用 git reset --hard 的原因是你从未上过任何东西 . 如果你进行了改变,那么 git checkout .git clean -f 都不会恢复 .

    希望这涵盖 .

  • 18

    对于 discard all 我喜欢藏匿并丢弃该藏匿处,这是丢弃所有内容的最快方式,特别是如果您在多个回购之间工作 .

    这将隐藏 {0} 键中的所有更改,并立即将其从 {0} 中删除

    git stash && git stash drop

  • 7

    使用:

    git checkout -- <file>
    

    放弃工作目录中的更改 .

  • 4

    与git中的所有内容一样,有多种方法可以实现 . 您使用的两个命令是一种方法 . 你可以做的另一件事就是用 git stash -u 隐藏它们 . -u 确保还包括新添加的文件(未跟踪) .

    关于 git stash -u 的方便之处就在于此

    • 它可能是实现目标的最简单(唯一?)单一命令

    • 如果你之后改变了主意,那么 git stash pop 就会得到 all 你的工作(这就像在Gmail中删除一封电子邮件,如果你之后改变主意你就可以撤消)

    从您的另一个问题开始, git reset --hard 将不会删除未跟踪的文件,因此您仍然需要 git clean -f . 但是 git stash -u 可能是最方便的 .

  • 480

    此时添加答案的原因:

    到目前为止,我在最初的问题本身上添加了结论和“答案”,使问题非常冗长,因此转向单独的答案 .

    我还添加了更多 frequently used git commands ,帮助我git,帮助别人 .

    First 在执行任何提交之前的步骤是配置与您的提交一起出现的用户名和电子邮件 .

    #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

    $ git config --list
    

    #check status

    git status
    

    #List all local and remote branches

    git branch -a
    

    #create a new local branch and start working on this branch

    git checkout -b "branchname"
    

    或者,它可以作为两步过程完成

    创建分支: git branch branchname 在此分支上工作: git checkout branchname

    #commit local changes [两步过程: - 将文件添加到索引,这意味着添加到临时区域 . 然后提交此暂存区域中存在的文件]

    git add <path to file>
    
    git commit -m "commit message"
    

    #checkout some other local branch

    git checkout "local branch name"
    

    #remove all changes in local branch [假设您在本地分支中进行了一些更改,例如添加新文件或修改现有文件,或进行本地提交,但不再需要) git clean -d -fgit reset --hard [清除所有本地对本地分支所做的更改,除非本地提交]

    git stash -u 也会删除所有更改

    Note: 很明显,我们可以使用(1) git clean –d –fgit reset --hard OR(2) git stash -u 的组合来实现所需的结果 .

    注1:存储,因为这个词的意思是“在指定的地方安全地,秘密地存储(某物)” . 这总是可以使用git stash pop进行检索 . 因此,在上述两个选项之间进行选择是开发人员的号召 .

    注2: git reset --hard 将删除工作目录更改 . 在运行此命令之前,请务必隐藏要保留的任何本地更改 .

    # Switch to the master branch 并确保您是最新的 .

    git checkout 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.

    git reset origin/master
    

    #Accidentally deleted a file from local , how to retrieve it back? 执行 git status 以获取已删除资源的完整文件路径

    git checkout branchname <file path name>
    

    而已!

    #Merge master branch with someotherbranch

    git checkout master
    git merge someotherbranchname
    

    #rename local branch

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

    #delete local branch

    git branch -D branch-name
    

    #delete remote branch

    git push origin --delete branchname
    

    要么

    git push origin :branch-name
    

    #revert a commit already pushed to a remote repository

    git revert hgytyz4567
    

    #branch from a previous commit using GIT

    git branch branchname <sha1-of-commit>
    

    #Change commit message of the most recent commit that's already been pushed to remote

    git commit --amend -m "new commit message"
    git push --force origin <branch-name>
    

    # Discarding all local commits on this branch [Removing local commits]

    为了丢弃此分支上的所有本地提交,要使本地分支与此分支的“上游”相同,只需运行

    git reset --hard @{u}
    

    参考:http://sethrobertson.github.io/GitFixUm/fixup.html或做 git reset --hard origin/master [如果本地分支是主人]

    # Revert a commit already pushed to a remote repository?

    $ git revert ab12cd15
    

    #Delete a previous commit from local branch and remote branch

    Use-Case: 你刚刚改变了当地的分支机构并立即推到了远程分支机构,突然意识到,哦不!我不需要这个改变 . 现在做什么?

    git reset --hard HEAD~1 [用于从本地分支删除该提交 . 1表示你做的ONE提交]

    git push origin HEAD --force [必须执行这两个命令 . 从远程分支删除] . 当前签出的分支将被称为您进行此操作的分支 .

    #Delete some of recent commits from local and remote repo and preserve to the commit that you want. ( a kind of reverting commits from local and remote)

    让's assume you have 3 commits that you' ve推送到名为' 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 (改变分支状态)

    git log --oneline --decorate --graph //查看所有的提交

    git clean -d -f //清除所有本地更改

    git reset --hard commitid-1 //本地恢复到此commitid

    git push -u origin +develop //将此状态推送到远程 . 做推力

    # Remove local git merge: 案例:我在主分支和合并主分支与新工作分支阶段2

    $ git status
    

    在分支大师

    $ git merge phase2 $ git status

    在分支大师

    您的分支在 8 提交之前领先于'origin/master' .

    问: How to get rid of this local git merge? 试过 git reset --hardgit clean -d -f 两个都不起作用 . worked 唯一的东西是以下任何一个:

    $ git reset --hard origin / master

    要么

    $ git reset --hard HEAD~8

    要么

    $ git reset --hard 9a88396f51e2a068bb7 [sha commit code - 这是在所有合并提交发生之前存在的代码]

    #create gitignore file

    touch .gitignore //在mac或unix用户中创建文件

    样本.gitignore内容:

    .project
    *.py
    .settings
    

    GIT备忘单的参考链接:https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf

  • 1

    首先检查是否保存了您的重要更改:

    $ git状态

    比尝试

    $ git reset --hard它会将你的分支重置为默认值

    但如果你只需要撤消:

    $ edit(1)$ git add frotz.c filfre.c $ mailx(2)$ git reset(3)$ git pull git://info.example.com/nonfol


    阅读更多>> https://git-scm.com/docs/git-reset

相关问题