Git学习笔记(一):常见问题及其解决办法

常见错误(一):fatal: remote origin already exists

输入以下命令关联远程库(注:以下的 operating-system-and-xv6 是我自己的一个仓库名):

1
$ git remote add origin git@github.com:dengfuping/operating-system-and-xv6.git

出现 fatal: remote origin already exists 的问题:

只需输入以下命令将远程关联删除即可解决:

1
$ git remote rm origin

之后再使用 git remote add origin git@github.com:dengfuping/operating-system-and-xv6.git 命令将本地仓库与远程库重新关联即可。

常见问题(二):error: failed to push some refs to

我在将本地的仓库和远程对应的 Github 的远程库进行关联之后,输入 git push -u origin master 命令出现如下错误:

1
2
3
4
5
6
7
8
9
$ git push -u origin master
To git@github.com:dengfuping/operating-system-and-xv6.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:dengfuping/operating-system-and-xv6.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 新建远程仓库的时候创建了一个 README.md 文件,而这个文件则不在本地仓库目录中。

可以通过如下命令进行代码合并(其中 pull = fetch + merge):

1
$ git pull --rebase origin master

执行上面代码后就可以看到本地代码库中多了 README.md 文件。此时再执行语句 git push -u origin master 即可完成代码上传到 Github 上。

坚持原创技术分享,您的支持将鼓励我继续创作!

热评文章