简体   繁体   中英

Git command to get all files under a specific directory and hash

I am trying to get all the files under a specific directory using git. I am using git rev-parse origin/master to get the master branch hash. I now want to use this hash to get all the files under a specific directory.

I am able to get all the files using the command git ls-tree -r masterhash --name-only --full-tree but wondering if there is a better way for a specific directory.

You can specify a specific tree for a specific revision with a syntax like this: HEAD:sha256 . That refers to the tree object at the HEAD revision in the directory sha256 (eg, for the Git project repository).

If you want to get only the names, you can do this:

$ git ls-tree --name-only -r HEAD:sha256
block/sha256.c
block/sha256.h
gcrypt.h

If you want the path relative to the root of the repository, then write this:

% git ls-tree --name-only -r HEAD sha256
sha256/block/sha256.c
sha256/block/sha256.h
sha256/gcrypt.h

You don't need --full-name or --full-tree unless you believe that you'll be operating in a subdirectory and don't want to be restricted to that subdirectory. Additionally, while I've specified HEAD here, you can use any revision pattern you like (see gitrevisions(7) ).

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