$_ CLI In Depth
Learning progress 0%
lesson performance tmux vim git

git from the Command Line

5 min read Module 18: Performance, Debugging & Power Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:08

Objects, commits, and branches

A git repository is a directory with a hidden .git folder inside it. That folder holds the objects: blobs for file contents, trees for directories, and commits, each of which names one tree and its parents. Every object is identified by a hash of its contents. Two commits with the same content and parents get the same identifier, which is why history cannot be changed silently.

A branch is a file that holds one commit identifier, and it moves forward when you commit on it. HEAD is the pointer that says which branch you have checked out. When you commit, git creates a new commit whose parent is the commit HEAD points to, and then moves the branch to the new commit.

The three areas

Your files live in three places. The working tree holds the files you edit. The index, also called the staging area, holds the content that will go into the next commit. The repository holds the committed history. git add copies changes from the working tree into the index, and git commit turns the index into a commit. git status compares the three and shows what differs.

BASH4 lines
git init -q repo
echo hello > repo/hello.txt
git -C repo add hello.txt
git -C repo status --short

The -C option runs git as if you had changed into the directory first, which keeps scripts free of cd. The status output after an add shows a staged file marked with a letter in the first column. The short format prints one line per file, and the first column is the index state while the second is the working tree state.

Identity and the first commit

A commit records an author, so git needs a name and an email. Set them with git config, either globally or in one repository. A repository-level setting is the safer choice for a lab, because it does not change the account-wide configuration. Then commit with a message.

BASH3 lines
git -C repo config user.name "Lab User"
git -C repo config user.email "lab@example.com"
git -C repo commit -q -m "first commit"

The message is the subject line of the commit. Keep it short and describe the change, because the one-line log uses it as the only description. A commit without a message is not allowed, and an empty commit is refused unless you ask for it.

Reading history

git log lists commits from the newest to the oldest, starting at HEAD. The --oneline option prints one line per commit with a short identifier and the subject. git log --all includes every branch. git branch --list shows the branch names, with an asterisk on the one HEAD points to, and git switch -c creates a branch and moves to it.

BASH2 lines
git -C repo log --oneline
git -C repo branch --list
Tip

When a command is unclear, check its options with git help followed by the command name, or with the --help flag. The lab has no man pages, and git help prints the same reference in a shorter form.

Committing changes to tracked files

The -a option of commit stages every change to a file that git already tracks, and then commits. It does not add new files, which still need git add. For small edits to a project, commit -a is a quick way to make a commit, but it deserves care, because it also includes any change you forgot about.

Recovering and checking

git diff shows the changes in the working tree that are not yet staged, and git diff --cached shows the staged changes. git reflog lists where HEAD has pointed, which is the way back from a mistake such as a reset. Those commands are safe to run, because they only read. Commands that move branches or rewrite history are the ones to read about first.

For the reference, read the git book and the command documentation on the git-scm site. Both are free and both use the same terms as this lesson.

Ignoring files you do not commit

A file named .gitignore in the repository lists patterns for files that git should not track, such as build output, editor backups, and local secrets. Patterns that are already tracked stay tracked until you remove them from the index. Keep the ignore file in the repository so that every clone shares the same rules.

Remotes and cloning

A remote is another copy of the repository, usually on a server. git clone copies a repository and sets up its remote, and git push and git pull move commits between the copies. The lab has no network, so every exercise uses local repositories. The commands are the same when the remote is a server, which is why the local practice carries over directly.

Why the commit identity is set in the repository

A repository-level configuration stays with that repository, so a lab or a test does not change the identity used for other projects. The global configuration lives in a file in your home directory, and the local one lives inside .git. When both are set, the local value wins for that repository, which is what you want for an experiment.

Stash and temporary work

git stash saves the changes in the working tree and the index and returns the files to the last commit. A later git stash pop brings them back. It is a short-term holding area, so use it when you need a clean tree for a moment, and use a branch for anything that will last longer than an afternoon.

Merges and conflicts

Joining two branches with git merge creates a commit that has both branches as parents when the histories have diverged. When both branches changed the same lines, git marks the conflict in the file, and you choose the text to keep before you commit the resolution. The status command lists the files that still need attention, and the conflict markers are plain text you can read in any editor.

Terminal lab · 3 steps · real Linux in your browser

Create a repository and commit to it

Initialize a repo in /workspace, make two commits on a branch, and read the history back.

01
Initialize a repo and make a commit
git init creates a repository in a new directory. Commits need an author, so set user.name and user.email in the repo. Then add a file and commit it: ``bash git init -q repo git -C repo config user.name "Lab User" git -C repo config user.email "lab@example.com" echo hello > repo/hello.txt git -C repo add hello.txt git -C repo commit -q -m "first commit" `` Run git -C repo log --oneline. It should show one commit named first commit.
Show hint for step 1
git -C DIR runs git as if started in DIR. Paths given to add are relative to that directory.
02
Change a file and commit again
Append a line to hello.txt, then commit with -a so tracked changes are staged in the same step: ``bash echo second >> repo/hello.txt git -C repo commit -q -a -m "second commit" `` Run git -C repo log --oneline. Two commits should appear, the newest first.
Show hint for step 2
git commit -a stages changes to files git already tracks. New files still need git add.
03
Create a branch and list branches
switch -c creates a branch and moves to it. branch --list shows every branch, with an asterisk on the current one. Save the list: ``bash git -C repo switch -q -c feature git -C repo branch --list > branches.txt `` Open branches.txt. The line for the current branch starts with an asterisk and the name feature.
Show hint for step 3
The asterisk marks the checked-out branch. branches.txt is written to /workspace, not into repo.
Check yourself Question 1 of 4
Concept Check The three areas recall

Which three places does git status compare?

Recap & Key Takeaways

A commit is a snapshot with a parent link, and a branch is a name that points at one commit that moves forward on each commit.
The working tree, the index, and the repository are the three places that git status compares.
git -C DIR runs a command in another repository, and repository-level user.name and user.email make commits possible without changing global settings.
git log --oneline, git branch --list, and git diff only read the repository, which makes them safe checks.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue