David Härer / Cheatsheets

Git Cheatsheet



How do I create an empty commit?

git commit --allow-empty -m "COMMIT MESSAGE"

How do I work with subtrees?

# Add a subtree.
git subtree add --prefix path/in/your/repo REMOTE_URL BRANCH --squash

# Pull a subtree.
git subtree pull --prefix path/in/your/repo REMOTE_URL BRANCH --squash

# Push a subtree.
git subtree push --prefix path/in/your/repo REMOTE_URL BRANCH

How do I checkout files from another branch?

git checkout BRANCH -- FILE_PATH

What is forking in Git?

How can I change a commit author?

git rebase -i -p COMMIT_HASH
# Mark the commit you want to change with `edit`.
git commit --amend --author="John Doe <john.doe@example.com>" --no-edit
git rebase --continue

How do I squash all commits?

Run git rebase --root -i and change pick to squash for all commits except the first one.