Git

学习 Git 遇到的坑

即学即记

Posted by 白 on September 7, 2018

学习Git遇到的坑


如果不明白文中指令含义,建议查看 Git 笔记整理

1. git commit 遇到 error:pathspec

原因:输入 git commit -m "messages" 时,忘记加 -u

2. git commit -am "messages" ,不能提交新增文件

先使用 git add -A ,提交全部新增文件,再使用 git commit -m "messages"

3. 有时候 git push origin master 会报错,如下:

$ git push origin master
To github.com:smartBBer/LeetCode.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:smartBBer/LeetCode.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

错误原因很简单:本地库和GitHub中的库不同步,解决办法有两种。

第一种解决方法(不要轻易使用)

加上 -f,强制推送上去,这时你的GitHub上的库会以本地同步,

$ git push -f
Enumerating objects: 35, done.
Counting objects: 100% (35/35), done.
Delta compression using up to 4 threads
Compressing objects: 100% (24/24), done.
Writing objects: 100% (35/35), 6.35 KiB | 325.00 KiB/s, done.
Total 35 (delta 6), reused 6 (delta 1)
remote: Resolving deltas: 100% (6/6), done.
To github.com:smartBBer/LeetCode.git
 + 0794cc5...80639ea master -> master (forced update)

第二种解决方法(推荐)

使用git pull --rebase origin master合并 GitHub 和 本地 的库,本地会多出之前不同步的文件,在使用 git push -u origin master 推送到 GitHub 库中。

$ git pull --rebase origin master
$ git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

解决办法:重新输入一次:git remote add origin [email protected]:yourusername/test.git

fatal: remote error:
XXXXXX is not a valid repository name Email [email protected] for help

解决办法:使用git remote rm origin 然后再使用上传命令

4. 输入:git remote add origin [email protected]:yourName/yourRepo.git

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

解决办法:输入 git init ,初始化一个本地仓库

fatal: remote origin already exists.

解决办法: 1 删除Git仓库中的origin信息:git remote rm origin

2 重新添加Git仓库中的origin信息

5. 输入git add ./时

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

解决办法:先输入git init,再add

欢迎关注我的微信公众号

微信公众号:柏战不殆