Git Fundamentals
Git Fundamentals
Section titled “Git Fundamentals”Git is a distributed version control system. Every developer has a full copy of the repository history, and changes are tracked as a series of snapshots (commits).
Key Concepts
Section titled “Key Concepts”| Term | Description |
|---|---|
| Repository | The .git directory that stores all history and metadata |
| Working tree | The files you see and edit |
| Index / Staging area | Changes prepared for the next commit |
| Commit | A snapshot of staged changes with metadata (author, timestamp, message) |
| Branch | A lightweight pointer to a commit |
| HEAD | A pointer to the current branch (or commit in detached HEAD state) |
| Remote | A version of the repository hosted elsewhere (GitHub, GitLab, etc.) |
Configuration
Section titled “Configuration”git config --global user.name "Your Name"git config --global user.email "you@example.com"git config --global core.editor "code --wait" # VS Code as editorgit config --global init.defaultBranch main
# View all configgit config --listStarting a Repository
Section titled “Starting a Repository”# New repogit initgit init my-project # creates directory
# Clone existing repogit clone https://github.com/user/repo.gitgit clone https://github.com/user/repo.git my-folder # custom directory nameThe Three States
Section titled “The Three States”Files move through three states:
Untracked / Modified → Staged → Committed git add ↑ git commit ↑# Check current stategit statusgit status -s # short format
# Stage specific filesgit add file.txtgit add src/ # entire directorygit add . # everything in working directory
# Unstagegit restore --staged file.txt
# Commit staged changesgit commit -m "Add user authentication"
# Stage and commit tracked files in one stepgit commit -am "Fix typo in README"Viewing History
Section titled “Viewing History”git loggit log --oneline # compact viewgit log --oneline --graph # with branch graphgit log --oneline -10 # last 10 commitsgit log --author="Alice"git log --since="2 weeks ago"git log -- path/to/file # commits affecting a specific filegit show abc1234 # show a specific commitBranches
Section titled “Branches”# Create and switchgit branch feature/logingit switch feature/logingit switch -c feature/login # create + switch in one step
# List branchesgit branch # localgit branch -r # remotegit branch -a # all
# Deletegit branch -d feature/login # safe delete (warns if unmerged)git branch -D feature/login # force delete
# Renamegit branch -m old-name new-nameMerging
Section titled “Merging”# Merge feature branch into maingit switch maingit merge feature/login
# Merge with a merge commit (no fast-forward)git merge --no-ff feature/login
# Abort a merge in progressgit merge --abortWorking with Remotes
Section titled “Working with Remotes”# List remotesgit remote -v
# Add a remotegit remote add origin https://github.com/user/repo.git
# Pushgit push origin maingit push -u origin main # set upstream (then use just `git push`)git push --all # push all branches
# Pull (fetch + merge)git pull
# Fetch (download without merging)git fetch origin
# Delete remote branchgit push origin --delete feature/old-branchViewing Differences
Section titled “Viewing Differences”# Changes not yet stagedgit diff
# Staged changes (about to be committed)git diff --stagedgit diff --cached # same thing
# Between two commitsgit diff abc1234 def5678
# Between branchesgit diff main feature/new.gitignore
Section titled “.gitignore”# Dependenciesnode_modules/.venv/
# Build outputdist/build/*.pyc
# Secrets.env.env.local*.pemsecrets.json
# IDE.vscode/.idea/*.swpGenerate recommended .gitignore files at gitignore.io.