Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Rewriting Your Git History

00:00 Now you’re at the point where git status shows that you have some changes that you do care about in the file prnt.py, but also changes in a file inside a folder that you didn’t really want to commit in the first place.

00:13 So how are you going to fix this problem? Well, the first thing you’re going to do is you’re going to follow the tip that Git tells you, and you’re going to use the command git restore to restore the changed files, but only the ones that Git is already tracking.

00:27 You’re going to restore them to the previous commit, to the previous snapshot state. So if you run git restore ., Git will take all of your files that have been changed since the last commit, and it will put them back into that state.

00:42 Now be careful, because you only want to do if you’re sure that you want to restore those files, because there’s no undoing. You cannot undo this action. So now you press Enter, and if you run git status, you’ll see that Git finds no untracked changes to commit.

00:58 Now what you want to do is you want to go back in history and get rid of __pycache__ as far as Git is concerned. If you run the command git log, you’ll find that the top commit shows the commit where you provided the module prnt for printing, and that’s also where you added the module __pycache__.

01:19 So now what you want to do is reset the history to the point in time that’s immediately before. Git has many, many tools for editing your history and for navigating your history efficiently, so this is just a very specific application.

01:36 You’ll want to learn more if you have to do more complex editing. But if you want to go back one commit in history, you can run the command git reset to reset the history, and then, all uppercase, you type HEAD, tilde (~), and then the number one.

01:53 You can essentially read this as “Reset my Git history to the previous commit.” And so when you press Enter, you’ll see a message from Git telling you that now there are some unstaged changes for the file myscript.py.

02:08 And if you run git status, you’ll see that your file system is currently at the point you were at immediately before making the last commit, which was precisely the commit where you added __pycache__ by mistake.

02:23 So this time you’re going to add myscript and prnt to the staging area, but you’re not going to add __pycache__. How can you do that?

02:32 Well, you can run the command git add, and you can explicitly list the files that you care about. So that’s going to be myscript.py and prnt.py.

02:41 You press Enter, and then if you run git status again, you’ll find that the files myscript and prnt are ready to be committed, while the folder __pycache__ is not being tracked.

02:55 So now you run git commit. You can reuse the message from before. You can say “Provide module prnt for printing.” You press Ctrl+O and then Enter and then Ctrl+X, and there you go.

03:09 Now if you run git log, you’ll see again your two commits, but the top one is the one that you just made. Now press Q to exit the log, and if you run git status, you’ll see that the folder __pycache__ is still not being tracked.

03:24 It’s kind of annoying that Git is even paying attention to it. So in the next lesson, you’re going to learn how to tell Git to ignore the folder __pycache__ altogether.

03:34 But first, let’s do a quick review of the commands you’ve been learning. So you have some files in your file system that you changed, and those are represented by the red rectangles.

03:44 And you can use the command git add to add those files to the staging area, which tells Git that you want to track those files and in particular those changes.

03:54 Now if by mistake you add something that you didn’t mean to add, you can always use git restore to bring your files from the staging area back to just the file system.

04:04 So the files stay there, you can still look at them, edit them, but Git will not track those changes. Now if you use the command git log, you can take a look at the history of commits you’ve made, which is this sequence of snapshots that you took.

04:19 And if you have some files in the staging area, you can use git commit to create a new commit. Now what you just learned was that if you make a commit by mistake, you can use the command git reset HEAD~1 to bring your file system to the state of the previous commit.

Become a Member to join the conversation.