9 Undoing Uncommitted Changes

Git’s two-step process for tracking a commit means you can have files that are staged for commit that you’re not ready to commit. You use git reset HEAD or git rm --cached depending on the circumstance.

Scenario 1: You staged a change to a file and want to unstage it—use git reset HEAD. This is the most common use. You’re telling Git, “Change the index—the staging area—to the latest version of this file.”

Scenario 2: You have a new file that’s been staged that you don’t want to commit now—use git rm --cached. Normally, git rm is used to remove files from your repository, but adding the --cached option tells Git to leave your working tree alone.

Another common problem is making changes that you want to undo completely. You can use git checkout to do this, but be careful. git checkout happily removes all untracked changes from a file or directory. You can’t get those changes back if they were never tracked by Git.

What To Do...
  • Unstage a modified file that’s been staged.

    For example, to undo changes to cache.py, use this:

     
    prompt>​ git reset HEAD -- cache.py
     
    Unstaged changes after reset:
     
    M cache.py

    If you’re not familiar with command-line programs, you might not recognize that --. It tells Git that all arguments are done and that the rest are files or paths. It’s useful when files and branch or tag names clash.

  • Undo all uncommitted changes to a file.

    Warning: Doing this deletes files and cannot be undone.

     
    prompt>​ git checkout -- cache.py

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.68