Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 395
Andrew Grimm Avatar asked Feb 29 '16 01:02

Andrew Grimm


People also ask

What is Skip Worktree?

--skip-worktree explained: This allows you to make changes to a file that you don't want to be pushed to upstream.

What is assume unchanged in git?

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git.


1 Answers

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.

like image 177
LightBender Avatar answered Oct 24 '22 03:10

LightBender