Git Series 2-Git-Dir & work-tree

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.