×
By the end of this chapter, you should be able to:
git checkoutgit checkout and git cleangit rm --cachedgit reset--soft, --mixed, and --hard flags when performing a git resetAs you get more comfortable adding and committing changes using git, you may sometimes accidentally commit something you didn't mean to, or you may want to remove some file that's in the working directory before it's been staged. We'll show you some techniques to undo your changes in this chapter. Please be warned that some of these commands are not reversible, so use them with caution!
checkoutIf you want to remove files from the working directory (before they have been staged) you can use git checkout NAME_OF_FILE. Be careful with this - you can not undo this command!
Here's a quick example. Create a new git repository, then add and commit a blank file called first.txt. Once you've committed the file, echo hello > first.txt to add some text to the file. If you check git status now, you'll see that first.txt is not staged for commit. If you decide that you don't like the change you just made to the file, you can type git checkout -- first.txt. If you cat first.txt you'll see the file is empty again!
cleanIf you are dealing with an untracked or unmerged file, you cannot use git checkout to remove it from the working directory. You must use git clean -df to remove these files. Be careful with this - you can not undo this command either! (Curious about the -df flags? Run man git-clean to learn more about this command!)
git rm --cachedWe've seen what to do if we have something in the working directory that we want to remove. But what if we accidentally add something to the staging area and want to move it back to the working directory? To do this, you can type git rm --cached NAME_OF_FILE. If you need to remove a folder pass the -r flag to git rm --cached. If you want to move all of your files in the staging area to the working area you can type git rm -r --cached .. If you want remove your files from the staging area AND the working directory, you can type git reset --hard HEAD, but be careful - this can not be undone!
resetWe've seen how to remove things from the working directory, and how to remove things from the staging area back into the working directory. But sometimes we end up committing things we do not want to be remembered. To undo commits we can use the git reset command. There are 3 flags we can pass to this:
git reset --soft COMMIT_SHA - moves the files committed back to the staging area
git reset --mixed COMMIT_SHA - moves the files committed back to the working directory (if you use git reset without a flag, the default will be --mixed)
git reset --hard COMMIT_SHA - undoes the entire commit (dangerous!!!)
What's the COMMIT_SHA, you ask? You may have noticed that every commit has a unique identifier, called a sha, which identifies that commit. If you type git log --oneline, you'll see your list of commit messages along with the first seven characters of the commit sha. This is what you should pass into each of these commands.
Using reset will not change the commit that you switch to but any commits that have come after it.
So if we have 4 commits:
a808698 Fourth commit ca0bbb4 Third commit 5ffcac5 Second commit ac49968 First commit
And I want to move the last two commits to the staging area:
git reset --soft 5ffcac5, this will move whatever files we had in the Fourth commit and Third commit back to the staging area.
destruction.cd into that folder.done.txt.git checkout).stage_me.txt.stage_me.txt file to the staging area.stage_me.txt file from the staging area to the working directory.stage_me.txt file to the staging area.stage_me.txt from the staging area and the working directory.commit_me.txt.commit_me.txt to the staging area.second.txt.second.txt to the staging area.git log --oneline to see the unique identifier or SHA for each of your commits.git reset, undo the previous commit and move your changes back to the working directory.second.txt again.git reset undo the previous commit and move your changes back to the staging area.git reset undo the previous commit so that any changes are not part of the working directory. Well done! Play around some more with these commands as they will be essential when dealing with larger files, branches, and merges.
When you're ready, move on to Branching