Adding more commits

Simple changes

Let's add a comment to the Python file:

hello.py
# Print a greeting to the user
print("Hello, World!")

To commit this, we need to simply add it to the stage and commit, like last time:

$ git add hello.py
$ git commit -m "Added a comment"
# [main c5ac38c] Added a comment
#  1 file changed, 1 insertion(+)

Adding new files

Let's make our Python script import a file:

data.py
GREETING = "Hello, World!"
hello.py
import data

# Print a greeting to the user
print(data.GREETING)
$ git status
# On branch main
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git restore <file>..." to discard changes in working directory)
#         modified:   hello.py
# 
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#         data.py
# 
# no changes added to commit (use "git add" and/or "git commit -a")

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.

$ git add hello.py
$ git status
# On branch main
# Changes to be committed:
#   (use "git restore --staged <file>..." to unstage)
#         modified:   hello.py
# 
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#         data.py

And just like before, we'll add the new file so Git can track it:

$ git add data.py
$ git status
# On branch main
# Changes to be committed:
#   (use "git restore --staged <file>..." to unstage)
#         new file:   data.py
#         modified:   hello.py

Now we're ready to commit:

$ git commit -m "Moved the greeting message to data.py"
# [main f53d7d2] Moved the greeting message to data.py
#  2 files changed, 4 insertions(+), 1 deletion(-)
#  create mode 100644 data.py
$ git status
# On branch main
# nothing to commit, working tree clean

Ignoring files

$ git status -sb
# ## main
$ python hello.py
# Hello, World!
$ git status -sb
# ## main
# ?? __pycache__/

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

.gitignore
__pycache__/
$ git status -sb
# ## main
# ?? .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.

$ git add .gitignore
$ git status -sb 
# ## main
# A  .gitignore
$ git commit -m "Added __pycache__/ to gitignore"
# [main 92eca49] Added __pycache__/ to gitignore
#  1 file changed, 1 insertion(+)
#  create mode 100644 .gitignore

Last updated

Was this helpful?