简体   繁体   中英

How to apply local changes to git repo after every pull from master without ever pushing to master

My development setup is quite different to the other developers in the team - mac vs linux - docker vs vagrant

Every time I pull a new branch I have to make a load of changes to config files and docker related files to get my dev environment functioning.

I need to avoid pushing these to my working branches as they can't be merged into master.

At the moment I am leaving these files off git add and then manually adding them back every time I switch branches and perform a pull.

The following SO question suggests I could git stash and then git stash apply How to keep the git local changes without commit and apply to different repos after switch

I think that would work providing I keep my git stashes neat and the last one is restricted to my dev changes. I'm wondering if there is a better way to handle this workflow.

Can I save these changes in a dev-setup branch and use in a similar way to the stash method?

One solution could be to add theses files to your locally ignored files. It is located at .git/info/exclude .
It works the same way as the usual .gitignore (see https://git-scm.com/docs/gitignore ).

If the files are updated on the upstream, you will not see any modifications on the local files after a pull. And you will not be able to commit any changes to theses files until you remove them from this exclude file.

I am adding an answer in addition to the one I accepted earlier just to bring together all the additional learnings from implementing this is my environment.

Keeping a local developer version of a gitignore file for dealing with personal dev setup

This is a super useful link about the different types of gitignore : local(personal), shared or global(all repos).

Basically to keep your own local file versions, which will remain untouched by the pushes and pulls of git, you need to:

  • add the relevant files to the personal exclude file in .git/info/exclude (thanks to the accepted answer from @Abel)

Now the remaining process will differ depending on whether:

You want to delete the files from the remote server (which will affect all users of the repo) and just keep on local:

  • Remove these from the git cache (if they are already staged) and delete remotely on the next push git rm --cached file_name

You want to leave remote files alone and have git 'forget' about your local versions and their associated remote changes

  • Remove them from staging with git reset -- file_name
  • Remove these from the git workflow with git update-index --skip-worktree <file_name>

This Stack Overflow Q&A was particularly useful Git - Difference Between 'assume-unchanged' and 'skip-worktree'

Useful code snippets if you are using skip-worktree

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM