When using git, the general usage is CD to the repository directory for related operations, such as there is such a repository, D: \ pygit2 (through git clone https://github.com/libgit2/pygit2.git)
The general practice is cd d: \ pygit2, and then execute Git-related commands, such as git status and git log. In this case, the default git command is. gitr and working-tree are in the same directory.
In fact, there is another way to explicitly specify Git-Dir (. git directory) and working-tree. This is the -- git-Dir and -- work-tree parameters described in this section.
What should I do if I want to copy some commit in pygit2 repository to a directory other than D: \ pygit2? Of course, you can create a repository by using git clone, but is it the most concise way? In addition, git clone has other unnecessary overhead. In some cases (such as a large number of commit, Branch) may not be suitable. In this case, you can use the GIT checkout or git reset command to implement it. You only need to explicitly specify the working-tree. The specific implementation method is as follows:
Suppose you want to copy the master in D: \ pygit2 to D: \ Bak.
Method 1,
1.1 git -- git-Dir = D :\\ pygit2 \. Git -- work-tree = D :\\ Bak status
At this time, you can see a lot of prompts, which means that many files are missing compared with D: \ Bak and D: \ pygit2 repository.
1.2 git -- git-Dir = D :\\ pygit2 \. Git -- work-tree = D: \ Bak checkout-B TMP-F master
Use-F to force checkout and create a branch of TMP
1.3 git -- git-Dir = D :\\ pygit2 \. Git -- work-tree = D: \ Bak branch-D TMP
Delete TMP Branch
1.4 now we can see that the content in D :\\ Bak has been updated to d :\\ pygit2 \. Git master branch.
Method 2,
2.1 git -- git-Dir = D :\\ pygit2 \. Git -- work-tree = D: \ Bak reset -- quiet -- hard
2.2 now we can see that the content in D :\\ Bak has been updated to d :\\ pygit2 \\. git master branch content, but this method has some drawbacks, that is, the content in the index has been lost and you want to restore
Therefore, it is easier to exercise caution before using reset.
Note that git-Dir must pass in git repository, that is, the. Git folder.