Git:带账号密码的HTTPS推送后远程未更新,如何解决?
Hey there! Let's break down why you're running into this confusing situation and get it sorted out so you can focus on your course without interruptions.
Why the weird git status message?
When you use the full URL with your username/password to push (git push https://username:password@github.com/username/repo.git master), Git successfully sends your changes to GitHub—but it doesn’t update the local "remote tracking branch" (that’s the origin/master reference Git uses to compare your local master branch against). So even though your code is on GitHub, Git still thinks your local branch is ahead of the remote one you have configured (origin), hence the mismatch message.
And when you run git push origin master afterward, you’re using the original origin remote URL (which doesn’t include your credentials), so Git prompts you for username/password again.
Solution 1: Update your origin remote to include credentials (one-time setup)
The simplest fix is to reconfigure your local repository’s origin remote to use the URL with your username and password embedded. This way, every future git push or git pull will automatically use those credentials, no prompts needed, and Git will properly update the remote tracking branch.
Run this command in your repository:
git remote set-url origin https://username:password@github.com/username/repo.git
After this, you can just use git push origin master (or even just git push if your branch is set to track origin/master) and Git will handle the credentials automatically. Plus, git status will correctly reflect the state of your local and remote branches.
Solution 2: Use Git’s credential helper (safer than embedding credentials in URL)
If you’re worried about having your password stored in plaintext in your Git config, use Git’s built-in credential helper to temporarily store your credentials locally (this is different from SSH keys, so it’s totally okay for your use case).
Run this command to enable the credential helper (it will store your credentials in a secure system-specific store—Keychain on Mac, Credential Manager on Windows, or libsecret on Linux):
git config --global credential.helper store
The next time you run git push origin master, you’ll enter your username and password once, and Git will remember them for future operations. No more prompts, and your credentials aren’t exposed in your Git config.
Quick fix for the current status mismatch
If you just want to resolve the git status message right now (before setting up the above solutions), run:
git fetch origin
This will update your local origin/master reference to match the actual state of the remote repository, and git status will show your branch is up to date.
内容的提问来源于stack exchange,提问作者RinkyPinku

