

When you use git rebase -i HEAD~n there can be more than n commits. Once you squash your commits - choose the e/r for editing the message: In order to do a Git squash, follow these steps: // n is the number of commits up to the last commit you want to be able to edit
Edit git commit message update#
This allows you to edit any message you want to update even if it's not the latest message. Perform an interactive rebaseĪnother option is to use interactive rebase. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.

Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Warning: be cautious about amending commits that you have already shared with other people. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits. Warning: force-pushing will overwrite the remote branch with the state of your local one. If you've already pushed your commit up to your remote branch, then - after amending your commit locally (as described above) - you'll also need to force push the commit with: git push -force ( Unstaged changes will not get committed.) Changing the message of a commit that you've already pushed to your remote branch Make sure you don't have any working copy changes staged before doing this or they will get committed too. …however, this can make multi-line commit messages or small corrections more cumbersome to enter. Additionally, you can set the commit message directly in the command line with: git commit -amend -m "New commit message" Will open your editor, allowing you to change the commit message of the most recent commit. # Note that empty commits are commented outĪs you can see all of the last N commits are listed, and you can modify their messages as needed.Amending the most recent commit message git commit -amend # However, if you remove everything, the rebase will be aborted. # If you remove a line here THAT COMMIT WILL BE LOST.

# Rebase d05209d.5e9cdc5 onto d05209d (4 commands) # 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 log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # These lines can be re-ordered they are executed from top to bottom. Pick 788ebf0 Oops, I forgot to add a new file You'll then see a list of commits, similar to this: $ $ git rebase -i HEAD~4 Here, n is the number of commits you want to inspect and edit. To start the rebase, you can use the following command: $ git rebase -i HEAD~n Doing this allows you to inspect the last N commits and make changes to them, including picking, squashing, etc. Use Interactive RebaseĪnother option, which is more flexible than the previous solutions described, is to use an interactive rebase. Otherwise, you may need to find another solution to avoid losing any changes made after the commit message you're wanting to change. If you can catch the mistake fast enough, then making the change won't be a problem. We need to do it this way in order to overwrite the remote repository with our own local state.īe aware that by forcing the push, you'll also lose any commits on the local branch that you don't already have in your local branch. However, this must be done using the -force flag. Then, you need to push these changes to the remote repository. The first step is to amend the last commit, just like we did in the previous section: $ git commit -amend -m "Added a new file" This is another common case, and is a bit more difficult to fix, not only because we need to modify our own repository, but we also need to modify the remote repository, which could be further ahead than your local branch. Personally, I prefer to do most things on the command line, which you can do by adding an argument and message to the command above: $ git commit -amend -m "Added a new file"ĭoing it this way won't open the editor, but instead it'll just modify the commit message immediately.
