Git Commands Cheatsheet
Git commands for commits, branches, undoing changes, and remotes
Your data never leaves the browser — all processing is done locally 0 server requests
Configuration & Setup
-
git config --global user.name "이름"Set the user name recorded in commits
-
git config --global user.email "메일주소"Set the email address recorded in commits
-
git config --global init.defaultBranch mainSet the default branch name to main for new repositories
-
git initInitialize the current directory as a Git repository
-
git clone https://github.com/user/repo.gitClone a remote repository locally
-
git clone --depth 1 URLShallow clone with only the latest commit (faster for large repos)
Basic Workflow — Status, Stage, Commit & Push
-
git statusShow changed files and their staging status
-
git add .Stage all changes in the current directory
-
git add -pInteractively stage changes hunk by hunk
-
git commit -m "메시지"Commit staged changes with a message
-
git commit --amendAmend the last commit or edit its message ※ recommended before pushing only
-
git pushPush commits to the remote repository
-
git push -u origin 브랜치명Push a new branch for the first time and set up tracking
-
git pullFetch and merge remote changes (fetch + merge)
-
git fetch --allFetch remote changes from all remotes without merging
Branches
-
git branch -aList all local and remote branches
-
git switch 브랜치명Switch to a branch (use git checkout for older Git versions)
-
git switch -c 새브랜치Create a new branch and switch to it immediately
-
git branch -d 브랜치명Delete a merged branch (-D to force-delete)
-
git merge 브랜치명Merge the specified branch into the current branch
-
git rebase mainRebase the current branch's commits on top of main
-
git rebase -i HEAD~3Interactively squash or edit the last 3 commits
-
git cherry-pick 커밋해시Apply a specific commit onto the current branch
Viewing History
-
git log --oneline --graph --allView commit history as a compact one-line graph
-
git log -p 파일명Show change history for a specific file with diffs
-
git diffShow unstaged changes in the working directory
-
git diff --stagedShow changes that are staged for commit
-
git show 커밋해시Show the contents of a specific commit
-
git blame 파일명Show who last modified each line of a file and when
Undoing Changes
※ reset --hard and clean can permanently destroy work. Run git status first to review the current state.
-
git restore 파일명Discard working directory changes and restore the last committed state
-
git restore --staged 파일명Unstage a file while keeping the working directory changes
-
git reset --soft HEAD~1Undo the last commit but keep changes staged
-
git reset --hard HEAD~1Discard the last commit and all its changes ※ irreversible
-
git revert 커밋해시Create a new commit that reverses a given commit (safe for shared branches)
-
git stashTemporarily save uncommitted changes and clean the working directory
-
git stash popReapply the most recently stashed changes (use list to view all stashes)
-
git clean -fdDelete untracked files and directories ※ irreversible (use -n for a dry run)
Remotes & Tags
-
git remote -vShow configured remote repository URLs
-
git remote add origin URLAdd a new remote repository
-
git tag -a v1.0.0 -m "릴리즈"Create an annotated tag with a message
-
git push origin --tagsPush all tags to the remote repository
-
git fetch --pruneRemove local references to branches deleted on the remote
Git: The Fundamental Workflow
Git's core workflow is: modify files → git add (stage) → git commit (save snapshot) → git push (share). Understanding that Git tracks snapshots (not differences) and that commits are permanent, content-addressed objects makes the rest of the commands much easier to reason about.
Branching is Git's killer feature. git switch -c new-branch creates and switches to a new branch. Merge with git merge or rebase with git rebase — merge for public branches, rebase for cleaning up local history.
⚠ git reset --hard, git clean -fd, and git push --force discard or overwrite data permanently. For shared branches, prefer git revert to create a safe undo commit.
FAQ
- What is the difference between git merge and git rebase?
- git merge creates a merge commit that joins two branches, preserving the full history. git rebase replays your commits on top of another branch, creating a linear history. Rebase produces a cleaner log but rewrites commit history — only use it on local or private branches, never on shared ones.
- How do I undo the last commit without losing my changes?
- Use git reset --soft HEAD~1. This removes the last commit but keeps the changes staged. If you also want to unstage them, use git reset --mixed HEAD~1 (the default). For completely discarding changes, use --hard, but this is irreversible.
- How do I stash only specific files?
- Use git stash push -m "description" path/to/file. This stashes only the specified file. You can then apply it back with git stash pop or git stash apply stash@{0}.