7 Undo Changes

7.1 Unfo Working Directory Changes

  1. Suppose you made changes to a file accidentally and closed the file. How do you undo the changes?

    • git diff shows us the changes. No need to copy paste or retype.

    • git checkout -- index.html to discard changes in working directory

      • -- is how you tell git not to checkout a branch.

      • new git tells me to use git restore

7.2 Unstage Files

  • to unstage a file, use git reset HEAD tours.html

    • reset back to HEAD.

7.3 Amend commits

  • the SHA value of a commit is a function of the previous commit’s SHA value.

    • this is how git maintains data integrity (and knows history of commits).

  • thus, if you wanted to make changes to a previous commit, it’s not easy (I don’t know how) since future commits will no longer point to what you intended.

  • There’s an exception: you can make changes to the commit that HEAD is pointing to.

  • Suppose you make changes to some files and commit them. But you realize you didn’t make all the changes. What will you do?

    • git add changes

    • git commit --amend -m "new message"

    • git log now shows one commit and git show SHA shows all changes together.

  • Suppose you don’t like your commit message “new message”, what do you do?

    • git commit --amend -m "really good new message"

7.4 Retrieve old versions

  • git only allows amending the latest commit.

  • to undo changes, the correct way is to make the undos (edits) new commits.

  • but it may be helpful to retrieve an old version of a file so we can revert back to that version.

  • suppose you want to retrieve a file and see how it looked before you made some changes.

    • grab SHA value of that commit (a1b2c3)

    • git checkout a1b2c3 -- explorers.html

    • this not only modifies the file but stages that version (git status)

  • git diff --staged shows the differences. Commiting this would essentially undo the changes

  • you can make further changes to a staged file (but these changes won’t be staged). use git add.

  • you could commit this but in the next section, we’ll see a shortcut on how to revert a commit.

7.5 Revert a commit

  • to revert changes made in commit a1b2c3, first see changes in that commit using git show a1b2c3.

  • then use git revert a1b2c3 to revert changes. This takes you to the editor with a message displaying the SHA of the commit that is reverted.

  • this is one of the reasons changes should be atomic. It allows us to revert bad changes without worrying about the other good changes that may be squeezed into the same commit.

7.6 Remove Untracked Files

  • Files may get accidentally added to the repository that we don’t want.

  • git clean -n is an intermediate step that says what would happen to untracked files.

    • If something is staged, that would not be removed.

  • git clean -f actually removes untracked files.

  • git reset HEAD junk1.txt unstages junk1 which can then be cleaned.

  • Destructive process, use with caution. Useful when the repository a lot of untracked files that you don’t need.