日常问题之收集:Git篇(持续更新)

error: You have not concluded your merge (MERGE_HEAD exists)

error: You have not concluded your merge (MERGE_HEAD exists).  
hint: Please, commit your changes before merging.  
fatal: Exiting because of unfinished merge.

这个错误可能是因为在你一千pull下来的代码没有自动合并导致的。解决方法如下,首先强制取消合并:

$ git merge --abort
$ git pull
# do some merge
$ git commit -m "merge"

然后手动进行冲突合并操作,最后提交此次修改。

warning: push.default is unset; its implicit value is changing in Git 2.0 from ‘matching’ to ‘simple’.

解决方法:直接运行 git config –global push.default simple 即可

Ubuntu14.04上vscode总是提示git1.9应升级到git2.0,如何做?

# 删除旧版git
$ apt-get remove git
# 安装新版git
$ apt-add-repository ppa:git-core/ppa
$ apt-get update
$ apt-get install git
$ git --version

如何删除并忽略已经添加到git版本库的目录和文件

问题描述:vendor文件夹,之前把这个目录的内容都提交到git上面了,后来发现不应该提交,那么如何才能在之后的提交中忽略该目录呢?

解决:

$ git rm --cached logs/xx.log,

然后更新 .gitignore 忽略掉目标文件,
最后提交这次修改:

$ git commit -m "We really don't want Git to track this anymore!"

参考:git忽略已经被提交的文件 - SegmentFault

回退到过去的版本之后再如何回到未来的版本?

问题描述:使用git reset --hard COMMIT_ID回退到过去的版本之后,再想回到未来最新的版本时,但是 COMMIT_ID 忘记了,那么如何回到未来最新的版本?

解决:当使用 git reset --hard COMMIT_ID 回退版本时,不要在原来的开发分支上操作,而是新建一个分支,在新分支上进行操作(而且以后这种类似的对过去的版本的操作应该养成开一个新的临时分支进行操作的习惯)。在新分支上操作时,当想要回到未来最新的版本时,可以切换到原分支,复制其 COMMIT_ID2,然后切换回新分支,运行 git reset --hard COMMIT_ID2 即可回到未来的最新分支。但是如果已经直接在原来的开发分支上进行回退操作了,可以使用 git reflog 获得过去操作的记录,从中可以找到最新的 COMMIT_ID,然后同上命令回到未来分支。

error: unable to delete BRANCH: remote ref does not exist

解决方法:先执行下面的命令,删除本地git缓存,然后再执行删除操作

$ git fetch -p origin

Your configuration specifies to merge with the ref ‘refs/heads/dev-price’

使用 git pull 时发生这个问题,如何解决?

$ git pull
Your configuration specifies to merge with the ref 'refs/heads/dev-price'
from the remote, but no such ref was fetched.

这是因为远程该分支不存在而导致的错误,可能是别人已经删除了该远程分支,执行下面的命令刷新一下缓存就清楚了:

$ git fetch -p origin

fatal: refusing to merge unrelated histories

解决:https://www.centos.bz/2018/03/git-%E5%87%BA%E7%8E%B0-fatal-refusing-to-merge-unrelated-histories-%E9%94%99%E8%AF%AF/

git merge的撤销

如果没有进行后续的修改,则可以:

$ git checkout <你的分支>
$ git log
# 找到你的分支合并前的那次提交 commmitID
$ git reset --hard <commitID>

提交并推送修改到错误的分支上面

有三种解决方法:

第一种:远程回退

$ git reset --hard ^HEAD
$ git push --force

但这种方法有风险,可能会把别人的提交冲没了。

第二种:在自己的分支上再提交一次

$ git checkout <错误的分支>
$ git log
# 找到你要回退的版本 CommitID
$ git reset <CommitID>
$ git checkout <正确的分支>
$ git add .
$ git commit -m "正确的提交"
$ git push

设置 Git 命令简写

# windows 下面
$ vim ~/.gitconfig
# 增加以下内容,保存即可
-----------------------
[alias]
        st = status
        br = branch
-----------------------

Windows 下 Gitk 中文乱码

原因是 Windows 中的 Gitk 默认使用 GBK 编码显示,而 Commit 信息是用 UTF-8 编码保存,使用下面命令设置一下即可:

git config --global gui.encoding utf-8

git status 中文乱码

执行以下命令进行配置:

$ git config --global core.quotepath false

本地忽略一些文件,不添加到gitignore文件中

How to exclude files from Git only on your computer

GIT 提供了一个 .git/info/exclude 文件,这个文件就相当于 .gitignore,但它的作用就是只在本地计算机上忽略某些文件。


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com