本地有修改和提交,如何强制用远程的库更新更新。我尝试过用git pull -f,总是提示 You have not concluded your merge. (MERGE_HEAD exists)。
我需要放弃本地的修改,用远程的库的内容就可以,应该如何做?傻傻地办法就是用心的目录重新clone一个,正确的做法是什么?
正确的做法应该是:
git fetch --all
git reset --hard origin/master
git fetch 只是下载远程的库的内容,不做任何的合并.git reset 把HEAD指向刚刚下载的最新的版本
git-reset - Reset current HEAD to the specified state
--hard
Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.
参考链接:
http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull
在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件)。这个文件每一行保存了一个匹配的规则例如:
# 此为注释 – 将被 Git 忽略
*.a
# 忽略所有 .a 结尾的文件
!lib.a
# 但 lib.a 除外
/TODO
# 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/
# 忽略 build/ 目录下的所有文件
doc/*.txt
# 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
git
rm
-r --cached .
git add .
git commit -m
'update .gitignore'
git rm --cached <file>
will completely remove the file's contents from the index. This means that on commit the file will be removed from the HEAD
commit. (If the file was only added to the index and not yet tracked this is a "no-op".)
git reset -- <file>
resets the contents of the file in the index to be the same as the head commit. This means that on commit no changes will be committed to the file. This operation is not valid if there is no tracked version of the file in the HEAD
commit.
http://stackoverflow.com/questions/12661306/git-rm-cached-file-vs-git-reset-file
[git] warning: LF will be replaced by CRLF | fatal: CRLF would be replaced by LF
遇到这两个错误,是因为Git的换行符检查功能。
core.safecrlf
Git提供了一个换行符检查功能(core.safecrlf),可以在提交时检查文件是否混用了不同风格的换行符。这个功能的选项如下:
false - 不做任何检查
warn - 在提交时检查并警告
true - 在提交时检查,如果发现混用则拒绝提交
建议使用最严格的 true 选项。
core.autocrlf
假如你正在Windows上写程序,又或者你正在和其他人合作,他们在Windows上编程,而你却在其他系统上,在这些情况下,你可能会遇到行尾结束符问题。这是因为Windows使用回车和换行两个字符来结束一行,而Mac和Linux只使用换行一个字符。虽然这是小问题,但它会极大地扰乱跨平台协作。
Git可以在你提交时自动地把行结束符CRLF转换成LF,而在签出代码时把LF转换成CRLF。用core.autocrlf来打开此项功能,如果是在Windows系统上,把它设置成true,这样当签出代码时,LF会被转换成CRLF:
$ git config --global core.autocrlf true
Linux或Mac系统使用LF作为行结束符,因此你不想 Git 在签出文件时进行自动的转换;当一个以CRLF为行结束符的文件不小心被引入时你肯定想进行修正,把core.autocrlf设置成input来告诉 Git 在提交时把CRLF转换成LF,签出时不转换:
$ git config --global core.autocrlf input
这样会在Windows系统上的签出文件中保留CRLF,会在Mac和Linux系统上,包括仓库中保留LF。
如果你是Windows程序员,且正在开发仅运行在Windows上的项目,可以设置false取消此功能,把回车符记录在库中:
$ git config --global core.autocrlf false
Git 恢复本地误删的文件
通过git进行代码管理的项目,如果在本地编辑的过程中误删了某些文件或者文件夹,可以通过git操作来复原。
Step 1: git status
查看本地对改动的暂存记录。如下图所示,本人误删了文件夹“../Server”。
Step 2:git reset HEAD [ 被删除的文件或文件夹 ]
tep 3:git checkout [ 被删除的文件或文件夹 ]
以Server文件夹为例,需要先后执行:
git reset HEAD ../Server
git checkout ../Server