Check out the specified folder on git to the local code

Time:2021-4-3

When the project is too large, it’s a headache to pull the project from the server. Let’s talk about how to pull only one or several folders to the local area.

Upper Code:


git clone -n [email protected]:test/test_platform.git
cd test_platform
git config core.sparsecheckout true
echo webapp/css >> .git/info/sparse-checkout
git checkout master

If you need to check out multiple folders, and so on.


echo webapp/js >> .git/info/sparse-checkout
echo webapp/img >> .git/info/sparse-checkout

Finally, continue to execute the command and check out the project locally


git checkout master

Simple and practical, let’s go!

PS: let’s take a look at the use of check out in Git syntax

Case 1: when git add is not used to cache Code:

//Abandon a single file modification, pay attention not to forget the middle of the "--", do not write will become a check out branch!
git checkout -- filepathname
//Discard all file modifications
git checkout .

This command is used to discard all modifications that have not been added to the cache (GIT add command): content modification and entire file deletion. However, this command does not delete the newly created file. Because the newly created file has not been added to the GIT management system. So it’s unknown for GIT. Just delete it manually.

Case 2: git add has been used to cache the code

You can use git reset head filepathname (for example: git reset head) readme.md )To discard the cache of the specified file. To discard all the caches, you can use git reset head. Command.

This command is used to clear git’s cache of file modifications. It is equivalent to canceling the work of GIT add command. After using this command, the local modification will not disappear, but return to the state as shown in (1). If you continue with the operation in (1), you can discard the local modification.

Case 3: the code has been submitted with git commit

You can use * * git reset — hard head ^ to go back and forth to the last commit state.
This command can be used to fallback to any version: git reset — hard committed**

You can use the * * git log * * command to view git submission history. The output of GIT log is as follows. Here you can see that the first line is commitid:

Here is the article about checking out from git to local. For more information about checking out folder on git, please search previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!