Table of Contents
1. Context
After committing a change, you realized that either the author is not appearing correctly or the commit message needs a change. There are two likely scenarios and in each scenario, we’ll explore how to change the commit metadata:
- You haven’t pushed the commit yet
- You already have pushed the commit to the remote branch
2. Commit Is Not Yet Pushed
If you have not pushed the commit to the remote branch yet, do not worry. Use amend
option to change the last commit. You may include or exclude files of the last commit or just change the metadata such as author, message, etc. If you want to include additional files, ensure to stage the files before amending.
# Change author and commit message git commit --amend --author="Author Name <[email protected]>"
It opens up an editor where you can change the metadata such as author name, author email, commit message, etc.
After your change, save and close the editor. In order to confirm whether Git
applied your changes correctly, execute the following commands:
# Lists past commits. Check the metadata of the topmost one. git log # Copy the commit id from the topmost one from git log to see the changes git show <commit_id>>
3. Commit Is Already Pushed
In case you have already pushed the commit to the remote branch, still do not worry. You can still change the commit metadata using an interactive rebase. Explore the commit history of the remote branch and note down the previous commit id of the target commit, for which you are going to change the metadata. Use the rebase
command in interactive mode supplying the previous commit id.
# Rebase in interactive mode. Id of the previous commit git rebase -i <commit_id>
It opens an editor listing all subsequent commits with a set of commands, like below:
pick e9b09e1 Commit description pick ef509e1 Commit description # Rebase ef507a8..e9b09e1 onto ef507a8 (1 command) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # d, drop <commit> = remove commit ............
Change the pick
command with the desired action, save and close the editor. If you want to change multiple commits, for each commit, change the command appropriately. For amending a commit, use the edit
command, for dropping a commit, use drop
command, for changing just the commit message, use reword
command, and so on. Git lists all the commands at the bottom with descriptions.
Execute with amend
options to change metadata such as author name, author email id etc. and then continue
the rebase
operation.
# Change author and commit message git commit --amend --author="Author Name <[email protected]>" # Continue the rebase, amend the next commit and again continue git rebase --continue
Finally, submit all the changes using the force flag.
# Force push all the modifications git push --force-with-lease
4. Conclusion
Git interactive rebase is a powerful tool. It allows you to change almost everything in git history. Use it with utmost care.
Join our list to get instant access to new articles and weekly newsletter.