Skip to content

Latest commit

 

History

History
executable file
·
206 lines (139 loc) · 2.67 KB

File metadata and controls

executable file
·
206 lines (139 loc) · 2.67 KB

Git

Discard local changes

Discard unstaged changes in one file:

git restore path/to/file

Discard unstaged changes in all tracked files:

git restore .

Unstage files without touching the working tree:

git restore --staged path/to/file
git restore --staged .

Remove untracked files:

git clean -nd   # preview
git clean -fd   # delete files and dirs

Add changes

Add one file:

git add path/to/file

Add everything:

git add -A

Stage selected hunks:

git add -p

Branches

List branches:

git branch
git branch -r
git branch -a

Create and switch to a new branch:

git switch -c my-branch

Create a local branch that tracks a remote branch:

git switch --track origin/my-branch

History

Show a compact graph:

git log --oneline --graph --decorate --all

Show one commit:

git show <commit>

Undo the last local commit

Keep changes staged:

git reset --soft HEAD~1

Keep changes unstaged:

git reset --mixed HEAD~1

Rebase local branch on latest main

Replace main if your default branch is different.

git fetch origin
git switch my-branch
git rebase origin/main

If there are conflicts:

git add <resolved-file>
git rebase --continue
git rebase --abort

Squash recent commits

Squash the last n commits:

git rebase -i HEAD~n

In the editor keep the first commit as pick and change the rest to squash or fixup.

If the branch was already pushed:

git push --force-with-lease

Restore a deleted file or directory

Restore from HEAD:

git restore path/to/file-or-dir

Restore from another commit:

git restore --source <commit> path/to/file-or-dir

Recover a lost commit

Check the reflog:

git reflog

Create a branch from the old commit:

git branch recovered <commit>
git switch recovered

Edit the last commit

Change the message:

git commit --amend

Change files in the last commit:

# edit files
git add -p              # or: git add -A
git commit --amend
git push --force-with-lease

Pre-commit hooks

Install pre-commit:

python3 -m venv .venv
. .venv/bin/activate
pip install pre-commit
pre-commit install

The config file is .pre-commit-config.yaml. Docs: https://pre-commit.com/

Remove secrets from history

Rotate the secret first.

Rewrite history with git-filter-repo:

pip install git-filter-repo
printf 'old-secret==>REMOVED\n' > replacements.txt
git filter-repo --replace-text replacements.txt
git push --force --all
git push --force --tags