Git Beginner's Guide | Install, Commit, Branch & Remote in One Go

Git Beginner's Guide | Install, Commit, Branch & Remote in One Go

이 글의 핵심

Git is the world's most widely used version control system. This guide walks you through everything you need to get started: installation, commits, branches, and pushing to GitHub.

What is Git?

Git is the world’s most widely used version control system (VCS) — a tool that tracks every change to your files, lets you revert to any previous state, and enables many people to collaborate on the same codebase.

Without Git:          With Git:
project_v1.zip        git log → full history
project_v2.zip        git checkout → any version
project_FINAL.zip     git branch → parallel work
project_FINAL2.zip    git merge → combine work

This guide covers the core workflow. By the end you will be able to:

  1. Install and configure Git
  2. Stage and commit changes
  3. Create and switch branches
  4. Push to and pull from GitHub

Installation

macOS

# Option 1: Homebrew (recommended)
brew install git

# Option 2: Xcode Command Line Tools
xcode-select --install

Windows

Download the installer from git-scm.com and follow the wizard. Use Git Bash for all commands in this guide.

Linux (Ubuntu / Debian)

sudo apt update && sudo apt install git

Verify

git --version
# git version 2.44.0

Initial Configuration

Run these once after installation:

Run these Git commands:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Set default branch name to 'main'
git config --global init.defaultBranch main

# Use VS Code as the default editor
git config --global core.editor "code --wait"

# Better diff output
git config --global core.autocrlf input    # macOS/Linux
# git config --global core.autocrlf true   # Windows

Check your settings:

git config --list

Core Concepts

Working Directory  →  Staging Area  →  Repository
    (your files)       (git add)       (git commit)

                                      Remote (git push)
TermWhat it is
Repository (repo)The .git folder that stores all history
Working directoryThe files you see and edit
Staging area (index)A “draft” of your next commit
CommitA snapshot of your staged changes with a message
BranchA movable pointer to a sequence of commits
RemoteA copy of the repo on another machine (e.g. GitHub)

Basic Workflow

1. Initialize a Repository

mkdir my-project && cd my-project
git init
# → Initialized empty Git repository in .../my-project/.git/

Or clone an existing repo:

git clone https://github.com/user/repo.git
cd repo

2. Check Status

git status

Always run git status before staging or committing — it shows:

  • Untracked files (new files Git doesn’t know about yet)
  • Modified files (changed since last commit)
  • Staged files (ready to commit)

3. Stage Changes

Run the following commands:

# Stage a specific file
git add README.md

# Stage multiple files
git add src/main.py tests/test_main.py

# Stage all changes in the current directory
git add .

# Interactive staging (choose hunks)
git add -p

4. Commit

git commit -m "Add README with project overview"

Good commit message formula:

<type>: <short summary> (50 chars max)

<optional body — explain WHY, not what>

Types: feat, fix, docs, refactor, test, chore

# Examples
git commit -m "feat: add user registration endpoint"
git commit -m "fix: prevent duplicate email on signup"
git commit -m "docs: update API authentication section"

5. View History

Run the following commands:

# Full log
git log

# One line per commit
git log --oneline

# With graph (useful for branches)
git log --oneline --graph --all

# Show changes in last 3 commits
git log -p -3

Branching

Branches let you work on features or fixes without touching the main codebase.

Run the following commands:

# Create and switch to a new branch
git checkout -b feat/user-profile

# Modern syntax (Git 2.23+)
git switch -c feat/user-profile

# List all branches
git branch -a

# Switch to an existing branch
git switch main

# Delete a merged branch
git branch -d feat/user-profile

# Force delete (even if unmerged)
git branch -D feat/user-profile

Typical Feature Branch Workflow

Run the following commands:

# 1. Start from an up-to-date main
git switch main
git pull

# 2. Create a feature branch
git switch -c feat/42-add-search

# 3. Make changes and commit
git add src/search.py
git commit -m "feat: add full-text search with SQLite FTS5"

# 4. Merge back into main
git switch main
git merge feat/42-add-search

# 5. Clean up
git branch -d feat/42-add-search

Merging & Resolving Conflicts

git merge feat/42-add-search

If Git can merge automatically, it creates a merge commit. If two branches changed the same lines, you get a conflict:

<<<<<<< HEAD (main)
def search(query):
    return []
=======
def search(query, limit=10):
    return db.query(query, limit)
>>>>>>> feat/42-add-search

To resolve:

  1. Edit the file — keep what you want, delete the markers
  2. git add <file>
  3. git commit

VS Code makes this easier

Open the conflicted file in VS Code and use the “Accept Current / Accept Incoming / Accept Both” buttons in the diff view.


Undoing Changes

Run the following commands:

# Unstage a file (keep changes in working directory)
git restore --staged README.md

# Discard changes in working directory (IRREVERSIBLE)
git restore README.md

# Undo the last commit, keep changes staged
git reset --soft HEAD~1

# Undo the last commit, keep changes in working directory
git reset --mixed HEAD~1

# Create a new commit that reverses a previous commit (safe for shared branches)
git revert HEAD

# Show what changed in the last commit
git show HEAD

Remote Repositories (GitHub)

Push an Existing Repo to GitHub

Run the following commands:

# 1. Create a repo on github.com (no README, no .gitignore)

# 2. Connect your local repo
git remote add origin https://github.com/you/my-project.git

# 3. Push and set upstream
git push -u origin main

# After the first push, just:
git push

Pull Updates

Run the following commands:

# Fetch + merge in one step
git pull

# Fetch only (inspect before merging)
git fetch
git log origin/main  # see what's new
git merge origin/main

.gitignore

Create a .gitignore file at your repo root to exclude files from tracking:

Run the following commands:

# Python
__pycache__/
*.pyc
.env
venv/
.venv/

# Node
node_modules/
dist/
.next/

# Editors
.DS_Store
.idea/
.vscode/settings.json

# Build artifacts
*.log
*.tmp

Common Troubleshooting

I committed to the wrong branch

# Move last commit to a new branch
git branch feat/new-branch   # create branch pointing at current commit
git reset --hard HEAD~1      # remove commit from current branch
git switch feat/new-branch   # go to the new branch

I want to change the last commit message

git commit --amend -m "New message"
# ⚠️ Only do this if you haven't pushed yet

I accidentally deleted a file

git checkout HEAD -- deleted-file.txt

I need to temporarily set aside work

Run the following commands:

# Stash uncommitted changes
git stash

# Get them back
git stash pop

# List stashes
git stash list

Essential Command Reference

CommandWhat it does
git initInitialize a new repository
git clone <url>Clone a remote repository
git statusShow working tree status
git add <file>Stage a file
git commit -m "msg"Create a commit
git log --onelineShow compact commit history
git branch -b <name>Create + switch to branch
git merge <branch>Merge a branch into current
git pushPush to remote
git pullPull from remote
git stashTemporarily shelve changes
git revert HEADSafely undo last commit

Next Steps

  • Branching strategies: Learn Git Flow or GitHub Flow for team collaboration
  • Rebase: git rebase for a cleaner history
  • GitHub Actions: Automate CI/CD on push or pull request

Related posts: