I’ve got some deployment scripts that automate cloning of a repository to a local dir. Unfortunately, I haven’t found a good way to handle updates, so I just wipe out the local dir beforehand. This is clearly very wasteful.
Ideally, I want a sequence of commands that will guarantee a particular local directory becomes a copy of a specified repo/branch, with the minimum amount of data transfer required. So if the dir is already on the right repo/branch, it will simply update it. If it’s on the right repo, but not the right branch, it will just switch to the new branch.
I’m sure there’s a way to do this by examining some of the files in .git and performing specific actions depending on what is found, but I’m hoping there’s a simpler way.
One important note is that any changes in the local dir can always be discarded.
There may be a question already that answers this, but I haven’t been able to find the right wording to find it.
Note that this does leave old objects in the repo. If at any point you want to irreversibly delete the unreferenced objects and clean the repository, you can run