Git automatic fetch periodically/on branch checkout

浪尽此生 提交于 2019-12-16 18:05:49

问题


I would like some automated method to do a fetch from our remote origin server. So perhaps every time I switch branches git would automatically do a fetch on all branches from origin before the switch... this way when I move from a local "fix branch" to master it would tell me that my master is behind origin master.

Is this possible? A more detailed reason for my requirement is below if you need to know why I want this.

Thanks guys!

I work in a team of developers. When I add a change to our origin/master branch I also upload that change to our FTP server (we have not yet setup git on our server)

If I create a "fix branch" and make changes to a file... Then I want to get these files live and into master. If I simply merge the changes in my local file into my local master without a fetch this could cause problems. if I forget to bring in changes from our origin prior to upload the file I may be uploading the file with missing revisions from origin. (naughty I know)

Also I would like to be aware of any updates in the master as soon as possible so that I can incorporate them into my "fix branch"


回答1:


Git doesn't have any built-in scheduling features. You'll have to use whatever your operating system provides: cron or Windows Task Scheduler or write a batch file to sleep, fetch, sleep, fetch, or whatever.




回答2:


One option is to use a git alias:

git config --global alias.co "!f() { git fetch && git checkout $1; }; f"

If you use the alias like git co <branch>, it'll do a fetch before checkout. (Thanks to Git alias with positional parameters for demonstrating how to do this)




回答3:


A cron job makes sense here, but I bet you could also write your own client-side git hook to do this for you. It would fire off a script whenever its corresponding git command gets executed. Consider, for example, the post-checkout hook which is invoked whenever you run a git checkout.

http://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks



来源:https://stackoverflow.com/questions/30256976/git-automatic-fetch-periodically-on-branch-checkout

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