doc/tutorials/update_commit_messages/_index.md
{{< details >}}
{{< /details >}}
Occasionally, after you've made a few commits to your branch, you realize you need to update one or more commit messages. Perhaps you found a typo, or some automation warned you that your commit message didn't completely align with a project's commit message guidelines.
Updating the message can be tricky if you don't have much practice using Git from the command-line interface (CLI). But don't worry, even if you have only ever worked in the GitLab UI, you can follow these steps to use the CLI.
This tutorial explains how to rewrite commit messages. If you work in the GitLab UI only, start from the beginning. If you already have your repository cloned locally, you can skip to the step to fetch and check out your branch.
You must have:
You must authenticate with GitLab to overwrite the commit messages in the final step. If your GitLab account uses basic username and password authentication, you must have two factor authentication (2FA) disabled to authenticate from the CLI. Alternatively, you can use an SSH key to authenticate with GitLab.
To get started, create a local copy of your repository on your machine.
{{< guide >}}
Copy the repository URL.
In GitLab, on your project's overview page, in the upper-right corner, select Code. In the dropdown list, copy the URL for your repository by selecting {{< icon name="copy-to-clipboard" >}} next to:
Clone the repository.
Switch to the CLI (Terminal, PowerShell, or similar) on your local machine, and go to
the directory where you want to clone the repository. For example, /users/my-username/my-projects/.
Run git clone and paste the URL you copied earlier:
git clone https://gitlab.com/my-username/my-awesome-project.git
This clones the repository into a new directory called my-awesome-project/.
{{< /guide >}}
Now your repository is on your computer, ready for your Git CLI commands.
Next, switch to the branch that contains the commits you want to update.
{{< guide >}}
Change to your project directory.
Assuming you are still at the same place in the CLI as the previous step,
change to your project directory with cd:
cd my-awesome-project
Fetch your branch if needed.
If you've just cloned the repository, your branch should already be on your computer. However, if you previously cloned the repository and skipped to this step, you might need to fetch your branch:
git fetch origin my-branch-name
Check out the branch.
Now that the branch is on your local system, switch to it:
git checkout my-branch-name
Verify you're on the correct branch.
Run git log and check that the most recent commits
match the commits in your branch on GitLab. To exit the log, use q.
{{< /guide >}}
Now you're ready to rewrite the commit messages using an interactive rebase.
{{< guide >}}
Determine how many commits to update.
In GitLab, check how far back in the commit history you need to go:
Start an interactive rebase.
From the CLI, start the rebase process by adding the count of commits from the previous step
to the end of HEAD~:
git rebase -i HEAD~4
In this example, Git selects the four most recent commits in the branch to update.
Mark which commits to reword.
Git launches a text editor and lists the selected commits. For example, it should look similar to:
pick a0cea50 Fix broken link
pick bb84712 Update milestone-plan.md
pick ce11fad Add list of maintainers
pick d211d03 update template.md
# Rebase 1f5ec88..d211d03 onto 1f5ec88 (4 commands)
#
# 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 [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# [and so on...]
The pick command tells Git to use the commits without change. Change
the command from pick to reword for the commits you want to update.
Type i to enter INSERT mode, and then start editing the text.
For example, to update the text of the second and fourth commits in the previous sample, edit it to look like:
pick a0cea50 Fix broken link
reword bb84712 Update milestone-plan.md
pick ce11fad Add list of maintainers
reword d211d03 update template.md
Save your changes.
Press <kbd>Escape</kbd> to exit INSERT mode,
then type :wq and <kbd>Enter</kbd> to save and exit.
Update each commit message.
Git now goes through each commit one at a time. Any commits with pick are added back to
the branch unchanged. When Git reaches a commit with reword, it stops and opens the text
editor again.
Update the commit message as needed:
Update the monthly milestone plan
Update the monthly milestone plan
Make the milestone plan clearer by listing the responsibilities
of each maintainer.
After you save and exit, Git updates the commit message and processes the next
commits in order. You should see the message Successfully rebased and update refs/heads/my-branch-name
when finished.
Verify the updates.
To confirm the commit messages were updated, run git log
and scroll down to see the commit messages.
{{< /guide >}}
Finally, push your updated commits back to GitLab.
{{< guide >}}
Force push the changes.
From the CLI, push the changes back to GitLab. You must use the -f "force push" option,
because the commits were updated and a force push overwrites the old commits in GitLab:
git push -f origin
Your terminal might prompt you for your username and password before overwriting the commit messages in GitLab.
Verify the changes in GitLab.
In your project in GitLab, confirm that the commits have been updated:
{{< /guide >}}
Congratulations, you have successfully updated your commit messages and pushed them to GitLab!