Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to auto update my local branch when a change happens in remote tracking branch?

Tags:

git

shell

I want to run something in the background which basically does a git pull --rebase whenever some changes happen in my remote branch. Most of the time it work silently in the background if there are no conflicts. In there is a conflict it just leaves me in the rebase-resolve-conflict stage and until I resolve everything it waits. How do I do this? Is there an existing software that already does this?

like image 674
pathikrit Avatar asked Oct 12 '17 17:10

pathikrit


People also ask

How do you update your working tree with changes from a remote?

If you want to have a working tree on a remote machine that you push to you can either run 'bzr update' in the remote branch after each push, or use some other method to update the tree during the push. There is an 'rspush' plugin that will update the working tree using rsync as well as doing a push.

Which command used to updates our local branches from the remote?

git fetch is used in conjunction with git remote , git branch , git checkout , and git reset to update a local repository to the state of a remote. The git fetch command is a critical piece of collaborative git work flows.


1 Answers

If you don't have control on the remote repository, one solution is to use crontab to run periodically git fetch or maybe even a git pull --rebase as you propose. The exact command to choose depends on your workflow, personally I prefer to use a git fetch because I can decide when and how to merge or rebase.

To run the command periodically run:

crontab -e

And add a line such as:

* * * * * git -C PATH_TO_LOCAL_REPO fetch

or

* * * * * git -C PATH_TO_LOCAL_REPO pull --rebase

This will run the git command every minutes with your user permissions.

If you want to apply the git command on a list of repository, you can can add the line:

* * * * * /home/myself/scripts/git-refresh.sh

where git-refresh is a script that apply on all your repositories.

The -C option allows you to run a git command without changing directory. From the man page:

-C <path>

Run as if git was started in "path" instead of the current working directory.

like image 196
Ortomala Lokni Avatar answered Sep 18 '22 00:09

Ortomala Lokni