Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
git_undoing_things [2019/02/26 09:45] – [Undoing unstaged changes (part one)] rpjdaygit_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 what you just did. 
 + 
 +===== 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):
  
 <code> <code>
 $ git reset --hard $ git reset --hard
 </code> </code>
 +
 +They're gone.
  
 ==== Undoing unstaged changes (part one) ==== ==== Undoing unstaged changes (part one) ====
  
-From ''man git-checkout'':+Say you've made some local changes, but have staged nothing so far. From ''man git-checkout'':
  
 <code> <code>
Line 24: Line 38:
 </code> </code>
  
-To undo some local (unstaged) edits to ''README'', overwrite your local copy with the copy //from the index//:+To undo some local (unstaged) edits to ''README'', overwrite your working tree copy with the copy //from the index//:
  
 <code> <code>
Line 30: Line 44:
 </code> </code>
  
-To undo //all// unstaged changes:+To undo //all// unstaged changes throughout the working tree:
  
 <code> <code>
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 (unstagedchanges, see above.+If you have already staged some changes, then made //additional// changes and want to undo //only// those additional, unstaged changes, see above -- it works the same way. 
 + 
 +<code> 
 +$ git status 
 +On branch master 
 +Your branch is up to date with 'origin/master'
 + 
 +Changes to be committed: 
 +  (use "git reset HEAD <file>..." to unstage) 
 + 
 + modified:   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:   README 
 + 
 +
 +</code> 
 + 
 +Discard unstaged changes: 
 + 
 +<code> 
 +$ git checkout -- README 
 +</code> 
 + 
 +End result -- your unstaged changes are lost, while the staged changes in the index are right where you left them. 
 + 
 +<code> 
 +$ git status 
 +On branch master 
 +Your branch is up to date with 'origin/master'
 + 
 +Changes to be committed: 
 +  (use "git reset HEAD <file>..." to unstage) 
 + 
 + modified:   README 
 + 
 +</code> 
 + 
 +==== Undoing all staged and unstaged changes ==== 
 + 
 +<code> 
 +git checkout [<tree-ish>] [--] <pathspec>... 
 +    Overwrite paths in the working tree by replacing with the 
 +    contents in the index or in the <tree-ish> (most often a 
 +    commit). When a <tree-ish> is given, the paths that match 
 +    the <pathspec> are updated both in the index and in the 
 +    working tree. 
 +</code> 
 + 
 +To overwrite with the most recently //committed// version of the file: 
 + 
 +<code> 
 +$ git checkout HEAD -- README 
 +</code> 
 + 
 +Recall how to do this for everything in the working tree: 
 + 
 +<code> 
 +$ git reset --hard 
 +</code> 
 + 
 +==== Unstaging staged content ==== 
 + 
 +From ''man git-reset'': 
 + 
 +<code> 
 +       git reset [<mode>] [<commit>
 + 
 +       ... 
 + 
 +           --mixed 
 +               Resets the index but not the working tree (i.e., the 
 +               changed files are preserved but not marked for commit) 
 +               and reports what has not been updated. This is the 
 +               default action. 
 +</code> 
 + 
 +Example: 
 + 
 +<code> 
 +$ git reset -- README 
 +</code> 
 + 
 +==== Restoring accidentally-deleted files ==== 
 + 
 +Recall: 
 + 
 +<code> 
 +git checkout [<tree-ish>] [--] <pathspec>... 
 +    Overwrite paths in the working tree by replacing with the 
 +    contents in the index or in the <tree-ish> (most often a 
 +    commit). When a <tree-ish> is given, the paths that match 
 +    the <pathspec> are updated both in the index and in the 
 +    working tree. 
 +</code> 
 + 
 +To restore file from HEAD: 
 + 
 +<code> 
 +$ git checkout HEAD -- <file> 
 +</code> 
 + 
 +To restore file from index: 
 + 
 +<code> 
 +$ git checkout -- <file> 
 +</code>
  • git_undoing_things.1551174357.txt.gz
  • Last modified: 2019/02/26 09:45
  • by rpjday