auto-update-git is a bash script to automatically update a git repository to match the current state of a given remote git repository. It should handle any modification to the local repository: new commits, file modifications, a changed branch, and untracked files. It does not require hard-coding the HEAD branch of the remote repository, and should continue working if the remote repository's HEAD branch changes.
It can also do a "shallow" fetch from the remote repository, if you would like to only keep the last N commits on disk.
cd <repository>
auto-update-git [--depth=<depth>] <remote-url>The core algorithm is:
- Ensure that the
gitrepository has a remote calledoriginpointing at<remote-url>. - Set the
HEADof the remote automatically, thus discarding any local changes to the remoteHEAD, or picking up a new remoteHEAD. - Determine which branch the remote
HEADpoints to. - Fetch the branch the remote
HEADpoints to. - Forcibly checkout the branch the remote
HEADpoints to, potentially changing the current branch and overwriting any changes to a local branch of the same name. - Delete any branches other than the branch the remote
HEADpoints to. - Clean any untracked files.
There are crude (but effective) tests in the tests/ directory, you may execute them individually, for example:
./tests/test-01.sh