Pie*_*rre 10 git newline git-stash
我有一个状态的本地仓库禁止我提交,藏匿,结账到另一个分支,甚至放弃更改.所以我只是卡住了.
据我记忆,我会试着描述一下这些情况给我带来了什么.
请坐下
不久之前,在远处的另一台计算机......根据以下内容在项目中另一个开发标准化的crlf:https://help.github.com/articles/dealing-with-line-endings
在那一刻(你知道,光速......)我在本地做了一些改变,提交和拉动.
当我拉Git说:
error: Your local changes to the following files would be overwritten by merge:
wp-config.php
Run Code Online (Sandbox Code Playgroud)
wp-config.php之前已从索引中删除,git update-index --assume-unchanged wp-config.php因为它的模板配置文件适用于每个本地环境.
基础"模板"可以改变.没什么好惊讶的.这就是我的计划:
wp-config.phpstash 我自己的配置发生变化pull origin masterstash apply 我的配置回来了在第3步出现问题git pull origin master仍然是上面的错误,好像存储无效.
git status说wp-config.php没有进行改变的提交.在藏匿之后,这让我感到惊讶.
自从我把我的变化藏起来后,我跑了git checkout -- wp-config.php......但没有任何效果!文件仍未进行提交.
由于我变得疯了,我创建了一个新的分支my-config,添加并提交wp-config.php到它,然后切换回master,删除wp-config.php(使用git rm),并合并origin/master ...成功!
所以现在主人是最新的和干净的,我计划在没有Git帮助的情况下恢复我自己的配置(手动编辑文件).
因为我想知道发生了什么,我切换到my-config分支,并尝试了一个非常简单的操作:
git stash
git stash apply
Run Code Online (Sandbox Code Playgroud)
你猜怎么着?stash apply失败说:
error: Your local changes to the following files would be overwritten by merge:
wordpress/license.txt
wordpress/readme.html
...
(all the files that where modified by the crlf conversion)
Run Code Online (Sandbox Code Playgroud)
现在我被困在我的分支上(计划看到它,法语国家会理解;))因为:
git stash apply,commit并checkout master给出上面的错误git stash 生成一个存储条目,但不会更改未暂存的状态git checkout -- <file>都不会删除未暂停的状态我现在唯一能做的就是删除所有这些文件(使用操作系统rm),以便能够返回到主分支.
真实的故事.
我很想了解主分支上发生了什么,然后在my-config分支上,以及在这种情况下带来了什么(我怀疑在crlf转换文件上使用存储).
重要笔记:
git core.autocrlf 是的 input.gitattributes和"处理行结尾"一文中的内容相同当我stash在my-config分支上执行时,它输出了:
warning: CRLF will be replaced by LF in wordpress/license.txt.
The file will have its original line endings in your working directory.
... (one for each crlf converted file) ...
Saved working directory and index state WIP on my-config: dbf65ad my config -- should not be pushed
HEAD is now at dbf65ad my config -- should not be pushed
Run Code Online (Sandbox Code Playgroud)
(dbf65ad是我在my-config分支上做的唯一提交)
经过一番研究后,我猜以下情况发生了.你的同事改变了导致第一次拉动冲突的线端.
这就是为什么你把你的工作藏起来,拉出东西(现在没有问题)并开始再次申请藏匿.
通过调用git stash applygit开始递归合并你的stashed更改.
错误消息只是告诉您遇到合并冲突.根据开发人员的存储文档,这可以通过解决git stash drop冲突后解决:
"应用[stash]可能会因冲突而失败;在这种情况下,它不会从存储列表中删除.您需要
git stash drop手动解决冲突并在之后手动调用."
结论:如果必须在现有项目中完成行结尾的配置,那么最好通过.gitattributes在项目文件夹中使用a来实现.由于它与您的行结束更改提交一起分发,因此可以避免将当前工作转换为新的规范化标准.
根据开发人员的文档,.gitattributes您可以使用以下步骤更改所有文件(在活动分支中)的行结尾:
$ echo "<<filepattern>> eol=lf" >>.gitattributes
$ rm .git/index # Remove the index to force Git to
$ git reset # re-scan the working directory
$ git status # Show files that will be normalized
$ git add -u
$ git add .gitattributes
$ git commit -m "Introduce end-of-line normalization"
Run Code Online (Sandbox Code Playgroud)
更换<<filepattern>>与匹配您的源文件的模式-你的情况*.py为Python-文件(的良好格局,说明在此给出.gitignore-description).
如果要添加多个文件模式,则可以向该文件添加多个行结束定义.gitattributes.
注意:由于第二步需要删除
.git/index(特定于分支),因此必须在您希望保留的每个分支中执行此操作.
如果要处理许多分支,可以考虑编写一个通过git分支迭代的短脚本.
重置本地模块的最佳方法是使用
git clean -f -x -d
Run Code Online (Sandbox Code Playgroud)
这有效地删除了对本地模块的所有未跟踪的更改,并将其恢复为“普通”状态。
-f干净的文件。
-x通常被 .gitignore 忽略的干净文件。
-d干净的目录。
现在运行git reset --hard origin/<BRANCH_NAME>。这会将您的分支重置为远程状态。
git status此时应该告诉你:
# On branch master
nothing to commit (working directory clean)
Run Code Online (Sandbox Code Playgroud)
如果您保存了实际的更改,那么您应该能够git stash apply正确地将它们放回原处。
如果git stash show stash@{0}显示的更改比您预期的要多,您可以仅应用您想要查看的更改git checkout stash@{0} -- <filename>。
希望这可以帮助