使用git 的各种报错
在git 上创建了一个仓库(附带README文件),在上传本地文件的过程中出现的问题
1:git remote add 自定义远程仓库名字 (本次我的是my_summary)远程仓库的地址
2:git add .
3:git commit “注释”
4:git push -u 远程仓库名字 本地分支名字
报错:fatal: not a git repository (or any of the parent directories): .git
此处报错是因为少了初始化:git init
- 之后再执行以上命令到git push
报错:
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/fairy66/My_summary.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.
此处的报错大概意思是“远程仓库中的一些东西,本地是没有的,所以提交被拒绝”,需要执行git pull
- 接着执行git pull
- 报错:
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=my_summary/<branch> master
- 大概意思是你当前分支没有指定跟踪信息,需要指定与哪个分支合并。根据最后一行提示进行操作
- 解决:
git pull 远程仓库名字 本地分支名字
再执行
git branch --set-upstream-to=my_summary/<branch> master
<branch> 指的是远程分支的名字
**远程分支的信息也可以使用git branch -a
或者git branch -r
来查询,此处如下:
执行完这个命令,会提示关联成功,
- 再次执行git pull
- 报错:fatal: refusing to merge unrelated histories
解决-执行以下命令:翻译过来就是循允许不相关的历史记录
git merge my_summary/master --allow-unrelated-histories
之后出现:
输入i:注释——ESC——:wq回车—— git push -u 远程仓库名字 本地分支名字
报错:
You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you can merge.
错误可能是因为在你以前pull下来的代码没有自动合并导致的
解决方法:
1.保留你本地的修改
git merge --abort
git reset --merge
合并后记得一定要提交这个本地的合并
然后在获取线上仓库
git pull
2.down下线上代码版本,抛弃本地的修改
不建议这样做,但是如果你本地修改不大,或者自己有一份备份留存,可以直接用线上最新版本覆盖到本地
git fetch --all
git reset --hard origin/master
git fetch
3.合并代码
git stash
会把所有未提交的修改(包括暂存的和非暂存的)都保存起来,用于后续恢复当前工作目录。通过git stash命令推送一个新的储藏,当前的工作目录就干净了
git pull
拉取最新的代码
git stash pop
从git栈中获取到最近一次stash进去的内容,恢复工作区的内容。。