Stuck repo using stash after crlf normalization?

徘徊边缘 提交于 2019-11-29 06:35:48
Florian Breisch

After some research i guess the following happened. Your workmate changed the lineendings which caused the first pull-conflict.

That's why you stashed your work, pulled the stuff (now without problems) and started to apply the stash again.

With invoking git stash apply git starts a recursive merge in of your stashed changes.

The error-message just tells you ran into a merge-conflict. According to the developer's stash-documentation this may be resolved by a git stash drop after resolving the conflicts:

"Applying the [stash] can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards."

Conclusion: If the configuration of the line-endings must be done in a existing project it seems to be best practise to do so by using a .gitattributes in your project-folder. Since it gets distributed with your line-ending-change-commit, it will avoid the headaches switching your current work to the new normalization-standard.

Changing line endings within projects & .gitattributes

According to the developers documentation of .gitattributes you can change the line-endings for all files (in the active branch) with the following steps:

$ 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"

Replace <<filepattern>> with a pattern that matches your source files - in your case *.py for python-files (a good pattern-explanation is given in this .gitignore-description).

If you have more than one filepattern to add, you may add several line-ending-definitions to the .gitattributes.

Attention: Since the second step requires to delete the .git/index (which is branch-specific) this must be done in every single branch you'd like to keep.

If you have many branches to process, you might consider writing a short script iterating through your git branches.

The best way to reset your local module is to use

git clean -f -x -d

This effectively removes all untracked changes to your local module and puts it back in a 'vanilla' state.

-f clean files.
-x clean files normally ignored by .gitignore.
-d clean directories.

Now run git reset --hard origin/<BRANCH_NAME>. This will reset your branch to the state of the remote.

git status at this point should tell you:

# On branch master
nothing to commit (working directory clean)

If you have your actual changes stashed you should then be able to git stash apply to put them back correctly.

If git stash show stash@{0} shows more changes than you expected you can just apply the ones you want to see with git checkout stash@{0} -- <filename>.

Hope this helps

VonC

First, regarding the GitHub help page, I would seriously recommend setting:

git config --global core.autocrlf false

Let reserve eol modification to explicit declaration in .gitattributes files instead of a global magic rule: keep core.autocrlf to false.

Second, you can see if you observe the same eol modifications on git stash with git update-index --skip-worktree.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!