问题1: git Your branch is ahead of ‘origin/master’ by 2 commits
遇到这种问题,表示在你之前已经有x个commit了,解决方式:
git reset HEAD~x
这里的x表示的就是在这之前已经有多少次的提交,这句命令的意思就是直接回退到x 个commit之前。
补充内容:撤消操作:
1. 撤消已暂存(git add)的文件: git reset HEAD file
$ touch 1.txt /* 新建文件 */
$ git add . /* 全部暂存 */
$ git status /* 查看文件状态 */
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: 1.txt
$ git reset HEAD 1.txt /* 取消暂存 1txt */
$ git status /* 再次查看文件状态,1.txt 已经被取消啦 */
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Untracked files:
(use "git add <file>..." to include in what will be committed)
1.txt
nothing added to commit but untracked files present (use "git add" to track)
2. 撤消commit操作: git reset HEAD^:
# HEAD是指向最新的提交,上一次提交是HEAD^,上上次是HEAD^^,也可以写成HEAD~2 ,依次类推。(如果加上 --hard 表示放弃所有本地改动)
git reset HEAD^
3. 版本回退: git reset commit_id
git log # 查看日志,找到需要回退的那次commit的 哈希值
git reset --hard commit_id # 回退到指定版本
问题2: The following untracked working tree files would be overwritten by merge
git pull的时候遇到这样的问题:
The following untracked working tree files would be overwritten by merge balabala...
解决办法:
git clean -d -fx
备注:会删除掉没有add到仓库的文件,操作记得慎重,以免改动文件的丢失。本质上就是远程操作仓库中没有被追踪的本地文件
git clean -n # 显示将要删除的文件和目录
git clean -df # 删除文件和目录
git clean -f # 删除文件
git clean -fX # 如果只想移除已被忽略的文件,请使用选项-X
git clean -fx # 如果想移除已被忽略和未被忽略的文件,请使用选项-x
问题3: error: Your local changes to the following files would be overwritten by merge:
本地修改了某个文件,在执行git commit操作之前,先执行git pull操作,如果此配置文件在服务器上已被别人做了修改,会发生代码冲突:
error: Your local changes to the following files would be overwritten by merge:
protected/config/main.php
Please, commit your changes or stash them before you can merge.
如果希望用代码库中的文件完全覆盖本地工作版本. 方法如下:
git reset --hard
git pull
如果希望保留生产服务器上所做的改动,仅仅并入新配置项, 处理方法如下:
git stash /* 暂存当前正在进行的工作 */
git pull /* 冲突的代码也可以pull下来 */
git stash pop
git diff -w +文件名 /* 确认代码自动合并的情况 */
/* 显示有人已经更改test = 3,而本次提交test =2 */
<<<<<<< Updated upstream
test = 3
=======
test = 2
>>>>>>> Stashed changes
/* 解决冲突,修改相应文件,选择需要保留的值,重新push */
git status
git add .
git commit -s
git push
如果你没有git pull,直接git push,发生冲突是提示:
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@code.csdn.net:qq_15437629/cstring.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.
此时你再执行git pull ,发生冲突会有如下提示:
Auto-merging conflict.txt
CONFLICT (content): Merge conflict in 1.txt
Automatic merge failed; fix conflicts and then commit the result.
如果你在commit之后就执行git pull,也会有以上提示,Auto-merging之后修改相应的文件,解决冲突即可。