Skip to content

Commit guidelines

nivekyee edited this page Apr 24, 2021 · 1 revision

When to commit

Commits should be logical, atomic units of change that represent a specific idea. So, theoretically, it should be done for each solution for a problem.

For commit message guidelines, see link.

How to undo commits

  • Undoing a commit that exists on the remote (force push) could create big problems for your collaborators
  • Undoing a commit on work that you only have locally is much safer

What can go wrong on a force push?

  • Worse case: Drastic changes can cause Git to no longer recognise the projects as related.
  • Common case: The "deleted" commit could be "undeleted" without any notification (could be a password, token, or large binary file).

git revert

git revert is the safest way to change history with Git. Instead of deleting existing commits, git revert looks at the changes introduced in a specific commit, then applies the inverse of those changes in a new commit. It functions as an "undo commit" command, without sacrificing the integrity of your repository's history. git revert is always the recommended way to change history when it's possible.

git reset

Moves the HEAD pointer and the branch pointer to another point in time, making it seem like the commits in between never happened.

  • Maybe cause loss of work
  • Useful when a commit includes sensitive information that must to be deleted
  • Before git reset:
    • Make sure to talk with your team about any shared commits
    • Research the three types of reset to see which is right for you (--soft, --mixed, --hard)
    • Commit any work that you don't want to be lost intentionally - work that is committed can be gotten back, but uncommitted work cannot

git reflog

A log of every commit that HEAD has pointed to. Could be use to find and access commits that are unintentionally lost from performing a git reset.

Updating commits with commit amend

git commit --amend only changes the most recent commit on your current branch. Useful for commits that:

  • Haven't been pushed to the remote yet
  • Have a spelling error in the commit message
  • Don't contain the changes that you'd like to contain

Clone this wiki locally