Everyone has probably experienced the experience of files piling up like "last, last, last.zip" when modifying code without version control. Git is a tool that saves change history as a snapshot, allowing you to go back at any time. GitHub is a platform that puts this history on the Internet and allows multiple people to work together.
Learning Objectives
- You can install Git and set
user.name·user.emailglobally. - You can create a new repository with
git initand leave the first commit. - You can check the storage status with
git status·git log·git show. - Understand the flow of creating a GitHub account and creating a remote repository.
Why Git
Code continues to change over time. Because Git records “what changed, when, and why” on a per-commit basis. It becomes easier to undo mistakes or experiment with two ideas at the same time.
Key concepts
1) Install Git
Windows installation files
| tools | file name | Description | Official Download |
|---|---|---|---|
| Git for Windows | Git-2.52.0-64-bit.exe |
Includes Git command line tools + Git Bash. You must install it to run git commands on Windows. | git-scm.com/install/windows |
| TortoiseGit | TortoiseGit-2.18.0.1-64bit.msi |
A GUI client that integrates Git functionality into Windows Explorer. You can manipulate commits, logs, and diffs with the mouse, which is useful when you are not familiar with the command line. Please use it after installing Git for Windows. | tortoisegit.org/download |
Installation order (Windows): Install in the following order: Git for Windows → TortoiseGit. TortoiseGit uses Git for Windows as its backend, so Git must be installed first.
macOS / Linux
# macOS
brew install git
# Ubuntu / Debian
sudo apt update && sudo apt install git
Check version after installation:
git --version
# git version 2.44.0
2) Global user settings
Sets author information that remains in all commits.
git config --global user.name "Hong Gil-dong"
git config --global user.email "[email protected]"
#OK
git config --list --global
| Options | range |
|---|---|
--global |
All repositories for current user |
--local |
Current repository only (.git/config) |
--system |
System wide |
3) Repository initialization (git init)
mkdir my-project
cd my-project
git init
# Initialized empty Git repository in .../my-project/.git/
Once the .git/ folder is created, Git will start managing this directory.
4) First commit
Working directory → (git add) → staging area → (git commit) → repository
echo "# My Project" > README.md
git add README.md
git commit -m "docs: add README"
5) Three areas
| area | Description |
|---|---|
| Working Directory | Where the actual files are |
| staging area | A temporary space to store changes to commit |
Repository (.git) |
Where commit snapshots are permanently stored |
See as example
Example 1 — ex01_setup.sh: Check Git version and output user settings
#!/usr/bin/env bash
set -euo pipefail
echo "=== Check Git version ==="
git --version
echo ""
echo "=== Current global user settings ==="
git config --global user.name || echo "(user.name not set)"
git config --global user.email || echo "(user.email not set)"
Execution result
=== Check Git version ===
git version 2.44.0
=== Current global user settings ===
Hong Gil-dong
[email protected]
Key point: You can look up the value saved as git config --global at any time.
Example 2 — ex02_init.sh: First commit after repository initialization in temporary directory
#!/usr/bin/env bash
set -euo pipefail
REPO=$(mktemp -d)
echo "Working directory: $REPO"
cd "$REPO"
git init
git config user.name "For practice"
git config user.email "[email protected]"
echo "# Hello Git" > README.md
git add README.md
git commit -m "chore: initial commit"
echo ""
echo "=== Commit list ==="
git log --oneline
Execution result
Working directory: /tmp/tmp.XyZ123
Initialized empty Git repository in /tmp/tmp.XyZ123/.git/
[main (root-commit) a1b2c3d] chore: initial commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
=== Commit list ===
a1b2c3d chore: initial commit
Key point: The three steps git init → git add → git commit are the basic cycle of Git.
Example 3 — ex03_status.sh: Observing working directory, staging, and commit status
#!/usr/bin/env bash
set -euo pipefail
REPO=$(mktemp -d)
cd "$REPO"
git init -q
git config user.name "For practice" && git config user.email "[email protected]"
echo "File 1" > a.txt
echo "[1] Status after adding files"
git status
git add a.txt
echo ""
echo "[2] Status after git add"
git status
git commit -q -m "add a.txt"
echo ""
echo "[3] Status after commit"
git status
Execution result
[1] Status after adding files
On branch main
Untracked files:
a.txt
[2] Status after git add
On branch main
Changes to be committed:
new file: a.txt
[3] Status after commit
On branch main
nothing to commit, working tree clean
Bottom line: git status tells you which of the three areas a file is located in.
Example 4 — ex04_inspect.sh: Check commit log and contents
#!/usr/bin/env bash
set -euo pipefail
REPO=$(mktemp -d)
cd "$REPO"
git init -q
git config user.name "For practice" && git config user.email "[email protected]"
echo "first content" > file.txt
git add file.txt && git commit -q -m "feat: first file"
echo "add second line" >> file.txt
git add file.txt && git commit -q -m "feat: add second line"
echo "=== git log --oneline ==="
git log --oneline
echo ""
echo "=== git show HEAD ==="
git show HEAD
Execution result
=== git log --oneline ===
b2c3d4e feat: add second line
a1b2c3d feat: first file
=== git show HEAD ===
commit b2c3d4e...
Author: For practice <[email protected]>
Date: ...
feat: add second line
diff --git a/file.txt b/file.txt
...
+Add second line
Key point: git log shows a list of commits in reverse order, and git show HEAD shows a diff of the latest commit.
Seeing from a different perspective
| Git concepts | Everyday metaphor |
|---|---|
| storage | photo album |
| commit | One photo |
| staging area | Print waiting tray |
git add |
Upload photos to tray |
git commit |
Paste in photo album |
Common Mistakes
git commit** without **git add— There is nothing in staging, so a “nothing to commit” error occurs.- user.name/email not set — “Please tell me who you are” error occurs when committing.
- Run
git initfrom your home directory — All files will be tracked and.gitwill become huge. - Leave the commit message blank — If run without
-m, the editor opens and the commit is canceled when saving with an empty message. - Confusion between
--globaland--local— There may be a problem with commits being made from a company PC to a personal email.
Cleanup
- Register the author as
git config --global user.name/email. git init→git add→git commitis the basic cycle.- Check the status of the three areas with
git statusand the commit history withgit log. - You can view changes in a specific commit with
git show <commit>.
Try it yourself
cd 01_Getting-started/src
chmod +x ex01_*.sh ex02_*.sh ex03_*.sh ex04_*.sh
./ex01_setup.sh
./ex02_init.sh
./ex03_status.sh
./ex04_inspect.sh
Application:
- Try changing the editor to
git config --global core.editor "vim". - Add the
git log --oneline --graph --alloption to print it in graph form.