Common errors and solutions
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:
- Check if the existing
.gitignore
file contains the correct rules:gitignore# Correct example node_modules/ *.log .env
- Remove tracked files that should be ignored:
bash
git rm -r --cached node_modules git commit -m "remove ignored files"
- 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:
- Use
git status
to locate conflicting files. - 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
- 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
:
- Locate the lost commit:
bash
git fsck --lost-found
- Check the generated
.git/lost-found
directory. - 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:
- Remove the large file from history:
bash
git filter-branch --tree-filter 'rm -f assets/video.mp4' HEAD
- Configure the correct
.gitattributes
:gitignore*.mp4 filter=lfs diff=lfs merge=lfs -text
- 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
上一篇:学习资源推荐
下一篇:let关键字及其块级作用域