Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Updating Tracked Code
00:00
As you saw in the previous lesson, the file myscript.py has a bug. And that’s because you’re using the wrong function to print. You typed prnt instead of print.
00:11 So, how do you fix it? Well, the most natural fix would be to fix the typo. But you’re going to overengineer the solution here just a bit to explore a couple of other Git features.
00:22
You’re going to start by creating a file called prnt.py. And in it, you’re going to set prnt to the built-in print.
00:34
Save it. You go back to your original script. And from prnt, you’re going to import prnt. Now, if you save both files and you go back to your terminal, you should see that you can now run your original script and that it works just fine.
00:53
So now, looking at git status, you’ll find that there’s a section saying that there are changes that haven’t been staged for commit. And you should find the myscript file there because Git is already tracking it.
01:07
But the file is now different. And in the section “Untracked files,” you should find the file prnt.py because it’s a new file that isn’t being tracked yet.
01:16
And alongside it, you should find the __pycache__ folder. Now, you’re going to add myscript.py and prnt.py so that you can create a new commit that snapshots the current content of these files.
01:30
The first thing you need to do is to use the git add command and list the files that you want to add to the staging area. And as a shortcut, you can use a dot (.) to refer to the contents of the current repo.
01:41
So you can run git add .. And if you run git status again, you’ll find that your files myscript and prnt.py have been staged.
01:51
Now, to make the commit, you run git commit. You press Enter. You’re going to open your editor to write a commit message. And you can say “Provide module prnt for printing.”
02:03 And then you can press Ctrl+O and Enter to confirm that you’re saving and then Ctrl+X to exit. Once you’re done, you’ll see that there were three files changed and three insertions in this commit.
02:19
As soon as you finish that commit, you might be thinking that it might actually be a good idea to make sure that your module prnt includes some other common misspellings and typos of the built-in print.
02:32
So you might actually cover, for example, prit and also pint. Now you save your code and you’re going to open your terminal again and run myscript.py just to make sure that everything still works.
02:46
It does. So now it’s time to add the new changes to the file prnt.py. And if you run git status, you’ll see that prnt.py is shown as having been changed.
02:59
But so does a file inside __pycache__. And __pycache__ is just a folder that contains some cached compiled code that you don’t really care about.
03:08
So in hindsight, it was maybe a bad idea to ask Git to track __pycache__ because it just adds noise. It’s a caching folder. You don’t really want to track it in your project history.
03:21 So what you’re going to do in the next lesson is figure out how you can actually fix this mistake. How can you go back in history and remove things that shouldn’t have been added in the first place?
Become a Member to join the conversation.