How to change your commit messages in Git

There are a few times when you need to amend your commits in git.

git commit —amend

git commit --amend will allow the modification of the last commit message.

This is the current log in our example.

$ git log --oneline
d09ef8b4 (HEAD -> refactor/655-some-refactoring, origin/refactor/655-some-refactoring) Did some stuff
dd3d303e Some feature spec should now use the new refactored code
cc5ec6b4 Something filter is added to the location search

Let’s change the commit message from Did some stuff to Added tests and removed unneeded method

$ git commit --amend
$ Did some stuff
 
 # Please enter the commit message for your changes. Lines starting
 # with '#' will be ignored, and an empty message aborts the commit.

Modify your messages to reflect your updated message.

Added tests and removed unneeded method
 
 # Please enter the commit message for your changes. Lines starting
 # with '#' will be ignored, and an empty message aborts the commit.

Save.

$ git commit --amend
[refactor/655-some-refactoring a380ce8c] Added tests and removed unneeded method
 Date: Sun Aug 19 19:11:14 2017 +0800
 3 files changed, 7 insertions(+), 19 deletions(-)
git push origin refactor/655-some-refactoring --force

You might want to check with your teammates because you will be overwriting the branch history.

 Git Cheetsheet

This is a cheatsheet for Git beginners.

git init

git init creates a new local repository

$ git init
Initialized empty Git repository in /Users/a_user/projects/testproject/.git/

git clone path/to/repository

git clone creates a local repository from a remote repository

git checkout —

git checkout --<filename> replace file with latest version on HEAD

git add filename

git add <filename> stages files for a commit

git add .

git add . stages all files for a commit

git commit -m “message”

git commit -m commits files that have been staged

git commit -a

git commit -a commits all new files and have been changed since last commit

git push origin master

git push origin master pushes all commits to the remote repository with the master branch

git remove -v

git remove -v lists all the remote repositories currently configured on the local repository

$ git remote -v 
origin	git@github.com:awesomeuser/myproject.git (fetch)
origin	git@github.com:ausomeuser/myproject.git (push)

 Squash or Merge commits in Git

Squashing is a really simple way to merge some commits.

$ git log
commit 4bd83a06ad4609eaafafc093f96849f17060593c (HEAD -> refactor/some-refactoring, origin/refactor/some-refactoring)
Author: My Name <48761+thatuser@users.noreply.github.com>
Date:   Thu Sep 26 17:25:08 2019 +0800

    Another initial commit message

commit b8d1997e4ee132075b49f958a38eeebc013d094d
Author: My Name <48761+thatuser@users.noreply.github.com>
Date:   Wed Sep 25 10:05:04 2019 +0800

    Initial commit for Terms

commit aef327c33d0521f6e1a19d3a5eca2190509f471b
Author: Kris <kris@memmee.eu>
Date:   Tue Sep 24 12:04:34 2019 +0000

    Some really important stuff

commit d6d856c32fbed3bb6bbc03f3879352e4c9629ca0
Merge: 2e6e7075 0e3a91e1
Author: My Coworker <mycoworkers@mailmail.com>
Date:   Mon Sep 23 15:48:01 2019 +0800

    Merge pull request #1002 from user/feature/refactor-some-task
    
    The really awesome commit message

The last two commits should probably merged. There won’t be two intial commits

git rebase -i HEAD~[N]

$ git rebase -i HEAD~4

This command would result in the following:

pick d94e78 Another initial commit message
pick 4e9baa Initial commit for Terms
pick afb581  Some really important stuff
pick 643d0e Merge pull request #1002 from user/feature/refactor-some-task

Change this to the following:

pick d94e78 Another initial commit message
squash 4e9baa Initial commit for Terms
pick afb581  Some really important stuff
pick 643d0e Merge pull request #1002 from user/feature/refactor-some-task

This will result in merging the last two commits:

pick d94e78 Another initial commit message
pick afb581  Some really important stuff
pick 643d0e Merge pull request #1002 from user/feature/refactor-some-task

 Undo Local changes in Git

git reset —hard

git reset --hard allows you to remove all changes to local files. This is final.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   vue-cart/index.html

no changes added to commit (use "git add" and/or "git commit -a")
$ git reset --hard
HEAD is now at b25f597 Creating a simple Cart
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

git reset HEAD

git reset HEAD <file> allows you reset changes to the last commit. This is final.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   vue-cart/main.js

no changes added to commit (use "git add" and/or "git commit -a")
$ git reset HEAD vue-cart/main.js 
Unstaged changes after reset:
M	vue-cart/main.js