{:check ["true"], :rank ["unstaged_files" "staged_files"]}

Index

Undoing Changes

box

https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things

A major benefit from working with version controlled file system is that the entire change history is available in the repository. This means that we are able to reverse changes made to the files at anytime.

Unstaged Files

Undo changes to unstaged files.

  • The situation:

    split=8
    +-------------------+  +---------+   +------------+
    | repository        |  | staging |   | working    |
    | with commits:     |  | area    |   | directory  |
    |      C1           |  |         |   |            |
    |      C2           |  |         |   | <file>(B)  |
    |      C3           |  |         |   |            |
    |      C4           |  |         |   |            |
    |      C5           |  +---------+   +------------+
    | HEAD C6  <file>(A)|
    +-------------------+
    

    Suppose that we have made changes to file.

    • The original file from the repository is (A).
    • Changes have been made in the working directory (B).
  • Undo changes in working directory.

    We can revert the version of <file> in the working directory to the versions in the repository.

    split=8
    \$ git checkout HEAD -- <file>
    

    Copies the <file>@HEAD into the working directory. All recent changes to <file> will be lost.

    split=8
    \$ git checkout -- <file>
    

    The default checkout is from the HEAD of the current branch.

    split=8
    \$ git checkout <commit> -- <file>
    
    \$ git checkout HEAD -- <file>
    \$ git checkout HEAD~1 -- <file>
    \$ git checkout HEAD~2 -- <file>
    \$ git checkout HEAD~3 -- <file>
    

    We can check out a file from any commit. The commit can be specified by its hash name, or by its relative offset from the HEAD.

Staged Files

Undo staged files

  • The situation:

    split=8
    +-------------------+  +--------------+  +------------+
    | repository        |  | staging      |  | working    |
    | with commits:     |  | area         |  | directory  |
    |      C1           |  |              |  |            |
    |      C2           |  | <file>(B) <------ <file>(B)  |
    |      C3           |  |              |  |            |
    |      C4           |  |              |  |            |
    |      C5           |  +--------------+  +------------+
    | HEAD C6  <file>(A)|
    +-------------------+
    

    Suppose that we have made changes to file.

    • The original file from the repository is (A).
    • Changes have been made in the working directory (B).
  • Undo the staged version

    \$ git reset HEAD <file>
    

    or simply

    \$ git reset <file>
    
    split=8
    +-------------------+  +--------------+  +------------+
    | repository        |  | staging      |  | working    |
    | with commits:     |  | area         |  | directory  |
    |      C1           |  |              |  |            |
    |      C2           |  |              |  | <file>(B)  |
    |      C3           |  |              |  |            |
    |      C4           |  |              |  |            |
    |      C5           |  +--------------+  +------------+
    | HEAD C6  <file>(A)|
    +-------------------+
    

    The git reset unstages the <file> by reverting the copy in the staging area to the HEAD copy.