Change the username for commits you haven't pushed yet
If you've made commits locally but haven't pushed them to a remote repository (like GitHub or GitLab), you can rewrite the author name on those commits. The simplest way is to use git commit --amend, which rewrites the most recent commit with your new username.
First, set your new username in Git's configuration. Open your terminal or command prompt and run:
git config user.name "Your New Name"
This changes the username for the current repository only. If you want the new name to explore to all repositories on your machine, add the --global flag: git config --global user.name "Your New Name"
Now amend the most recent commit:
git commit --amend --no-edit
The --no-edit flag keeps your commit message unchanged and only updates the author. If you have multiple unpushed commits, you'll need to use interactive rebase instead (see the next section).
Rewrite multiple unpushed commits at once
If you need to change the username on several commits, use interactive rebase. First, set your new username as shown above. Then count how many commits back you want to go — for example, if you want to change the last three commits, run:
git rebase -i HEAD~3
This opens an editor showing your last three commits. Change the word pick to reword on each line you want to modify, then save and close the editor. Git will open your editor again for each commit, letting you confirm or change the message. The username will automatically update to whatever you set in your configuration.
If you want to change the username without touching the commit messages, use a different approach: run git rebase -i HEAD~3 again, change pick to exec, and add git commit --amend --no-edit after each line. This is more complex, so the reword method is usually simpler.
Key Takeaways
- Set your new username with git config user.name "Your Name" before making any changes to commits.
- For a single unpushed commit, use git commit --amend --no-edit to rewrite the author without changing the message.
- For multiple unpushed commits, use git rebase -i HEAD~N where N is the number of commits, then mark each line with reword.
- Never rewrite commits that have already been pushed to a shared repository, because it will confuse other people working on the same code.
What to do if you've already pushed the commits
Once you've pushed commits to a remote repository, rewriting them locally creates a mismatch between your machine and the server. Other people cloning or pulling from that repository will see the old username, and you'll have to force-push to overwrite the history — which can break other people's work if they've already pulled those commits.
In most cases, it's better to leave pushed commits as they are and use the correct username going forward. If you absolutely must change the username on pushed commits, you can force-push after rewriting them locally, but only if you're the only person working on that branch. Run git push --force-with-lease instead of git push — the --force-with-lease flag is safer because it will refuse to push if someone else has added commits since you last pulled.
For shared repositories or production code, changing pushed history is almost never worth the risk. Document the reason for the change in a new commit message instead, or contact your team lead before proceeding.
Verify your username is set correctly
Before you make new commits, check that Git is using the name you want. Run:
git config user.name
This shows the username for the current repository. If you set it with --global, you can check that too:
git config --global user.name
Git uses the repository-level setting if it exists, and falls back to the global setting otherwise. So if you set a global username but then set a different one for a specific repository, that repository will use its own setting.
The difference between username and email
Git tracks both a username and an email address on each commit. The username is the display name people see; the email is used to link commits to accounts on platforms like GitHub. You can set both separately:
git config user.name "Your Name" git config user.email "your.email@example.com"
If you only change the username but not the email, platforms like GitHub may still show the old email address on your commits. To fully change how a commit appears, update both the name and the email, then amend or rebase as described above.
Using different usernames for different repositories
If you work on multiple projects and want different usernames for each one, set the username at the repository level instead of globally. Navigate into the repository folder and run:
git config user.name "Project-Specific Name"
Without the --global flag, this setting applies only to that folder and its subfolders. This is useful if you have work repositories where you use your full name and personal repositories where you use a nickname or handle.
You can check which setting is active by running git config user.name from inside the repository — it will show the repository-level setting if one exists, or the global setting if not.
Frequently Asked Questions
Can I change the username on commits from months ago?
Yes, but only if those commits haven't been pushed. If they're on your local machine only, use interactive rebase to go back as far as you need. If they're already pushed, rewriting them will require force-pushing, which can break other people's work. It's safer to leave old pushed commits alone.
Will changing my username affect my GitHub or GitLab account?
No. Your Git username is separate from your account on GitHub, GitLab, or other platforms. Changing your Git username only changes what name appears on future commits. The platform will still link commits to your account based on the email address, so make sure the email in your Git configuration matches the email on your account.
What if I set the wrong username globally and made commits with it?
Update your global username with git config --global user.name "Correct Name", then use interactive rebase on any unpushed commits to rewrite them. For pushed commits, it's usually not worth rewriting — just use the correct username going forward.
Do I need to change my username if I'm using SSH keys?
No. SSH keys control authentication (proving you are who you say you are), while the Git username controls what name appears on commits. They are separate. You can have any username in your Git configuration regardless of which SSH key you use.