返回
Git 操作常见问题一网打尽
前端
2023-12-14 21:33:00
1. 无法提交代码:git commit
报错
$ git commit -m "feat: add new feature"
fatal: empty commit message
解决方案: 提交代码时,需要提供一个提交信息,所做的更改。确保在 -m
参数后加上提交信息。
$ git commit -m "feat: 添加新功能"
2. 提交错误,需要回滚
解决方案: 使用 git reset
命令回滚到上一次提交。
$ git reset HEAD~1
3. 分支切换问题:git checkout
报错
$ git checkout new-branch
error: pathspec 'new-branch' did not match any file(s) known to git.
解决方案: 确保目标分支存在。如果不存在,可以使用 git branch
创建一个新的分支。
$ git branch new-branch
$ git checkout new-branch
4. 远程仓库连接问题:git push
报错
$ git push origin master
remote: Permission to origin.master denied to user-name.
fatal: unable to access 'https://github.com/user-name/repo-name.git/'
解决方案: 检查远程仓库的访问权限,确保您有推送权限。
5. 跟踪远程分支问题:git pull
报错
$ git pull origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
解决方案: 确保远程仓库存在并且可访问。如果远程仓库不存在,可以使用 git remote add
添加一个新的远程仓库。
6. 合并冲突:git merge
报错
$ git merge new-branch
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt
解决方案: 合并冲突需要手动解决。打开冲突的文件,解决冲突并提交更改。
7. 撤销本地更改:git restore
解决方案: 使用 git restore
命令撤销本地更改。
$ git restore file1.txt
8. 查看提交历史:git log
$ git log
commit 1234567890abcdef
Author: User Name <user@email.com>
Date: 2023-01-01T00:00:00Z
feat: 添加新功能
解决方案: 使用 git log
查看提交历史。-p
参数可以显示每次提交的更改。
$ git log -p
9. 创建分支:git branch
$ git branch new-branch
解决方案: 使用 git branch
创建一个新的分支。
10. 切换分支:git checkout
$ git checkout new-branch
解决方案: 使用 git checkout
切换到一个现有的分支。