Adding more commits
Simple changes
Let's add a comment to the Python file:
To commit this, we need to simply add it to the stage and commit, like last time:
This pattern of "change file, add file, commit file" is so common, Git has a shortcut: git commit -a -m "your message"
will add all the files you've changed since the commit, so you don't need to run any git add
commands. Note that this does not add untracked (new) files, you'll still need to use git add
for those.
Adding new files
Let's make our Python script import a file:
This time we got another section of output
Changes not staged for commit
These files are being tracked by Git, but you haven't told Git you're ready to commit these files. Git wants you to explicitly stage your changes, meaning you have to tell Git which files you want to commit before committing them. Note that Git works on a file basis here, so you can't stage or commit only certain lines in a file at a time.
And just like before, we'll add the new file so Git can track it:
Now we're ready to commit:
git status -sb
gives a more concise output. We'll use that from now on, but feel free to run without it.
Ignoring files
What happened? We had a clean working tree, but when we ran the code, Python generated a cache that contained the compiled output of data.py
. In general, compiling code will often produce files that you don't want to commit because they're not the actual source of truth; they can be derived from other files. We want to tell Git to ignore __pycache__
and not track it. We can do this by creating a gitignore file, which is just a file called .gitignore
Now, Git is telling us we have a new file called .gitignore
. but it no longer cares about __pycache__
. It might feel weird to commit the file that tells Git what files to not commit, but if you didn't, your fellow team members would run into the same problems when they run your code. Every project gets to specify which files should be ignored.
Last updated
Was this helpful?