Common mistakes and practice
40 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Showing steps for Windows. They are marked “On Windows” below.
Almost every GitHub problem comes from a few mistakes: a GitHub repository that does not match your laptop, work that was never committed, a branch with no upstream, or a pull over unsaved changes. Each one prints a clear message and has a short, safe fix.
Mistakes and fixes at a glance
Read the first error:, fatal: or ! [rejected] line: it names the problem.
- Password refused (
Authentication failed) → browser sign-in on Windows, a personal access token on macOS, or an SSH key. Review: Put your project on GitHub (opens in a new tab) ! [rejected] ... (fetch first)→git pull, thengit push; never--force. Review: Clone, push, pull and fetch (opens in a new tab)- Image or code block broken on GitHub → check the file name's capitals and the closing backticks. Review: Write a README in Markdown (opens in a new tab)
- A second pull request for a review fix → push the fix to the same branch. Review: Pull requests and code review (opens in a new tab)
Permission ... denied→ fork, push to your fork, open a pull request. Review: Forks, issues and open source (opens in a new tab)
.env files, not even to a private repository. If one was pushed, deleting the file in a new commit is not enough: it stays in the history. Delete the token on GitHub (or change the password) first, then make a new one, and add the file to .gitignore as in .gitignore and keeping secrets out (opens in a new tab).The GitHub repository was created with a README
Priya made bus-pass on GitHub with Add a README ticked, then connected her local bus-pass project, which has its own commits and its own README.md.
On Windows
In PowerShell:
PS C:\Users\Priya\bus-pass> git push -u origin mainPS C:\Users\Priya\bus-pass> git pull origin mainPS C:\Users\Priya\bus-pass> git pull origin main --allow-unrelated-historiesOutputTo https://github.com/priya-dev/bus-pass.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/priya-dev/bus-pass.git' hint: Updates were rejected because the remote contains work that you do not hint: have locally. This is usually caused by another repository pushing to hint: the same ref. If you want to integrate the remote changes, use hint: 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) From https://github.com/priya-dev/bus-pass * branch main -> FETCH_HEAD * [new branch] main -> origin/main fatal: refusing to merge unrelated histories From https://github.com/priya-dev/bus-pass * branch main -> FETCH_HEAD Auto-merging README.md CONFLICT (add/add): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result.
- The first push is rejected: the GitHub repository already has a commit (its README) that Priya's laptop does not have.
refusing to merge unrelated histories: the two sides have no commit in common, so Git will not join them unless you allow it once.- With
--allow-unrelated-historiesthe merge runs but both sides have aREADME.md: a normal conflict. Keep the better text, thengit add README.md,git commitandgit push -u origin main.
This happens only once per repository. Next time, leave Add a README off for a project that already exists on your laptop.
"Everything up-to-date", but GitHub shows the old code
Priya edits bill.txt and runs git push straight away. Git says everything is up to date, yet GitHub shows the old file.
Example 1 · Nothing new was committed
Runs in: Your terminal
PS C:\Users\Priya\shop-bill> git pushPS C:\Users\Priya\shop-bill> git status --shortOutputEverything up-to-date M bill.txt
What this code does
git pushsends commits, and there is no new commit: the edit is only in the file.M bill.txtshows the change is not even staged.- Fix:
git add bill.txt,git commit -m "...", thengit push.
A new branch without -u
Priya makes a branch fix-total, commits on it and types a plain git push, as she does on main.
Example 2 · No upstream branch
Runs in: Your terminal
PS C:\Users\Priya\shop-bill> git pushfatal: The current branch fix-total has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin fix-total To have this happen automatically for branches without a tracking upstream, see 'push.autoSetupRemote' in 'git help config'.
What this code does
fix-totalexists only on Priya's laptop, so Git does not know where to push it.- The message gives the fix:
git push --set-upstream origin fix-total, which is the same asgit push -u origin fix-total. - After that one time, a plain
git pushworks on this branch.
Pulling over uncommitted changes
Rahul pushed a new line in README.md. Priya is in the middle of her own README edit, not committed yet, and runs git pull.
On Windows
In PowerShell:
PS C:\Users\Priya\shop-bill> git pullremote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0) From https://github.com/priya-dev/shop-bill e9e0b83..8a95c45 main -> origin/main Updating e9e0b83..8a95c45 error: Your local changes to the following files would be overwritten by merge: README.md Please commit your changes or stash them before you merge. Aborting
- Rahul pushed a new README line; Priya changed
README.mdtoo but did not commit it. - Git refuses to merge over work that is not saved in a commit, so nothing is lost. The fetch part did run:
origin/mainmoved. - Fix:
git add README.md,git commit, thengit pullagain (fix a conflict if both changed the same line) andgit push.
Finish up
Mark this lesson complete
This is the last lesson in this section.