Skip to content

Helpful Git Commands

Dylan Barkowsky edited this page Sep 12, 2024 · 1 revision

This section covers some advanced use cases with Git.

⚡ These commands shouldn't be part of your daily development workflow, unless things are going badly with your repo.

How to undo the last commit

First, before we bring the big guns in, let's make sure you really need them. Because in case you just want to edit your last commit, you can simply use Git's amend feature. It allows you to correct the last commit's message as well as add more changes to it. If that's what you want to do, read more about amend.

There's two flavours for "undoing" things in git:

# soft reset
git reset --soft HEAD~1

# hard reset - careful!
git reset --hard HEAD~1

Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.

If you don't want to keep these changes, simply use the --hard flag. This tells Git to replace the files in your working copy with the "HEAD - 1" revision (which is the last committed version), discarding all local changes.

⚡ Be sure to only do this when you're sure you don't need these changes anymore!

How to undo MULTIPLE commits

The same technique allows you to return to any previous revision:

# Find the commit id (SHA) you want to revert to, then
git reset --hard 0ad5a7a6 # <-- the commit SHA

# You will need to "force" push your changes back to GitHub
git push -f

⚡ Always keep in mind, however, that using the reset command undoes all commits that came after the one you returned to:

reset

Reverting a commit

Sometimes you'll want to undo a certain commit. E.g. when you notice that your changes were wrong, when you introduced a bug, or simply when the customer has decided he doesn't want this anymore.

Using the "git revert" command is one possibility to undo a previous commit. However, the command doesn't delete any commits. Instead, it reverts the effects of a certain commit, effectively undoing it. It does this by producing a new commit with changes that revert each of the changes in that unwanted commit. For example, if your original commit added a word in a certain place, the reverting commit will remove exactly this word, again.

revert

In case you wish to revert a commit, use the following instructions.

  • Create a branch and sync it with upstream.

    # create a branch
    git checkout -b myrevert
    
    # sync the branch with upstream
    git fetch origin
    git rebase origin/main
  • If the commit you wish to revert is a:

    • merge commit:

      # SHA is the hash of the merge commit you wish to revert
      git revert -m 1 SHA
    • single commit:

      # SHA is the hash of the single commit you wish to revert
      git revert SHA
  • This will create a new commit reverting the changes. Push this new commit to your remote.

git push origin myrevert

Attribution

This page heavily borrowed from the kubernetes.io project.

Another useful site for git-based mistakes: DangItGit

Clone this wiki locally