Grokking the Behavioral Interview
Get Educative’s popular interview prep course for free.
Commits to Git are accompanied with a commit message that explains what changes the commit has made to the code. However, a situation may arise where the commit message will need to be changed after it has been pushed.
If the message to be changed is for the latest commit to the repository, then the following commands are to be executed:
git commit --amend -m "New message"
git push --force repository-name branch-name
Note that using
--force
is not recommended unless you are absolutely sure that no one else has cloned your repository after the latest commit.
A safer alternative is to use:
git push --force-with-lease repository-name branch-name
Unlike --force
, which will destroy any changes someone else has pushed to the branch, --force-with-lease
will abort if there was an upstream change to the repository.
If the message needs to be amended for an older commit, then the interactive rebase tool can be used:
git rebase -i HEAD~n
command to display a list of the last commits in your default text editor. For example, the following command would display a list of the last three commits in your current branch:git rebase -i HEAD~3
The list would be similiar to this:
pick e499d89 Delete CNAME
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.
# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re- ordered; they are executed from top to
bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
pick
with reword
before each commit message that needs to be changed:pick e499d89 Delete CNAME
reword 0c39034 Better README
reword f7fde4a Change the commit message but push the same commit.
git push --force
.RELATED TAGS