We have a build server which is designed to check-out a version of the code from git and build it. Generally the server will check-out and build the
git checkout <branch>
This is the mechanism we are currently using. The
git clone is performed just once as part of the setup, and then the following is performed for every build.
I have removed error handling for clarity.
# Undo any modifications made to the working tree by the build process git reset --hard # Remove any untracked build artifacts created by the build process git clean -fdx # Fetch all commits and new branches/tags from the git server. git fetch # Set the index and working directory to the required tag or branch. # Notes: # * If the last build was for the same branch, then this will not # update the working directory. # * The %TAG_OR_BRANCH% variable is passed in via the GUI. This will # be in the form of a branch name (develop|release/vX.X.X) or a tag (vX.X.X) git checkout %TAG_OR_BRANCH% # Bring the index and working tree up-to-date. # Notes: # * the --ff-only flag is probably redundant, but we want to make it # clear that no merging is expected. git pull --ff-only
We have created new tags/branches on a developer machine, pushed these to the server, and this script correctly fetches them and builds the results. We experimented with adding the
origin/ prefix to the branch or tag names, but this approach did not work for us (the tag names were not recognised).
I think the main difference between this script and the much simpler answer suggested by @Yasser is that the git HEAD is pointing at the correct place and the command
git status gives a sensible answer. Whether or not this is important - I'm not sure.