Conclusion
When uploading a Git project for the first time, set the correct user information beforehand. If you discover an incorrect Commit Email after pushing, you can use git commit --amend --reset-author to correct the most recent Commit, then safely force update the remote.
When to Use This
- First-time upload of a local project to GitHub
- Discovering incorrect author name or email settings after a commit
- GitHub Contribution not correctly linked to the account
- Only needing to correct the author information of the “most recent commit”
Step-by-Step Process
1. Initialize Git Project
Initialize Git in your project directory, add all files, and set your GitHub Repository as the remote source.
git initgit add .git remote add origin https://github.com/user-name/project-name.gitgit branch -M maingit branch -M main renames the current branch to main to prevent discrepancies between local and remote default branch names.
2. Configure Commit Author Information and Upload
Before the first commit, ensure user.name and user.email are correct; otherwise, the commit will record incorrect author information.
# 設定目前 Repository 的 Git 作者資訊git config user.name "user-name"git config user.email "user-email"
# HTTPS Credential 依完整 Repository 路徑區分git config credential.useHttpPath true
# 建立第一次 Commitgit commit -m "Initial commit"
# 上傳到 GitHubgit push -u origin mainIf you want the settings to apply to all repositories, add --global; otherwise, omit it to affect only the current project.
git config --global user.name "user-name"git config --global user.email "user-email"3. Correcting an Already Committed Email
If you’ve already committed, or even pushed, and then discover an incorrect author email, first change your Git settings to the correct information, then recreate the author information for the most recent commit.
# 先設定正確作者資訊git config user.name "user-name"git config user.email "user-email"
# 使用目前設定重新建立最近一次 Commit 作者資訊git commit --amend --reset-author --no-edit
# 確認修改結果git log -1 --format="%an <%ae>"
# 更新遠端 Commitgit push --force-with-lease origin main-no-editpreserves the original Commit Message;-reset-authorrewrites the author information using the current Git settings.
Additional Notes
git commit --amendgenerates a new Commit Hash, so a pushed commit will require a force update to the remote.- Prioritize using
-force-with-leaseas it is safer than direct-force, reducing the risk of overwriting someone else’s remote updates. - The method above only corrects the “most recent commit”; if the incorrect email exists in multiple historical commits, you’ll need to rewrite Git History, which cannot be done with a single
-amend.
Command / Example Summary
Click to expand
# 初始化專案git initgit add .git remote add origin https://github.com/user-name/project-name.gitgit branch -M main
# 設定作者git config user.name "user-name"git config user.email "user-email"git config credential.useHttpPath true
# 第一次 Commit / Pushgit commit -m "Initial commit"git push -u origin main
# 修正最近一次 Commit 作者資訊git commit --amend --reset-author --no-edit
# 確認 Authorgit log -1 --format="%an <%ae>"
# 更新遠端git push --force-with-lease origin mainConclusion
Confirming user.name and user.email before your first push can save you the trouble of modifying Commit History later. If you do push with incorrect information, amend + force-with-lease is the most straightforward way to correct the most recent commit.