代码之家  ›  专栏  ›  技术社区  ›  Geo

如何将git文件还原为其临时区域版本?

git
  •  62
  • Geo  · 技术社区  · 15 年前

    假设我有一个名为 a.txt . 我将它添加到临时区域,然后修改它。我怎么能把它恢复到我添加它时的样子呢?

    2 回复  |  直到 15 年前
        1
  •  79
  •   Alexander Pozdneev Andrew Shepherd    4 年前
    • 在Git 2.23之前: git checkout a.txt
    • 从Git 2.23开始: git restore a.txt

    git status .

    在Git 2.23之前:

    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    # modified:   a
    #
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified:   a
    #
    

    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   a
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   a
    
        2
  •  32
  •   nonrectangular    10 年前

    git checkout -- a.txt

    此页上的另一个答案没有 -- ,并导致了一些混乱。

    这是Git在你打字时告诉你的 git status :

    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    # modified:   a
    #
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified:   a
    #
    
        3
  •  2
  •   Girish Rathi    5 年前

    取消暂存文件

    接下来的两部分将演示如何处理临时区域和工作目录更改。好的一点是,用于确定这两个区域的状态的命令还提醒您如何撤消对它们的更改。例如,假设您更改了两个文件,并希望将它们作为两个单独的更改提交,但您意外地键入git add*并将它们都转移到后台。你怎么能拆开其中一个呢?git status命令提醒您:

    $ git add *
    $ git status
    
    On branch master
    Changes to be committed:
    (use "git reset HEAD <file>..." to unstage)
    
    renamed:    README.md -> README
    modified:   CONTRIBUTING.md
    

    在要提交的更改文本的正下方,写着使用git重置头。。。拆箱。因此,让我们使用该建议来取消stage CONTRIBUTING.md文件:

    $ git reset HEAD CONTRIBUTING.md
    Unstaged changes after reset:
    M   CONTRIBUTING.md
    
    $ git status
    On branch master
    Changes to be committed:
    (use "git reset HEAD <file>..." to unstage)
    
    renamed:    README.md -> README
    
    Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git checkout -- <file>..." to discard changes in working directory)
    
    modified:   CONTRIBUTING.md