简体   繁体   中英

How do I git checkout a specific directory and strip the path to that directory?

I have a mono repo and want to checkout all the files from a path into a directory on my web server.

I know that I can checkout a directory by doing: git checkout origin/master -- path/to/directory , but that gives me my files in path/to/directory .

I want the checkout files, from the remote directory, in my current directory. So I have tried git --work-tree=. checkout origin/master -- path/to/directory git --work-tree=. checkout origin/master -- path/to/directory . But that does not alter the output directory path.

Right now, I just move the files but this quickly gets tedious ( mv path/to/directory/* . ). Is there a git command that can help me?

This question is similar to this unanswered question: Git checkout to specific directory

I would write a script checkout_git_dir_and_move_file_here.sh :

git checkout origin/master -- $1
mv $1/* .
rmdir -p $1

and place it in ~/bin . Then you can call it from wherever.

Git is a command-line tool, so it's natural to write shell scripts for dealing with things like this. A lot of more complex git tasks require explicit scripting.

While Git can be used as a deployment tool, there are better alternatives. But, we use Git for a very similar task that should work for you. However, this assumes your purpose is to only “checkout” files and not to commit changes back to the repository.

In our case, we have a repository containing a collection of Bash, Perl, and other scripts for different types of test servers. Due to similarities and for sanity's sake we keep them in one repository under separate directories for each server rather than a repository for each server.

To deploy scripts on a given server, say ServerA , we initially create a mirrored clone of the repository locally.

cd /opt/repos
git clone --mirror https://repo.mycompany.com/git/admin_repo

Updating the mirror as changes are committed to the main repository is performed as follows:

cd /opt/repos/admin_repo.git
git fetch --all

For this and other servers, scripts are in /usr/local/cscripts . To extract scripts for ServerA, which are contained within the repository under server_a/cscripts , the following commands are used

cd /usr/local/cscripts
rm -rf *
git archive --remote=/opt/repos/admin_repo.git HEAD:server_a/cscripts | tar -x

In essence, the resulting tarball is immediately extracted into the current directory without regard for the repository's path. It's not a perfect solution, because changes in filenames or subdirectory layouts are not automatically applied, hence the rm --rf to ensure a blank slate.

Edit: I failed to mention that we use a mirror of the repository because our current repository server only uses simple HTTPS and does not support git: or smart-HTTPS. If your repository is already local or maintained on a better repository server, you can extract archives directly without a mirror.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM