You can use git commit –amend option to update/modify the last commit message.
Example:
Create a new local git repository:
1 2 |
## Create a new git repository git init myrepo && cd myrepo |
Create couple of commits in your local git repository:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## Create first commit echo "cat" > file1 git add file1 git commit -m "commit1" ## Create second commit echo "dog" > file2 git add . git commit -m "wrong_commit_message" ## Check the commit messages git log --pretty=oneline ## sha2 (HEAD -> master) wrong_commit_message ## sha1 commit1 |
Update the last git commit message in local git repository:
1 2 3 4 5 6 7 |
## Update last local commit message git commit --amend -m "commit2" ## Check the commit messages git log --pretty=oneline ## sha2 (HEAD -> master) commit2 ## sha1 commit1 |
Create a new empty remote repository and add to your local repository:
1 2 3 4 5 6 7 8 |
## Create an empty remote git repository (i m using GitHub for this demo) ## Add your username and email to git config (optional) git config --add --local user.name cloudaffaire git config --add --local user.email cloudaffaire@gmail.com ## Add remote repository to your local repository git remote add origin git@github.com:CloudAffaire/myrepo.git git branch -M main git push -u origin main |
Create a new commit to your local repository and push it to the remote repository:
1 2 3 4 5 |
## Create a third commit and push to remote repository echo "cow" > file3 git add . git commit -m "again_wrong_commit_message" git push |
Update last commit message in a remote git repository:
1 2 3 4 5 6 |
## Update the last commit message in local git commit --amend -m "commit3" ## Update the last commit message in remote git push --force origin main |