16 January, 2022

Git commands that I usually forget.

Forgettable Git

There are many git commands that I use regularly, but not so often that I can actually remember them so I’ve listed them here. I suspect that this list will probably grow over time.

Change an older commit message

  • Run the following to show the last n commits in your editor.
$ git rebase -i HEAD~n
  • Find the commits whose messages you want to change, and replace the command pick with reword.
  • Save and exit.

The editor will now open for each commit in turn.

  • Change the message, then save and exit.

If you need to push it, use the --force flag.

Credit: https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/changing-a-commit-message

Create an empty branch with no history

This is particularly useful when you need to create gh-pages

$ git switch --orphan <branch_name>

Credit: https://stackoverflow.com/questions/34100048/create-empty-branch-on-github

Delete a remote branch

$ git push -d <remote_name> <branch_name>

Credit: https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely