Git

Git Cheat Sheet

Basic Commands

Scenarios

The following are some common scenarios I've run into.

Breaking up a big feature branch

Let's say we have a feature branch that has grown too large and we want to break it up into smaller, more manageable pieces. Here's how we can do that:

  1. Create a new branch from the feature branch.
  2. Use git rebase -i to interactively rebase the new branch.
  3. Squash commits together to create a single commit for each new feature.
  4. Push the new branch to the remote repository.

Alternatively, we can use cherry picking to transfer individual commits to new branches.

For example, to move the last three commits from feature to feature-part-1:

git checkout feature-part-1
git cherry-pick feature~2
git cherry-pick feature~1
git cherry-pick feature

Undoing a commit

If we need to undo a commit, we can use git reset to move the HEAD pointer to a previous commit. There are three options for resetting:

For example, to undo the last commit:

git reset --soft HEAD~1

Tagging a release

When we're ready to tag a release, we can use git tag to create a new tag. An annotated tag includes a message, while a lightweight tag does not.

For example, to create an annotated tag for version 1.0:

git tag -a v1.0 -m "Version 1.0"