Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| git_undoing_things [2019/02/26 09:49] – [Undoing unstaged changes (part deux)] rpjday | git_undoing_things [2019/02/26 12:32] (current) – [Unstaging staged content] rpjday | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== Overview ===== | ===== Overview ===== | ||
| - | Various ways to undo, backpedal and regret. | + | Various ways to undo, backpedal and regret |
| + | |||
| + | ===== The state of files ===== | ||
| + | |||
| + | For what follows, each file has three states: | ||
| + | |||
| + | * HEAD: its state as of the most recent commit on this branch | ||
| + | * staged: the changes added to the index | ||
| + | * local changes: the combination of both staged and unstaged changes | ||
| + | |||
| + | Within a fresh editing session, until you stage some changes to a file, it will have the same state in the index as in HEAD. | ||
| ===== Undoing uncommitted changes ===== | ===== Undoing uncommitted changes ===== | ||
| ==== Throwing away all staged and unstaged changes ==== | ==== Throwing away all staged and unstaged changes ==== | ||
| + | |||
| + | If you have no interest in //any// of the local changes (staged or unstaged): | ||
| < | < | ||
| $ git reset --hard | $ git reset --hard | ||
| </ | </ | ||
| + | |||
| + | They' | ||
| ==== Undoing unstaged changes (part one) ==== | ==== Undoing unstaged changes (part one) ==== | ||
| - | From '' | + | Say you've made some local changes, but have staged nothing so far. From '' |
| < | < | ||
| Line 24: | Line 38: | ||
| </ | </ | ||
| - | To undo some local (unstaged) edits to '' | + | To undo some local (unstaged) edits to '' |
| < | < | ||
| Line 30: | Line 44: | ||
| </ | </ | ||
| - | To undo //all// unstaged changes: | + | To undo //all// unstaged changes |
| < | < | ||
| Line 38: | Line 52: | ||
| ==== Undoing unstaged changes (part deux) ==== | ==== Undoing unstaged changes (part deux) ==== | ||
| - | If you have already staged some changes, made additional changes and want to undo just those local (unstaged) changes, see above. | + | If you have already staged some changes, |
| < | < | ||
| Line 65: | Line 79: | ||
| </ | </ | ||
| - | End result: | + | End result |
| < | < | ||
| Line 90: | Line 104: | ||
| </ | </ | ||
| - | So: | + | To overwrite with the most recently // |
| < | < | ||
| $ git checkout HEAD -- README | $ git checkout HEAD -- README | ||
| + | </ | ||
| + | |||
| + | Recall how to do this for everything in the working tree: | ||
| + | |||
| + | < | ||
| + | $ git reset --hard | ||
| + | </ | ||
| + | |||
| + | ==== Unstaging staged content ==== | ||
| + | |||
| + | From '' | ||
| + | |||
| + | < | ||
| + | git reset [< | ||
| + | |||
| + | ... | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | and reports what has not been updated. This is the | ||
| + | | ||
| + | </ | ||
| + | |||
| + | Example: | ||
| + | |||
| + | < | ||
| + | $ git reset -- README | ||
| + | </ | ||
| + | |||
| + | ==== Restoring accidentally-deleted files ==== | ||
| + | |||
| + | Recall: | ||
| + | |||
| + | < | ||
| + | git checkout [< | ||
| + | Overwrite paths in the working tree by replacing with the | ||
| + | contents in the index or in the < | ||
| + | commit). When a < | ||
| + | the < | ||
| + | working tree. | ||
| + | </ | ||
| + | |||
| + | To restore file from HEAD: | ||
| + | |||
| + | < | ||
| + | $ git checkout HEAD -- < | ||
| + | </ | ||
| + | |||
| + | To restore file from index: | ||
| + | |||
| + | < | ||
| + | $ git checkout -- < | ||
| </ | </ | ||