Git
Setting Up
- Initialize an existing directory as a Git repository
git init
- Clone a specified remote repository
git clone <remote repository url>
- Specifies the remote repository for your local repository.
git remote add origin <remote repository url>
Staging
- Show modified files in working directory, and files staged for your next commit
git status
- Add a file as it looks now to your next commit
git add <file 1> ... <file N>
- Unstage a file while retaining the changes in working directory
git restore --staged <file 1> ... <file N>
Committing
- Commit the staged content as a new commit snapshot
git commit
Note
Scoped commit message style:
<scope>: <what changed>
<why the change was made or extra context>
<Key: value>
- Modify the previous commit's message
git commit --amend -m "<new commit message>"
Branching
- Display local branches
git branch
- Display remote branches
git branch -r
- Create a branch
git branch <branch>
- Rename a branch
git branch -m <branch to rename> <new branch name>
- Switch to a branch
git switch <branch>
- Delete a branch
git branch -d <branch>
- Delete a branch with unmerged changes
git branch -D <branch>
- Delete a remote branch
git push origin --delete <remote branch>
Inspecting & Comparing
- Show the commit history for the current branch
git log
- Show a condensed commit history as a graph, including all branches
git log --oneline --graph --all
- Show unstaged changes in the working directory
git diff
- Show staged changes (what will go into the next commit)
git diff --staged
- Show the difference between two branches or commits
git diff <branch or commit A> <branch or commit B>
- Show who last modified each line of a file, and in which commit
git blame <file>
- Show the details and changes of a specific commit
git show <commit>
- Move the
HEADpointer to a commit
git switch --detach <commit> or HEAD<relative reference>
Note
Checking out a commit causes the HEAD pointer to be in a detached head state, where any commits won't belong to any branches, and will be lost when you switch away (unless you run git switch -c <new branch> first to keep them).
Merging & Rebasing
- Merge a branch into the current branch
git merge <from branch>
- Replay the current branch's commits on top of a target branch
git rebase <from branch>
Note
Squashing is done inside an interactive rebase: mark commits with squash (or s) to combine them into the commit above.
- Abort a merge or rebase that has conflicts and return to the previous state
git merge --abort
git rebase --abort
Note
When to use rebase vs merge:
- Updating your private stuff from upstream: rebase
- Landing your stuff into shared branches: merge
Stashing
- Save all staged and unstaged changes
git stash
- List stack-order of stashed file changes
git stash list
- Show a summary of the files changed in the most recent stash
git stash show stash@{<index>}
- Write the most recent stash back to working copy and removes it from the top of the stash stack
git stash pop
- Write the most recent stash back to working copy and does not remove it from the top of the stash stack
git stash apply
- Discard the changes from top of stash stack
git stash drop
Undoing Things
- Move the current branch back to a commit, keeping changes staged (
--soft), unstaged (--mixed) — the default — or discarding them entirely (--hard)
git reset [--soft | --mixed | --hard] <commit>
- Create a new commit that reverses the changes of a previous commit
git revert <commit>
Syncing
- Download commits and branches from the remote without changing your working directory
git fetch <remote>
- Fetch from the remote and rebase the current branch's local commits on top of the remote branch, instead of merging
git pull --rebase
Pushing
- Push a branch to the remote
git push <remote> <branch>
- Push a branch that you've never pushed before to the remote
git push -u origin <branch>