Home > Blockchain >  How to checkout folder into another folder GIT
How to checkout folder into another folder GIT

Time:08-07

I know git checkout cmd to switch and create branch, but unable to understand folder checkout, what it actually does. for ex : i have created a test-repo and cloned it to my machine, now i want to create another folder locally on my machine localfolder. can i checkout my test-repo to localfolder? after reading some posts here i found out below cmd is used to checkout, but local folder is not initialized, does it need to be initialized before running cmd?

git --work-tree=/Users/username/localfolder  checkout HEAD -- .

CodePudding user response:

now i want to create another folder locally on my machine localfolder. can i checkout my test-repo to localfolder?

You're into git's core, for my money it's both the reason Git won and the reason so many people hate it regardless of whether they also love it..

The reason for all the questions in the comments is, the answer to every question in this territory is "well, it doesn't really matter what you mean, you can do it, so... what exactly do you want? Of course Git can do it, doing it won't be hard, the hard part is figuring out that "what exactly you want" bit."

The simplest interpretation from the git core viewpoint, making as few assumptions about context as I can manage, is to just dump some committed tree into an arbitrary folder somewhere and still be able to use Git's machinery with it:

( export GIT_WORK_TREE=path/to/some/worktree/aka/folder
  export GIT_INDEX_FILE=path/to/my/manifest
  mkdir -p $GIT_WORK_TREE
  git read-tree -u --reset $revspec:$path
)

and for your example command $revspec:$path would be HEAD: (no dot, the core's not about dwim conveniences). or v1.0:html to load up that tag's html folder.

This is all one full mind-that-first step down from the "checkout" abstraction, and like I say it's why you're seeing all the "what do you actually want" questions in the comments. This alone is enough to do quite a lot with that checked-out folder, from here it's only a question of how many more of Git's conveniences, how much of the vcs layer, you really intend to hook in to later.

When I'm doing stuff like this I tend to do scratch-monkey clones,

git clone -ns . `mktemp -d`; cd $_
git read-tree -um v2.30.0:Documentation

because those are dirt-cheap, well isolated, easy to just abandon, and still having basically all of Git's machinery on tap. Instead of `mktemp -d` you can use path/to/localfolder

CodePudding user response:

folder (path I guess would be a better way to call it, it works for files or directories) checkout is used when you want to get something from a different branch or from HEAD into your working tree. So.... one question I have seen rather often the last few days: How can I get the content of the project from a different branch into my current branch?

git checkout the-other-branch -- .

(assuming no renames or deletions... if you have any of those, it might be better to run git rm -r . before doing the checkout).

Another common use is: How can I revert the changes I have applied on the file? (like, I want to get rid of my change)

git checkout HEAD -- some-file

To try to make UI a little more consistent, restore/switch were created. So switch will move you around the branches/revisions..... restore is what you use to get files in a certain way into the working tree/index. But the old checkout is around there to confuse all new users (it's not confusing to me.... just needed some different scenarios through the years to see all its different uses).

And then, consider using worktrees to checkout more stuff.... but I would not recommend to put it outside of the local repo, even if it is possible to do it:

git worktree add the-new-directory the-branch-I-want-there

Then there is no need to hack around. You go into the directory and you are in the branch separate from the branch of the parent directory and ready to go without any additional hacks.

  • Related