# Git — Distributed Version Control for Your Source Code

> Practical guide to Git — configuration, commits, branching, merge, rebase, remotes, stash and history on the command line, with examples.

Source: https://www.jpkc.com/db/en/cheatsheets/version-control/git/

<!-- PROSE:intro -->
Git is the distributed version control system that shapes modern software development: every clone of a repository carries the complete history and works fully offline. Rather than storing individual differences, Git records snapshots of your entire project and links them into a traceable history. Branches let you try out ideas without risk, while merge and rebase bring them back together. This guide walks you through the commands you reach for daily – from initial configuration through commits and branching to remotes, stash and history.
<!-- PROSE:intro:end -->

## Configuration

`git config --global user.name "<name>"` — Set your global username for all repositories.

```bash
git config --global user.name "John Doe"
```

`git config --global user.email "<email>"` — Set your global email for all repositories.

```bash
git config --global user.email "john@example.com"
```

`git config --list` — List all configuration settings.

`git config --global core.editor "<editor>"` — Set the default text editor for commit messages.

```bash
git config --global core.editor "vim"
```

`git config --global init.defaultBranch <name>` — Set the default branch name for new repositories.

```bash
git config --global init.defaultBranch main
```

## Repository Basics

`git init` — Initialize a new Git repository in the current directory.

`git clone <url>` — Clone a remote repository to your local machine.

```bash
git clone https://github.com/user/repo.git
```

`git clone <url> <directory>` — Clone a repository into a specific directory.

```bash
git clone https://github.com/user/repo.git my-project
```

`git clone --depth <n> <url>` — Create a shallow clone with limited commit history.

```bash
git clone --depth 1 https://github.com/user/repo.git
```

`git status` — Show the working tree status including staged, unstaged, and untracked files.

## Staging & Commits

`git add <file>` — Stage a specific file for the next commit.

```bash
git add index.html
```

`git add .` — Stage all changes in the current directory and subdirectories.

`git add -p` — Interactively stage parts of files (hunks).

`git commit -m "<message>"` — Create a commit with a message.

```bash
git commit -m "feat: add login page"
```

`git commit --amend` — Modify the most recent commit (message or content).

`git commit --amend --no-edit` — Add staged changes to the last commit without changing the message.

`git reset HEAD <file>` — Unstage a file while keeping changes in the working directory.

```bash
git reset HEAD index.html
```

## Branching

`git branch` — List all local branches. The current branch is marked with an asterisk.

`git branch -a` — List all branches including remote-tracking branches.

`git branch <name>` — Create a new branch without switching to it.

```bash
git branch feature/login
```

`git checkout <branch>` — Switch to an existing branch.

```bash
git checkout main
```

`git checkout -b <branch>` — Create a new branch and switch to it immediately.

```bash
git checkout -b feature/login
```

`git switch <branch>` — Switch to an existing branch (modern alternative to checkout).

```bash
git switch main
```

`git switch -c <branch>` — Create and switch to a new branch.

```bash
git switch -c feature/login
```

`git branch -d <branch>` — Delete a branch that has been fully merged.

```bash
git branch -d feature/login
```

`git branch -D <branch>` — Force delete a branch even if it has unmerged changes.

```bash
git branch -D feature/login
```

## Merging & Rebasing

`git merge <branch>` — Merge the specified branch into the current branch.

```bash
git merge feature/login
```

`git merge --no-ff <branch>` — Merge with a merge commit even if fast-forward is possible.

```bash
git merge --no-ff feature/login
```

`git merge --abort` — Abort the current merge and restore the pre-merge state.

`git rebase <branch>` — Rebase the current branch onto the specified branch.

```bash
git rebase main
```

`git rebase --abort` — Abort a rebase in progress and return to the original state.

`git cherry-pick <commit>` — Apply a specific commit from another branch.

```bash
git cherry-pick a1b2c3d
```

## Remote Repositories

`git remote -v` — List all remote repositories with their URLs.

`git remote add <name> <url>` — Add a new remote repository.

```bash
git remote add origin https://github.com/user/repo.git
```

`git fetch <remote>` — Download objects and refs from a remote without merging.

```bash
git fetch origin
```

`git pull` — Fetch and merge changes from the remote tracking branch.

`git pull --rebase` — Fetch and rebase local commits on top of remote changes.

`git push <remote> <branch>` — Push local commits to a remote branch.

```bash
git push origin main
```

`git push -u <remote> <branch>` — Push and set the upstream tracking branch.

```bash
git push -u origin feature/login
```

`git push --tags` — Push all local tags to the remote repository.

## Stash

`git stash` — Temporarily save uncommitted changes and clean the working directory.

`git stash push -m "<message>"` — Stash changes with a descriptive message.

```bash
git stash push -m "WIP: login form"
```

`git stash list` — List all stashed entries.

`git stash pop` — Apply the most recent stash and remove it from the stash list.

`git stash apply stash@{<n>}` — Apply a specific stash without removing it.

```bash
git stash apply stash@{0}
```

`git stash drop stash@{<n>}` — Remove a specific stash entry.

```bash
git stash drop stash@{0}
```

`git stash clear` — Remove all stash entries.

## Log & History

`git log` — Show the commit history for the current branch.

`git log --oneline` — Show commit history in compact one-line format.

`git log --graph --oneline --all` — Show a visual ASCII graph of all branches.

`git log -n <count>` — Show only the last N commits.

```bash
git log -n 5
```

`git log --author="<name>"` — Filter commits by author.

```bash
git log --author="John"
```

`git show <commit>` — Show details and diff of a specific commit.

```bash
git show a1b2c3d
```

`git blame <file>` — Show who last modified each line of a file.

```bash
git blame index.html
```

## Diff & Comparison

`git diff` — Show unstaged changes in the working directory.

`git diff --staged` — Show changes that are staged for the next commit.

`git diff <branch1> <branch2>` — Compare two branches.

```bash
git diff main feature/login
```

`git diff <commit1> <commit2>` — Compare two specific commits.

```bash
git diff a1b2c3d e4f5g6h
```

## Undoing Changes

`git checkout -- <file>` — Discard changes in a file and restore it to the last commit.

```bash
git checkout -- index.html
```

`git restore <file>` — Discard changes in the working directory (modern alternative).

```bash
git restore index.html
```

`git restore --staged <file>` — Unstage a file while keeping changes.

```bash
git restore --staged index.html
```

`git revert <commit>` — Create a new commit that undoes the specified commit.

```bash
git revert a1b2c3d
```

`git reset --soft HEAD~<n>` — Undo last N commits, keeping changes staged.

```bash
git reset --soft HEAD~1
```

`git reset --hard HEAD~<n>` — Undo last N commits and discard all changes. Use with caution!

```bash
git reset --hard HEAD~1
```

`git clean -fd` — Remove untracked files and directories from the working tree. Irreversible – preview with `git clean -nfd` (dry run) first!

## Tags

`git tag` — List all tags in the repository.

`git tag <name>` — Create a lightweight tag at the current commit.

```bash
git tag v1.0.0
```

`git tag -a <name> -m "<message>"` — Create an annotated tag with a message.

```bash
git tag -a v1.0.0 -m "Release version 1.0.0"
```

`git tag -d <name>` — Delete a local tag.

```bash
git tag -d v1.0.0
```

`git push origin --delete <tagname>` — Delete a remote tag.

```bash
git push origin --delete v1.0.0
```

<!-- PROSE:outro -->
## Conclusion

Git looks overwhelming at first, but daily work rests on a manageable handful of commands: add, commit, push, pull, branch and merge. Add rebase, stash and undoing changes with reset and revert, and you stay in control of your history even on large projects. Adopt meaningful commit messages and a clear branching strategy – that turns Git from a tool into a reliable safety net.

## Further Reading

- [Pro Git – the official book](https://git-scm.com/book/en/v2) – the comprehensive standard reference, free online
- [Git – official documentation](https://git-scm.com/doc) – reference and manual pages
- [Git – Wikipedia](https://en.wikipedia.org/wiki/Git) – background and history
<!-- PROSE:outro:end -->

## Related Commands

- [gh](https://www.jpkc.com/db/en/cheatsheets/version-control/gh/) – GitHub workflows straight from the command line

