阿里云主机折上折
  • 微信号
Current Site:Index > Common errors and solutions

Common errors and solutions

Author:Chuan Chen 阅读数:15399人阅读 分类: 开发工具

Git, as a distributed version control system, is widely used for code management, but various issues can arise during its usage. Below are common error scenarios and their solutions.

Accidentally Ignoring Files During Commit

Misconfigured .gitignore files can lead to sensitive or temporary files being accidentally committed. For example:

bash 复制代码
# Error scenario
$ git add .
$ git commit -m "add config"
# Notice node_modules/ was also committed

The solution involves three steps:

  1. Check if the existing .gitignore file contains the correct rules:
    gitignore 复制代码
    # Correct example
    node_modules/
    *.log
    .env
  2. Remove tracked files that should be ignored:
    bash 复制代码
    git rm -r --cached node_modules
    git commit -m "remove ignored files"
  3. Verify the ignore effect:
    bash 复制代码
    git status --ignored

Merge Conflict Failures

Merge conflicts often occur during collaboration. A typical error message:

复制代码
CONFLICT (content): Merge conflict in src/app.js
Automatic merge failed; fix conflicts and then commit the result.

Resolution steps:

  1. Use git status to locate conflicting files.
  2. Manually edit the files to resolve conflict markers:
    javascript 复制代码
    // Conflict example
    <<<<<<< HEAD
    const apiUrl = 'https://prod.example.com';
    =======
    const apiUrl = 'https://test.example.com';
    >>>>>>> feature/new-api
  3. Keep the desired version and recommit:
    bash 复制代码
    git add src/app.js
    git commit -m "resolve api url conflict"

Recovering Accidentally Deleted Branches

After executing git branch -D feature/login, recover the branch as follows:

bash 复制代码
# View recent reference history
git reflog
# Find the commit hash before deletion
git checkout -b feature/login a1b2c3d

Incorrect Commit Messages

To modify the most recent commit message:

bash 复制代码
git commit --amend -m "New commit message"
# Force push to remote (only for branches not shared with others)
git push origin feature/login --force

Recovering from Incorrect Code Resets

Recover lost uncommitted code after using git reset --hard:

  1. Locate the lost commit:
    bash 复制代码
    git fsck --lost-found
  2. Check the generated .git/lost-found directory.
  3. Use git show to verify the content before recovery.

Large File Commit Errors

When encountering errors like remote: error: File is 123.00 MB while committing large files:

  1. Remove the large file from history:
    bash 复制代码
    git filter-branch --tree-filter 'rm -f assets/video.mp4' HEAD
  2. Configure the correct .gitattributes:
    gitignore 复制代码
    *.mp4 filter=lfs diff=lfs merge=lfs -text
  3. Use git-lfs to manage large files.

Branch Pollution

When multiple features are mixed in the same branch:

bash 复制代码
# Currently on dev branch with unfinished features A and B
git stash save "feature A"
git checkout -b feature/B
# After completing B
git checkout dev
git stash pop

Credential Storage Issues

To avoid frequent password prompts, configure credential storage:

bash 复制代码
git config --global credential.helper store
# The next input will be saved

Chinese Path Display Issues

Fix octal encoding of Chinese paths in git status:

bash 复制代码
git config --global core.quotepath false

Submodule Update Failures

When updating a project containing submodules:

bash 复制代码
git submodule update --init --recursive
# Or clone with submodules directly
git clone --recurse-submodules <repo-url>

Line Ending Issues

Resolve CRLF/LF warnings during cross-platform development:

bash 复制代码
# Convert to LF uniformly
git config --global core.autocrlf input
# For Windows systems
git config --global core.autocrlf true

Risks of Rewriting History

Interactive rebasing may disrupt collaborative branches:

bash 复制代码
# Safe operation steps
git checkout feature
git rebase -i main
# After resolving conflicts, force push
git push origin feature --force-with-lease

Tag Management Errors

Recover accidentally deleted tags:

bash 复制代码
# Local recovery
git tag v1.0.1 a1b2c3d
# Push to remote
git push origin v1.0.1

Cleaning the Working Directory

To completely remove untracked files:

bash 复制代码
git clean -fd
# Preview files to be deleted
git clean -fdn

Bisect Debugging

Use git bisect to locate problematic commits:

bash 复制代码
git bisect start
git bisect bad HEAD
git bisect good v1.0
# Mark the current commit after testing
git bisect good/bad
# Reset after completion
git bisect reset

Patch Application Errors

Path issues when applying .patch files:

bash 复制代码
git apply --check fix.patch
# Ignore path levels
git apply -p2 fix.patch

Expired Reflog

By default, git reflog records expire after 30 days. Modify the retention period:

bash 复制代码
git config --global gc.reflogExpire 90.days

Sparse Checkout

Clone only specific directories:

bash 复制代码
git clone --filter=blob:none --no-checkout <repo>
cd repo
git sparse-checkout init --cone
git sparse-checkout set src/docs
git checkout main

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.