Skip to content

Common Git Errors & Solutions: Complete Git Troubleshooting Guide

28 people viewed this

Common Git Errors & Solutions: Complete Git Troubleshooting Guide

Common Git Errors & Solutions — Complete Guide for PHP & Laravel Developers

Git is one of the most important tools for modern software development, but sooner or later every developer encounters a Git error.

You may see messages such as:

error: failed to push some refs

CONFLICT (content)

fatal: not a git repository

Your branch is behind 'origin/main'

Permission denied (publickey)

fatal: ambiguous argument

error: pathspec did not match any file(s)

Your local changes would be overwritten by merge

For beginners, these messages can look confusing. For experienced developers, they are usually clues about the current state of the repository.

In this article, we will understand the most common Git errors, why they happen, and how to fix them safely with practical PHP and Laravel examples.


1. First Rule: Don't Panic When Git Shows an Error

A Git error usually tells you something important about your repository.

Before running random commands, first check:

git status

Then inspect your branches:

git branch
git branch -vv

Check the remote:

git remote -v

And, when needed:

git log --oneline --decorate --graph --all -10

A good troubleshooting habit is:

Read Error
    ↓
Check Status
    ↓
Understand Repository State
    ↓
Choose the Appropriate Fix
    ↓
Test
    ↓
Continue

2. Git Error #1 — "fatal: not a git repository"

Error

fatal: not a git repository (or any of the parent directories): .git

Why does this happen?

You are running a Git command outside a Git repository.

For example:

cd Downloads
git status

If the current directory does not belong to a Git repository, Git cannot find the .git directory.

Solution

Move into the project directory:

cd pharmacy-erp

git status

If this is a new project and you want to create a Git repository:

git init

Then:

git status

Laravel example

cd C:\xampp\htdocs\pharmacy-erp

git status

Make sure you are actually inside the Laravel project.


3. Git Error #2 — "Your branch is behind origin/main"

Error

Your branch is behind 'origin/main' by several commits,
and can be fast-forwarded.

This means the remote branch contains commits that your local branch does not have.

For example:

Remote:
A---B---C---D

Local:
A---B---C

Your local branch is behind.

Solution

Update your local branch:

git pull origin main

Or, if you prefer to inspect the remote changes first:

git fetch origin

git log HEAD..origin/main --oneline

Then integrate the changes using your team's preferred merge or rebase workflow.


4. Git Error #3 — "failed to push some refs"

Error

! [rejected] main -> main (fetch first)

error: failed to push some refs to 'origin'

Why?

The remote branch has changes that your local branch doesn't have.

For example:

Remote:
A---B---C

Local:
A---B---D

Git refuses to push because doing so could overwrite remote history.

Solution

git fetch origin

git pull origin main

Resolve conflicts if necessary. Then:

git push origin main

If your team uses rebase:

git fetch origin
git rebase origin/main
git push

Use the workflow your team has established.


5. Git Error #4 — "non-fast-forward"

Error

! [rejected] main -> main (non-fast-forward)

error: failed to push some refs

Why?

The remote branch has commits that are not present in your local branch.

Solution

git fetch origin
git pull origin main

Or:

git fetch origin
git rebase origin/main

Then push:

git push

Avoid using force push as the first solution.


6. Git Error #5 — "Updates were rejected because the remote contains work"

Error

! [rejected] main -> main (fetch first)

hint: Updates were rejected because the remote contains
work that you do not have locally.

Why?

The remote repository has commits that aren't in your local repository.

This is common when:

  • Someone else pushed changes.
  • A README was created remotely.
  • A Pull Request was merged.
  • The branch was changed through Bitbucket.

Solution

git pull origin main
git push origin main

If the histories are unrelated, see the "unrelated histories" section later in this article.


7. Git Error #6 — "There is no tracking information for the current branch"

Error

There is no tracking information for the current branch.

Why?

Your local branch does not have an upstream remote branch configured.

For example:

git switch -c feature/payment

You then run:

git pull

Git doesn't know which remote branch should be used.

Solution

Push the branch and set its upstream:

git push -u origin feature/payment

After that, you can normally use:

git pull
git push

The local branch now tracks:

origin/feature/payment

8. Git Error #7 — "The current branch has no upstream branch"

Error

fatal: The current branch feature/payment has no upstream branch.

Solution

git push -u origin feature/payment

The -u option sets the upstream branch.

You can verify it with:

git branch -vv

9. Git Error #8 — "CONFLICT (content)"

Error

CONFLICT (content):
Merge conflict in app/Services/PaymentService.php

Why?

Git cannot automatically combine two changes to the same part of a file.

For example:

Branch A:
return $amount * 0.10;

Branch B:
return $amount * 0.15;

Git cannot decide which implementation is correct.

Solution

Check the conflict:

git status

Open the conflicted file. You may see:

<<<<<<< HEAD

Your changes

=======

Incoming changes

>>>>>>> origin/develop

Choose or combine the correct code.

Then:

git add app/Services/PaymentService.php

Complete the merge:

git commit

If you're resolving a rebase instead:

git rebase --continue

Finally run:

php artisan test

10. Git Error #9 — "Your local changes would be overwritten by merge"

Error

error: Your local changes to the following files would be overwritten by merge:

    resources/views/payments/index.blade.php

Please commit your changes or stash them before you merge.

Why?

You have local modifications that conflict with incoming changes.

Git refuses to overwrite your work.

Solution 1 — Commit the changes

If your changes are ready:

git add .
git commit -m "feat(payment): update payment view"

git pull

Solution 2 — Stash the changes

If the work isn't ready to commit:

git stash

Then:

 
git pull
 

Restore your changes:

 
git stash pop
 

Solution 3 — Discard the changes

 

Only if you are certain you don't need them:

 
git restore resources/views/payments/index.blade.php
 

Be careful: discarded uncommitted changes may not be recoverable through normal Git history.

 

 

11. Git Error #10 — "The following untracked working tree files would be overwritten by merge"

 

Error

 
error: The following untracked working tree files would be
overwritten by merge:

    app/Services/PaymentService.php

Please move or remove them before you merge.
 

Why?

 

You have an untracked local file with the same path as a file coming from the branch you are trying to merge.

 

Solution

 

First inspect:

 
git status
 

If you need the file, back it up or move it somewhere safe.

 

If you don't need it and it is safe to remove:

 
git clean -f -- app/Services/PaymentService.php
 

Then retry the merge or pull.

 

Be careful with git clean because it removes untracked files.

 

 

12. Git Error #11 — "Changes not staged for commit"

 

Message

 
Changes not staged for commit:
  modified: app/Services/PaymentService.php
 

Why?

 

You changed a file, but haven't staged it yet.

 

Solution

 
git add app/Services/PaymentService.php
 

Then:

 
git commit -m "fix(payment): update payment service"
 

For multiple reviewed files:

 
git add .
git commit -m "feat(payment): add payment feature"
 

 

13. Git Error #12 — "Nothing to commit, working tree clean"

 

Message

 
nothing to commit, working tree clean
 

This is not really an error.

 

It means Git doesn't see any changes that need to be committed.

 

Possible reasons

 
  •  
  • You haven't changed anything.
  •  
  • You already committed the changes.
  •  
  • Your changes are ignored.
  •  
  • You are on a different branch than expected.
  •  
 

Check:

 
git status
git branch
git log --oneline -5
 

 

14. Git Error #13 — "fatal: pathspec did not match any file"

 

Error

 
fatal: pathspec 'app/Services/PaymentServcie.php'
did not match any file(s) known to git
 

Why?

 

The path or filename is incorrect.

 

Notice the typo:

 
PaymentServcie.php
             ↑
          incorrect
 

Correct:

 
PaymentService.php
 

Solution

 

Check the actual file:

 
git status
 

Then use the correct path.

 

 

15. Git Error #14 — "Your branch has diverged"

 

Error

 
Your branch and 'origin/main' have diverged,
and have 2 and 3 different commits each, respectively.
 

What does this mean?

 

Both local and remote branches contain commits that the other side doesn't have.

 
        D---E  Local
       /
A---B---C
       \
        F---G---H  Remote
 

Solution

 

First inspect:

 
git fetch origin

git log --oneline --graph --decorate --all
 

Then use your team's preferred integration strategy.

 

Merge:

 
git merge origin/main
 

Or rebase:

 
git rebase origin/main
 

Resolve conflicts if required, test, and push.

 

 

16. Git Error #15 — "fatal: ambiguous argument"

 

Error

 
fatal: ambiguous argument 'main':
unknown revision or path not in the working tree.
 

Why?

 

Git cannot find the branch, tag, commit, or path you specified.

 

For example:

 
git checkout main
 

but the repository may use:

 
master

instead.

 

Solution

 
git branch -a
 

Check the available branches.

 

You may see:

 
* master
  remotes/origin/master
 

Then:

 
git switch master
 

 

17. Git Error #16 — "error: src refspec ... does not match any"

 

Error

 
error: src refspec feature/payment does not match any

error: failed to push some refs to 'origin'
 

Why?

 

Git cannot find the local branch or ref you are trying to push.

 

Possible reasons:

 
  •  
  • Branch name is incorrect.
  •  
  • Branch doesn't exist locally.
  •  
  • You haven't created the branch.
  •  
  • You haven't made an initial commit in a new repository.
  •  
 

Check branches

 
git branch
 

If needed:

 
git switch -c feature/payment
 

Then:

 
git push -u origin feature/payment
 

 

18. Git Error #17 — "src refspec main does not match any"

 

Error

 
error: src refspec main does not match any
 

This commonly occurs in a new repository when there is no local commit yet or when the local branch name differs.

 

Check branch

 
git branch
 

If there is no commit yet:

 
git add .
git commit -m "Initial commit"
 

Then push the actual branch name:

 
git push -u origin main
 

 

19. Git Error #18 — "Permission denied (publickey)"

 

Error

 
Permission denied (publickey).

fatal: Could not read from remote repository.
 

Why?

 

Git is using SSH, but the server cannot authenticate your SSH key.

 

This is common with Bitbucket and other Git hosting platforms.

 

Check SSH keys

 
ls ~/.ssh
 

On Windows, you can inspect your SSH directory using the appropriate shell or file explorer.

 

Test SSH authentication using the host's documented SSH endpoint. For Bitbucket, a common test is:

 
ssh -T git@bitbucket.org
 

Make sure the correct public key is associated with the correct account and that the repository permissions are correct.

 

 

20. Git Error #19 — "Authentication failed"

 

Error

 
remote: Authentication failed
fatal: Authentication failed for 'https://...'
 

Why?

 

The credentials used for the remote repository are invalid or no longer accepted.

 

Possible causes include:

 
  •  
  • Incorrect credentials
  •  
  • Expired authentication method
  •  
  • Incorrect remote URL
  •  
  • Missing repository permission
  •  
  • Incorrect credential stored by the operating system
  •  
 

Check remote URL

 
git remote -v
 

For HTTPS authentication, use the authentication method supported by your Git hosting provider.

 

For SSH, verify:

 
ssh -T git@bitbucket.org
 

 

21. Git Error #20 — "Repository not found"

 

Error

 
remote: Repository not found.
fatal: repository '...' not found
 

Possible reasons

 
  •  
  • Remote URL is incorrect.
  •  
  • Repository was moved.
  •  
  • Repository was deleted.
  •  
  • Your account doesn't have access.
  •  
  • You are authenticated as the wrong account.
  •  
 

Check

 
git remote -v
 

Update the remote if necessary:

 
git remote set-url origin <correct-repository-url>
 

Then test:

 
git fetch origin
 

 

22. Git Error #21 — "Permission denied" When Pushing

 

Error

 
remote: Permission denied
fatal: Could not read from remote repository.
 

Why?

 

You may have access to the repository but not permission to push to the target branch.

 

For example, a protected main branch may require a Pull Request.

 

Solution

 

Push a feature branch instead:

 
git switch -c feature/payment

git push -u origin feature/payment
 

Then create a Pull Request.

 

 

23. Git Error #22 — "fatal: refusing to merge unrelated histories"

 

Error

 
fatal: refusing to merge unrelated histories
 

Why?

 

The local and remote repositories have independent histories.

 

For example:

 
Local repository:
A---B---C

Remote repository:
X---Y---Z
 

This can happen when:

 
  •  
  • You initialized a local repository independently.
  •  
  • The remote repository was initialized with its own commit.
  •  
  • You later tried to merge the two histories.
  •  
 

Solution

 

First make sure you actually intend to combine the repositories. If so:

 
git pull origin main --allow-unrelated-histories
 

Resolve conflicts if required, then commit.

 

Don't use this option blindly. First verify that the two repositories should actually be combined.

 

 

24. Git Error #23 — "fatal: not possible to fast-forward"

 

Error

 
fatal: Not possible to fast-forward, aborting.
 

This can happen when your local and remote histories cannot be integrated as a simple fast-forward.

 

Solution

 

Inspect the history:

 
git fetch origin
git log --oneline --graph --decorate --all
 

Then integrate using the team's chosen strategy:

 
git merge origin/main
 

or:

 
git rebase origin/main
 

 

25. Git Error #24 — "You have unstaged changes"

 

Message

 
error: Your local changes would be overwritten...
 

This generally means your working tree contains modifications that conflict with the operation you're trying to perform.

 

Check:

 
git status
 

Then choose one:

 
# Save permanently
git add .
git commit -m "WIP: save changes"
 

or:

 
# Save temporarily
git stash
 

or discard if safe:

 
git restore <file>
 

 

26. Git Error #25 — "You are in the middle of a merge"

 

Message

 
MERGE_HEAD exists.

or Git reports that a merge is already in progress.

 

Why?

 

A previous merge was started but not completed.

 

Check

 
git status
 

If you want to complete the merge:

 
git add <resolved-files>
git commit
 

If you want to cancel:

 
git merge --abort
 

 

27. Git Error #26 — "You are currently rebasing"

 

Message

 
rebase in progress
 

A previous rebase hasn't completed.

 

Check

 
git status
 

If conflicts are resolved:

 
git add <file>
git rebase --continue
 

If you want to cancel:

 
git rebase --abort
 

Do not start another rebase until the current one is completed or aborted.

 

 

28. Git Error #27 — "Cannot continue because you have unmerged paths"

 

Error

 
error: Committing is not possible because you have unmerged files.
 

Why?

 

A merge conflict hasn't been completely resolved.

 

Check

 
git status
 

Resolve each conflict. Then:

 
git add <resolved-file>
 

Check again:

 
git status
 

Then complete the merge:

 
git commit
 

 

29. Git Error #28 — "You are in 'detached HEAD' state"

 

Message

 
You are in 'detached HEAD' state.
 

What does detached HEAD mean?

 

Normally:

 
HEAD
 ↓
branch
 ↓
commit
 

In detached HEAD:

 
HEAD
 ↓
specific commit
 

You are not currently on a branch.

 

This can happen when you check out a commit directly:

 
git checkout abc1234
 

Solution

 

If you only wanted to inspect the old commit:

 
git switch main
 

If you made useful changes while detached and want to keep them:

 
git switch -c fix/payment-from-old-commit
 

Your changes are now associated with a normal branch.

 

 

30. Git Error #29 — "You have not concluded your cherry-pick"

 

Message

 
error: CHERRY_PICK_HEAD exists.
 

Why?

 

A previous cherry-pick encountered a conflict and hasn't been completed.

 

Check

 
git status
 

After resolving:

 
git add <resolved-file>
git cherry-pick --continue
 

To cancel:

 
git cherry-pick --abort
 

 

31. Git Error #30 — "fatal: bad object"

 

Error

 
fatal: bad object abc1234
 

Git cannot find or correctly access the object you referenced.

 

Possible causes can include:

 
  •  
  • Incorrect commit hash
  •  
  • Missing object
  •  
  • Incomplete repository state
  •  
  • Corrupted Git data in unusual cases
  •  
 

Check:

 
git cat-file -t abc1234
 

Fetch remote objects:

 
git fetch --all --prune
 

If you suspect repository corruption, avoid destructive repair commands until you have a backup and have diagnosed the repository state.

 

 

32. Git Error #31 — "Another git process seems to be running"

 

Error

 
fatal: Unable to create '.git/index.lock':
File exists.
 

Why?

 

Git uses lock files to prevent simultaneous operations from corrupting the index.

 

This can happen if:

 
  •  
  • Another Git process is still running.
  •  
  • An editor or Git GUI crashed.
  •  
  • A previous Git operation was interrupted.
  •  
 

Solution

 

First make sure no Git operation is actually running.

 

If no Git process is running, remove the stale lock file:

 
rm .git/index.lock
 

On Windows, use the equivalent command in your shell, or remove the lock file manually after confirming that no Git process is active.

 

Never delete the lock file while another Git operation is genuinely running.

 

 

33. Git Error #32 — "Branch already exists"

 

Error

 
fatal: a branch named 'feature/payment' already exists
 

Why?

 

You are trying to create a branch that already exists locally.

 

Solution

 

Check:

 
git branch
 

Switch to it:

 
git switch feature/payment
 

Or choose another name:

 
git switch -c feature/payment-v2
 

 

34. Git Error #33 — "Branch not found"

 

Error

 
error: pathspec 'feature/payment' did not match any file(s) known to git
 

The branch may not exist locally.

 

Check all branches:

 
git branch -a
 

If the remote branch exists:

 
git fetch origin
 

Then create a local tracking branch:

 
git switch --track origin/feature/payment
 

 

35. Git Error #34 — Remote Branch Deleted

 

Sometimes a remote branch has been deleted, but your local repository still shows an old remote-tracking reference.

 

Clean stale references:

 
git fetch --prune
 

or:

 
git remote prune origin
 

Then:

 
git branch -r
 

to inspect remote branches again.

 

 

36. Git Error #35 — "Cannot delete branch checked out"

 

Error

 
error: Cannot delete branch 'feature/payment'
checked out at '...'
 

You cannot delete the branch you're currently using.

 

Switch to another branch:

 
git switch main
 

Then:

 
git branch -d feature/payment
 

 

37. Git Error #36 — "Remote origin already exists"

 

Error

 
error: remote origin already exists.
 

Why?

 

The repository already has a remote named origin.

 

Check:

 
git remote -v
 

If you want to change its URL:

 
git remote set-url origin <new-url>
 

If you really want to remove and recreate it:

 
git remote remove origin

git remote add origin <url>
 

 

38. Git Error #37 — Wrong Remote URL

 

Suppose:

 
git remote -v
 

shows an incorrect repository.

 

Update it:

 
git remote set-url origin <correct-url>
 

Verify:

 
git remote -v
 

Then:

 
git fetch origin
 

 

39. Git Error #38 — "Could not resolve host"

 

Error

 
fatal: unable to access 'https://...':
Could not resolve host
 

Possible causes

 
  •  
  • No internet connection
  •  
  • DNS problem
  •  
  • Incorrect remote hostname
  •  
  • Proxy configuration
  •  
  • Corporate network restrictions
  •  
 

Check remote

 
git remote -v
 

Then verify network connectivity and the configured remote hostname.

 

 

40. Git Error #39 — SSL Certificate Problem

 

Error

 
SSL certificate problem:
unable to get local issuer certificate
 

This may indicate a certificate trust or corporate proxy configuration issue.

 

Do not treat disabling SSL verification as the normal solution.

 

Instead, check:

 
  •  
  • System certificate configuration
  •  
  • Git certificate configuration
  •  
  • Corporate proxy settings
  •  
  • Network security software
  •  
  • Remote URL
  •  
 

Avoid using:

 
git config --global http.sslVerify false
 

as a permanent workaround because it weakens TLS verification.

 

 

41. Git Error #40 — "Would be overwritten by checkout"

 

Error

 
error: Your local changes to the following files would be
overwritten by checkout
 

Why?

 

Your current working changes conflict with the branch you're trying to switch to.

 

Solution 1 — Commit

 
git add .
git commit -m "WIP: save current changes"

git switch another-branch
 

Solution 2 — Stash

 
git stash

git switch another-branch
 

Solution 3 — Discard

 

Only if you are certain you don't need the changes:

 
git restore .
 

 

42. Git Error #41 — "Your local changes would be overwritten by checkout"

 

This is essentially the same class of problem as the previous error.

 

Git is protecting your uncommitted work.

 

The safest general workflow is:

 
git status

# If needed
git stash

git switch target-branch
 

Later:

 
git stash pop
 

Resolve any resulting conflicts carefully.

 

 

43. Git Error #42 — Stash Pop Conflict

 

Possible result

 
CONFLICT (content): Merge conflict in app/Services/PaymentService.php
 

This happens when your stashed changes conflict with the current branch.

 

Check:

 
git status
 

Resolve the conflicts manually. Then:

 
git add app/Services/PaymentService.php
 

Commit if appropriate:

 
git commit -m "fix(payment): resolve stash conflict"
 

 

44. Git Error #43 — "Could not apply commit" During Rebase

 

Error

 
error: could not apply abc1234...
 

Git is trying to replay a commit, but the changes conflict with the new base.

 

Solution

 
git status
 

Resolve the conflicted files.

 
git add <resolved-file>

git rebase --continue
 

Repeat until the rebase finishes.

 

If you want to cancel:

 
git rebase --abort
 

 

45. Git Error #44 — Accidentally Committed to the Wrong Branch

 

Suppose you intended to commit to:

 
feature/payment
 

but committed to:

 
develop
 

If the commit has not been pushed, you may be able to move it to the correct branch.

 

One common approach is:

 
# Create the correct branch at the current commit
git switch -c feature/payment

# Return to develop
git switch develop
 

Then reset develop according to the exact state you want.

 

Because resetting a shared branch can be destructive, inspect the history carefully before doing it.

 

 

46. Git Error #45 — Accidentally Committed Sensitive Information

 

Suppose you accidentally committed:

 
.env
API key
database password
private credential
 

Do not assume:

 
git rm .env
 

solves the security problem.

 

If the secret was committed, treat it as exposed.

 

Immediate priorities are:

 
  1.  
  2. Revoke or rotate the secret.
  3.  
  4. Remove it from the current source tree.
  5.  
  6. Assess whether it exists in Git history.
  7.  
  8. Follow your organization's security procedure.
  9.  
 

Then add the appropriate path to .gitignore.

 

 

47. Git Error #46 — Accidentally Used git reset --hard

 

A developer may run:

 
git reset --hard HEAD~1
 

and realize that a commit disappeared from the branch.

 

Git often still has a reference to the previous position in the reflog.

 

Check:

 
git reflog
 

Find the previous commit and inspect it:

 
git show <commit-hash>
 

If it is the commit you need, you can create a recovery branch:

 
git switch -c recovery/payment <commit-hash>
 

This is one reason git reflog is such an important Git troubleshooting tool.

 

 

48. Git Error #47 — Accidentally Deleted a Branch

 

Suppose:

 
git branch -D feature/payment
 

was executed accidentally.

 

Check the reflog:

 
git reflog
 

Find the last commit from the deleted branch.

 

Then recreate the branch:

 
git switch -c feature/payment <commit-hash>
 

The exact recovery depends on whether the commits still exist and whether the branch was also deleted remotely.

 

 

49. Git Error #48 — "Could not read from remote repository"

 

Error

 
fatal: Could not read from remote repository.
 

This usually indicates one of two broad problems:

 
  •  
  • Authentication
  •  
  • Repository permissions/access
  •  
 

Check:

 
git remote -v
 

For SSH:

 
ssh -T git@bitbucket.org
 

For HTTPS, verify the configured remote and the authentication mechanism supported by your Git provider.

 

 

50. Git Error #49 — Push Rejected Because Branch Is Protected

 

Example

 
remote: You are not allowed to push directly to this branch.
 

This can happen when main or another shared branch is protected.

 

Solution

 

Create a feature branch:

 
git switch -c feature/payment
 

Push:

 
git push -u origin feature/payment
 

Then create a Pull Request.

 

 

51. Git Error #50 — "Nothing to Compare"

 

A Pull Request may report that there are no differences between the selected branches.

 

Possible reasons:

 
  •  
  • The changes were already merged.
  •  
  • The source branch points to the same commit as the destination.
  •  
  • The wrong source branch was selected.
  •  
  • The changes were made on another branch.
  •  
 

Check:

 
git log --oneline --graph --decorate --all
 

and:

 
git branch -a
 

 

52. Laravel Example — Git Error During Pull

 

Imagine you're working on:

 
feature/payment
 

You have modified:

 
app/Services/PaymentService.php
 

You run:

 
git pull origin develop
 

and Git reports:

 
error: Your local changes to the following files would be
overwritten by merge
 

Your first step should be:

 
git status
 

If the changes aren't ready:

 
git stash push -m "payment service work"
 

Then:

 
git pull origin develop
 

Restore:

 
git stash pop
 

Run:

 
php artisan test
 

Resolve conflicts if necessary.

 

 

53. Laravel Example — Git Merge Conflict

 

Suppose two developers modify:

 
app/Services/PaymentService.php
 

Developer A changes:

 
$fee = $amount * 0.10;
 

Developer B changes:

 
$fee = $amount * 0.15;
 

Git may produce:

 
<<<<<<< HEAD
$fee = $amount * 0.10;
=======
$fee = $amount * 0.15;
>>>>>>> develop
 

The developer must determine the correct business rule rather than blindly choosing one side.

 

Then:

 
git add app/Services/PaymentService.php
git commit
 

And test:

 
php artisan test
 

 

54. Git Troubleshooting Workflow

 

When you see an unfamiliar Git error, use this process:

 
1. Read the complete error
        ↓
2. Run git status
        ↓
3. Check current branch
        ↓
4. Check remote
        ↓
5. Inspect recent commits
        ↓
6. Identify local vs remote changes
        ↓
7. Choose the least destructive solution
        ↓
8. Resolve conflicts if necessary
        ↓
9. Run tests
        ↓
10. Push/continue the workflow
 

 

55. The Five Commands You Should Know for Git Troubleshooting

 

1. git status

 
git status
 

Shows:

 
  •  
  • Current branch
  •  
  • Modified files
  •  
  • Untracked files
  •  
  • Merge/rebase state
  •  
 

2. git branch

 
git branch -a
 

Shows local and remote branches.

 

3. git remote -v

 
git remote -v
 

Shows configured remote URLs.

 

4. git log

 
git log --oneline --graph --decorate --all -10
 

Shows recent repository history.

 

5. git reflog

 
git reflog
 

Shows movements of references such as HEAD, which can help recover commits after resets, rebases, or deleted branches.

 

 

56. Don't Use Force Push as the First Solution

 

Developers sometimes see a push error and immediately run:

 
git push --force
 

This can overwrite remote history.

 

First understand why the push was rejected.

 

Safer workflow:

 
git fetch origin

git status

git log --oneline --graph --all

# Then choose merge/rebase or another appropriate solution
 

If force pushing is explicitly required for a private feature branch, a safer variant is:

 
git push --force-with-lease
 

Even then, understand the impact before using it.

 

 

57. Git Error Decision Tree

 
Git Error
   │
   ├── Not a repository?
   │      └── cd into project / git init
   │
   ├── Push rejected?
   │      ├── Remote ahead → fetch + merge/rebase
   │      ├── No upstream → git push -u
   │      └── Permission → check access
   │
   ├── Merge conflict?
   │      └── Resolve → git add → continue/commit
   │
   ├── Local changes overwritten?
   │      ├── Commit
   │      ├── Stash
   │      └── Discard if safe
   │
   ├── Authentication?
   │      └── Check SSH/HTTPS credentials
   │
   ├── Branch not found?
   │      └── git branch -a / git fetch
   │
   └── Lost commit?
          └── git reflog
 

 

58. Common Git Errors Cheat Sheet

 
Error Common Cause First Thing to Try
not a git repository Wrong directory cd project
failed to push some refs Remote has new commits git fetch origin
non-fast-forward Branches diverged Merge/rebase remote changes
no upstream branch No tracking branch git push -u origin branch
CONFLICT Changes overlap Resolve conflict
local changes would be overwritten Uncommitted work Commit or stash
pathspec did not match Wrong path/branch Check git status
permission denied Access/authentication Check credentials/permissions
refspec does not match Wrong/missing branch or no commit git branch
detached HEAD Checked out a commit directly Create/switch to a branch
MERGE_HEAD exists Merge unfinished Complete or abort merge
rebase in progress Rebase unfinished git rebase --continue
index.lock exists Stale/active Git process Check process, then remove stale lock
 

 

59. Best Practices to Avoid Git Errors

 
  •  
  • Run git status frequently.
  •  
  • Pull or fetch regularly according to your team workflow.
  •  
  • Keep feature branches reasonably short-lived.
  •  
  • Commit logical changes.
  •  
  • Write meaningful commit messages.
  •  
  • Review changes before committing.
  •  
  • Don't commit secrets.
  •  
  • Don't force push shared branches.
  •  
  • Understand merge conflicts instead of blindly choosing a side.
  •  
  • Run Laravel tests after resolving conflicts.
  •  
  • Keep your remote URL correct.
  •  
  • Use Pull Requests for protected/shared branches.
  •  
  • Learn git reflog for recovery.
  •  
 

 

60. Laravel Developer's Git Safety Checklist

 
Before Pull:
□ git status
□ Save or commit local changes
□ Check current branch

Before Commit:
□ git diff
□ Check .env is ignored
□ Check no secrets are staged
□ Run php artisan test

Before Push:
□ Check branch name
□ Check remote
□ Review commits
□ Make sure you're pushing the intended branch

After Conflict:
□ Resolve all conflict markers
□ git status
□ Run tests
□ Commit/continue operation

After Accidental Reset:
□ Don't panic
□ git reflog
□ Find previous commit
□ Create recovery branch
 

 

61. Git Interview Questions

 

Q1. What does "failed to push some refs" mean?

 

It generally means the remote branch contains history that your local branch does not have, or another push/branch condition prevents your update from being accepted.

 

Q2. How do you fix a non-fast-forward error?

 

Fetch the remote changes and integrate them using the team's merge or rebase strategy, then push again.

 
git fetch origin
git merge origin/main

# or
git rebase origin/main
 

Q3. What causes a merge conflict?

 

A merge conflict occurs when Git cannot automatically reconcile overlapping changes from different histories.

 

Q4. How do you resolve a merge conflict?

 
git status

# Resolve files

git add <resolved-file>

git commit
 

For a rebase:

 
git rebase --continue
 

Q5. What is a detached HEAD?

 

It means HEAD points directly to a commit rather than the tip of a branch.

 

Q6. How do you recover a deleted commit?

 

Use:

 
git reflog
 

Find the commit and create a recovery branch if necessary.

 

Q7. How do you fix "no upstream branch"?

 
git push -u origin feature/payment
 

Q8. What is the difference between git fetch and git pull?

 

git fetch downloads remote updates without integrating them into your current branch. git pull generally performs a fetch followed by an integration step according to the configured pull behavior.

 

Q9. Why should you avoid git push --force?

 

Because it can overwrite remote history and potentially remove commits other developers depend on.

 

Q10. What is git push --force-with-lease?

 

It is a safer force-push option that checks whether the remote reference is still at the expected state before replacing it.

 

Q11. What does git stash do?

 

It temporarily saves uncommitted changes so you can work with a clean working tree.

 

Q12. How do you cancel a merge?

 
git merge --abort
 

Q13. How do you cancel a rebase?

 
git rebase --abort
 

Q14. How do you cancel a cherry-pick?

 
git cherry-pick --abort
 

Q15. How do you check why a file is ignored?

 
git check-ignore -v path/to/file
 

 

62. Easy Way to Remember Git Troubleshooting

 
STATUS
   ↓
BRANCH
   ↓
REMOTE
   ↓
HISTORY
   ↓
LOCAL CHANGES
   ↓
CONFLICT
   ↓
FIX
   ↓
TEST
   ↓
PUSH
 

The most important command during troubleshooting is often:

 
git status
 

Don't underestimate it. It frequently tells you exactly which Git operation is currently in progress and what Git expects you to do next.

 

 

Conclusion

 

Git errors are a normal part of software development.

 

The important skill is not memorizing every error message. It is learning how to interpret Git's information and safely recover from the current repository state.

 

For PHP and Laravel developers, the most important errors to understand are:

 
  •  
  • Non-fast-forward push
  •  
  • Failed push
  •  
  • Missing upstream branch
  •  
  • Merge conflicts
  •  
  • Local changes being overwritten
  •  
  • Authentication failures
  •  
  • Permission problems
  •  
  • Wrong branch/refspec
  •  
  • Detached HEAD
  •  
  • Merge/rebase/cherry-pick in progress
  •  
  • Unrelated histories
  •  
  • Git index lock problems
  •  
  • Accidental reset or branch deletion
  •  
 

When you encounter an unfamiliar Git problem, remember:

 
Don't blindly run commands.

Read the error.
       ↓
git status
       ↓
Understand the state.
       ↓
Choose the least destructive fix.
       ↓
Test your Laravel application.
       ↓
Continue the Git workflow.
 

Once you develop this troubleshooting habit, Git errors become much easier to understand and resolve.

 

 

What's Next?

 

We have now covered Git errors and troubleshooting. The next useful topic for the Git series is one of the most important skills for professional development and interviews: understanding Git tags and release versions.

 

Next Article: Git Tags & Releases Explained

 

We will cover:

 
  •  
  • What is a Git tag?
  •  
  • Lightweight vs annotated tags
  •  
  • Creating tags
  •  
  • Semantic versioning
  •  
  • Tagging Laravel releases
  •  
  • Pushing tags to Bitbucket
  •  
  • Listing and deleting tags
  •  
  • Checking out a specific release
  •  
  • Git tags vs branches
  •  
  • Production release workflow
  •  
  • Git tag interview questions
  •  
 

Was this useful?

No ratings yet — yours can be the first.

Discussion

Notes from readers. Comments appear after a short review.

No comments yet. Start the thread below.