MaruOpsInfrastructure and AI notes

About (Korean)Contact (Korean)

[Git and GitOps 05] Make a First Commit with add, commit, status and diff

Laptop keyboard and trackpad

Reference photo · Laptop keyboard and trackpad · Photo: Scott Webb scottwebb / Source / CC0 · Resized for listings; illustrative photo, not the tested environment

Choose the files and changes that belong in a commit. Inspect the targets with status, read the changes with diff, then use add and commit. A small example shows how the working tree and staging area can hold different versions of one file.

Let’s start with Git installed and author setup. The shell commands below run in Linux Bash or Windows Git Bash. Replace the Python executable with python3 on Linux and py -3 on Windows, depending on your installation. The file history itself does not require Python.

Git · GitOps All Series · 05 / 24

On this page

1. Create a repository within the project boundary

cd ~/git-lab
mkdir gitops-demo
cd gitops-demo
git init -b main
printf '# GitOps Demo\n' > README.md
cat > greeting.py <<'EOF'
def greet(name):
    cleaned = name.strip()
    if not cleaned:
        raise ValueError("name must not be empty")
    return f"Hello, {cleaned}!"

if __name__ == "__main__":
    print(greet("Git"))
EOF
git status --short

?? refers to files that are not yet tracked. Do not git init your entire home directory or multiple projects at once. The gitops-demo you are creating now is the unit that divides the change history and sharing permissions.

2. Select the files to be included in the next commit

git add README.md greeting.py
git diff --staged
git status --short
git commit -m "Add greeting example and README"
git log -1 --oneline

git add is not a command that only reserves file names. The contents at the time of execution are stored in the index. Even if you don’t see anything in git diff in the first commit, you may see new file content in git diff --staged.

The commit message explains what changed. If it is a complicated modification, you can add the reason for the change to the text. Avoiding mixing documentation typos and deployment permission changes in one commit makes it easier to review and revert.

3. What will change if you edit again after add?

Add a description to the README, stage it, and then add one more line. This example shows that the same file can be in different states in the two spaces.

printf '\nSmall Git learning project.\n' >> README.md
git add README.md
printf 'More examples will follow.\n' >> README.md
git status --short
git diff -- README.md
git diff --staged -- README.md

The two status columns represent changes in the index and work tree, respectively. In the example above, MM can be seen in the same file. The modifications in the front have been staged, but the modifications in the back have not been included yet.

commandobject of comparisonTime of writing
git diffIndexes and work treesCheck for changes not yet included
git diff --stagedHEAD and indexCheck the contents of this commit
git diff HEADHEAD and work treeCheck full changes in trace file

If you decide to include both lines in this commit, run git add README.md again. When you need to pick out just a few changes, you can review chunks of changes with git add -p. First, get into the habit of dividing into files.

git add README.md
git diff --check
git diff --staged --check
git commit -m "Explain the learning project"
git status

diff --check helps find issues like white space errors or conflicting displays. It is not a substitute for testing to ensure that your program is operating correctly. If you have Python installed, you can run the examples as python3 greeting.py or py -3 greeting.py on Windows.

There is a server between commit and push

The two commits we just made are local only. commit does not transfer files to others. You must connect the remote repository of Part 10 and use push to view the same history on the server.

The bundle of example files in the series guide also includes tests to be used later in CI. Do not overwrite the existing .git, but only copy the files you need. You can check the comparison standard of the command in detail at git diff, and the file status at git status.

Previous · [Git and GitOps 04] Install Git and Prepare a Repository on Ubuntu 24.04 · Next · [Git and GitOps 06] Read History with HEAD, Graphs, diff and bisect·Serial list and example file