Handling changes to files with --skip-worktree from another branch

懵懂的女人 提交于 2020-04-29 09:11:32

问题


On my machine, I've set --skip-worktree to config/database.yml.

git update-index --skip-worktree config/database.yml

Another developer has committed and merged into the develop branch changes to config/database.yml while working on the project.

Now, when I do git pull origin develop, I get

Andrews-Air:[project] agrimm$ git pull origin develop
From bitbucket.org:[company]/[project]
 * branch            develop    -> FETCH_HEAD
Updating [SHA]..[Another SHA]
error: Your local changes to the following files would be overwritten by merge:
    config/database.yml
Please, commit your changes or stash them before you can merge.
Aborting

How should I handle such a change? Should I do

git update-index --no-skip-worktree config/database.yml
git stash save "Save changes to config/database.yml"
git pull origin develop
git stash apply
# Fix any conflicts
git update-index --skip-worktree config/database.yml

Or is there a less hacky approach?


回答1:


That isn't a hacky approach at all. It's actually exactly what you need to do in this situation. Let's go through the scenario step by step. (Making a few assumptions along the way because it's a YML file.)

You're ignoring the file because you have changes that only affect your local build process and don't want to accidentally push those changes to others and potentially affect them or, worse yet, production. A fairly normal and good practice.

Sometimes these files do need to be changed, as in this situation, so an updated version gets committed. Pulling in these changes will trigger a check to verify that the file it's overwriting matches the one in the commit and, if not, it throws the error. (Failing the integrity check will ignore the flag)

You're actively ignoring the file, so you need to tell git to care about it again. Otherwise no git process will see the file.

git update-index --no-skip-worktree config/database.yml

Then put your changes somewhere safe, and since you don't want to commit them, stash is the best place:

git stash save "Save changes to config/database.yml"

You can finally bring the changes from the remote into your local branch. You've already fetched when you got the original error, so you can also just do the merge. So:

git pull origin develop
 -or-
git merge origin/develop

You're up to date now, but you still want those updates to make it work locally, so let's get them back from our safe storage place:

git stash pop

If there are any conflicts, you can resolve them and make sure your project will build correctly after integrating the new changes.

Finally, we want to protect the other users from our local changes again, so we reinstate the skip-worktree flag on the path.

git update-index --skip-worktree config/database.yml

And we're done.



来源:https://stackoverflow.com/questions/35690736/handling-changes-to-files-with-skip-worktree-from-another-branch

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